Button1.Attributes.Add("onClick", "javascript:fillTxt();")
Actual DotNet Code
We did the background work of formatting text Now we start the actual coding of sending mail by importing the namespace, "System.Web.Mail". Then, in the ButtonClick event, we create an instance of MailMessage object. It is through the MailMessage object, we set all the properties such as To, From, Subject, Body etc. We can either send a text message or a html message. We need to specify the bodyformat in the BodyFormat property. One we set all the properties, it is ready to send the email. Before sending the email, you have to set another important property, ie; SmtpServer. You have to set this property. You should assign the name of your SMTP server to this property. In most cases you can assign this as "localhost" or “127.0.0.1”. If you do not set this property, then you will not be able to send email from an ASP .NET page. Finally we send the email using SmtpMail.Send.
Button1.Attributes.Add("onClick", "javascript:fillTxt();")
Dim attach1 As String = ""
Dim strFileName As String = ""
Dim message As New MailMessage()
If (attachFile1.PostedFile.FileName <> "") Then
Dim ulFile As HttpPostedFile = attachFile1.PostedFile
Dim nFileLen As Int64 = ulFile.ContentLength
If (nFileLen > 0) Then
strFileName= Path.GetFileName(attachFile1.PostedFile.FileName)
strFileName = "Uploads/" + strFileName
attachFile1.PostedFile.SaveAs(Server.MapPath(strFileName))
Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))
message.Attachments.Add(attach)
attach1 = strFileName
End If
End If
message.From = TextBox2.Text
message.To = TextBox3.Text
message.Cc = txtcc.Text
message.Bcc = txtbcc.Text
message.Subject = TextBox4.Text
message.Body = hdnmsg.Value
message.BodyFormat = MailFormat.Html
SmtpMail.SmtpServer = "127.0.0.1"
SmtpMail.Send(message)
lblMessage.Text = "Your email has been sent"
Now, we send the email with the attachments:
To send attachments, we need to add attachments using the Add method, which is available in the Attachments object. The only thing that we need to add to the above example is
Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))
Enhancements that you add to the above examples.
We can add any number of attachments to an email. To send multiple attachments, just repeat the line Msg.Attachments.Add with the file that needs to be attached.
Points to remember:-
- Make sure that you have created a Folder “ Uploads” in the root and have full access to this folder.
- Do not foget to change the Form tag property to encType="multipart/form-data"
- Enable your browser for JavaScript because the colorpalette is based on JavaScript.
- Configure your Default SMTP Virtual Server as SmtpMail.SmtpServer = "127.0.0.1" or Localhost.
- Import namespace system.web.mail