E-Mail is an extremely popular communcation mechanism. For a developer, e-mail is an effective way to allow an application to send files or reports to users and to notify users of problems or events.
The .NET Framework inludes the System.Net.Mail namespace, which provides classes that enable you to easily create and transmit e-mail messages.
1. Mailing The developer’s eye view
Microsoft.NET Framework
Visual Basic
Lessons:
No.1 - Creating an E-Mail Message
No.2 - Sending an E-Mail
Violeta Salas – Notes
2. Creating a MailMessage Object
There are 2 different constructors that allow you to create a MailMessage:
Visual Basic:
Dim m As MailMessage=New MailMessage (“jane@nmbusiness.com”,_
“ben@nmbusiness”, “Data Report”, “See the attached spreadsheet.”)
Dim m As MailMessage=New MailMessage _
(New MailAddress(“lance@md.com”, ”Lance Tucker”),_
New MailAdrress (“lance@md.com”, “Ben Miller”))
3. Adding MailAddress objects to
the MailMessage.To property
Use the MailAddressCollection type to specify:
• MailMessage.From
• MailMessage.Subject
• MailMessage.Body
Dim m As MailMessage=New MailMessage()
m.From = New MailAddress (“lance@md.com”, ”Lance Tucker”)
m.To.Add(New MailAddress (“james@md.com”, “James van Eaton”))
m.To.Add(New MailAddress (“ben@md.com”, “Ben Miller”))
m.To.Add(New MailAddress (“burke@md.com”, “Burke Fewel”))
m.Subject=“Data Report”
m.Body=“See the attached spreadsheet.”
4. How to attach files
• Add it to the MailMessage.Attachments AttacmentCollection by calling
the MailMessage.Attachments.Add method, the simplest way to add a
file is to specify the filename:
Dim m As MailMessage=New MailMessage ()
m.Attachments.Add(New Attachment(“C:windowswin.ini”))
• You can also specify a Multipurpose Internet Mail Extensions (MIME)
content type using the System.Net.Mime.MediaTypeNames
enumeration. This requires Systema.IO and System.Net.Mime in
addition to System.Net.Mail.
Using Steam as a file attachment and how to specify the Mime type:
Dim m As MailMessage=New MailMessage ()
Dim sr As Stream= New FileStream("C:Attachment.txt", FileMode.Open, FileAccess.Read)
m.Attachments.Add(New Attachment(sr, “myfile.txt”, MediaTypeNames.Application.Octet))
5. Creating HTML Emails
Supply HTML – tagged content for MailMessage.Body and set the
MailMessage.IsBodyHtml attribute to true:
Dim m As MailMessage=New MailMessage
m.From= new MailAddress(“lance@md.com”,”Lance Tucker”)
m.To.Add(New MailAddress(“burke@md.com”,”Burke Fewel”))
‘Specigy an HTML message body
m.Body= _
"<html><body><h1>My Message</h1><br>This is an HTML message.</body></html>"
m.IsBodyHTML=true
‘Send the message
Dim client As SmtpClient= New SmtpClient(“smtp.md.com”)
Client.Send(m)
6. To embed images into an
HTML message
They appear when the user clicks the message, use the AlternateView and
LinkedResource classes:
‘Create the HTML message body
‘Reference embedded images using the content ID
Dim htmlBody As String="<html><body><h1>Picture</h1><br>" + _
"<img src=""cid:Pic1""></body></html>“
Dim avHtml As AlternateView=AlternateView.CreateAlternateViewFromString(_
htmlBody, Nothing, MediaTypeNames.Text.Html)
‘Create a LinkedResource object for each embedded image
Dim pic1 As LinkedResource = New LinkedResource(_
“pic.jpg”, MediaTypeNames.Images.Image.Jpeg)
Pic1.ContentId=“Pic1”
avHtml.LinkedResources.Add(pic1)
7. ‘Create an alternate view for unsupported clients
Dim textBody As String= _
"You must use an e-mail client that supports HTML messages“
Dim avText As AlternateView=AlternateView.CreateAlternateViewFromString(_
textBody, Nothing, MediaTypeNames.Text.Plain)
‘Add the alternate views instead of using MailMessage.Body
Dim m As MailMessage= New MailMessage
m.AlternateViews.Add(avHtml)
m.AlternateViews.Add(avText)
‘Address and send the message
m.From= New MailAddress(“lance@md.com”,”Lance Tucker”)
m.To.Add(New MailAddress(“james@md.com”,”James van Eaton”))
m.Subject= “A picture using alternate views”
Dim client As SmtpClient = New SmtpClient(“smtp.md.com”)
Client.Send(m)
8. The code produces de HTML message
shown.
- Figure: Using AlternateView and
LinkedResource to embed images in an
e-mail.
9. LAB:
Now you will generate an
E-Mail Message
Exercise: Create a MailMessage Object
In this exercise, you will create a MailMessage object based on user input
into a Microsoft Windows Forms application that is provided for you.
10. Lesson Summary
• To send an email message create a MailMessage Object, specify
• Sender, Subject, Body
And you can add recipients, alternate views and file attachments. Then
create an instance of the SmtpClient class, and call SmtpClient.Send or
SmtpCliente.SendAsync.
• The MailMessage object includes constructors that allow you to create
simple messages, messages with multiple recipients requiere adding more
code.
• To attac a file, create an instance of the Attachment class and add it to the
MailMessage.Attachments collection.
• Creating an HTML message without image is as simple as specifying HTML
in the body and setting MailMessage.IsBodyHtml to true. If you need to
include images in the message, create an AlternateView object and a
LinkedResource object for each image.