|
Sending Email with ASP.NET
Sending Email With Attachment
ASP.NET also provides a mechanism to attach files with our emails. To send an attachment
with your email you need to use the following lines of code.
Dim Attach As MailAttachment = New MailAttachment("C:\images\sun.gif")
'creating an instance of MailAttachment class and specifying the location of attachment
mailMessage.Attachments.Add(Attach)
'adding the attachment to mailMessage
|
Sending Email to Multiple Recipients
To send an email to multiple recipients we need to specify the addresses of the recipients
seperated by a semicolon as shown in the line of code below.
|
mailMessage.To = "xyz@abc.com;san@universe.com;sen@baw.com"
|
Sending an HTML Email
We all know that email messages can be either in plain text or HTML. By default they
are sent as plain text, but we can change this to HTML by setting the BodyFormat property
to Html. The following lines of code will show that.
mailMessage.Body = "<html><body><b><u>" & "This tutorial is sending an HTML email_ with an ASP.NET app." & "</u></b></body></html>"
|
The text sent with the above lines of code will be bold and underlined. You can
use HTML tags to send an HTML email as shown above.
Complete Code Listing
Imports System.Web.Mail
'namespace to be imported
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim mailMessage As New MailMessage()
'creating an instance of the MailMessage class
mailMessage.From = "xyz@mydomain.com"
'senders email address
mailMessage.To = "xyz@abc.com;san@universe.com;sen@baw.com"
'multiple recipient's
mailMessage.Cc = "carboncopy@sendersemail.com"
'email address of the Cc recipient
mailMessage.Bcc = "blindcarboncopy@sendersemail.com"
'email address of the Bcc recipient
mailMessage.Subject = "Hello"
'subject of the email message
mailMessage.BodyFormat = MailFormat.HTML
'HTML message format
mailMessage.Body = "<html><body><b><u>" & "This tutorial is sending
an HTML email_
with an ASP.NET app." & "</u></b></body></html>"
'message body, html message
Dim Attach As MailAttachment = New MailAttachment("C:\images\sun.gif")
mailMessage.Attachments.Add(Attach)
'adding an attachment
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
SmtpMail.SmtpServer = "mail.yourserver.com"
'mail server used to send this email. modify this line based on your mail server
SmtpMail.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
'message stating the mail is sent
End Sub
|
|