Let us check a mail message created by System.Net.Mail.MailMessage
to send a default mail.
Listing 1 – Sample for plain mail
Public Sub SendPlainMail()
'create the mail message
Dim mail As New MailMessage("from@fromdomain.com", "to@todomain.com")
'set the message content
mail.Subject = "This is a plain text mail"
mail.Body = "This is a sample body... for default Mail Clients.."
'send the mail using SMTP Client
Dim smtp As New SmtpClient("My Mail Server") 'mail Server IP or NAME
smtp.Send(mail)
End Sub ' End SendPlainMail
Here, "mail" is the instance of MailMessage Class.
There are four overloaded constructors available initializing different
properties of MailMessage Class during making instance.
·
MailMessage (): Initializes an empty instance of the MailMessage
class.
·
MailMessage (from as MailAddress, to as MailAddress): Initializes
a new instance of the MailMessage class by using from and to as MailAddress
class objects.
·
MailMessage (from as String, to as String): Initializes a new
instance of the MailMessage class by using from and to as String objects.
·
MailMessage (from as String, to as String, subject as String,
body as String): Initializes a new instance of the MailMessage class by using
from, to, subject and body as String objects.
Again, "smtp" is the instance of SmtpClient Class
with Mail Server as parameter, which does the actual mail sending action. There
are three available constructors of SmtpClient Class as:
·
SmtpClient (): Initializes a new instance of the SmtpClient
class.
·
SmtpClient (host as String): Initializes a new instance of the
SmtpClient class that sends email by using the specified SMTP server (host).
·
SmtpClient (host as String, port as Int32): Initializes a new
instance of the SmtpClient class that sends email by using the specified SMTP
server and port.
The MIME Source for above mail is:
Listing 2
'--------------------------------
x-recipient: <to@todomain.com>
Received: xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
(###### #, #, #, #); Fri, 22 Jun 07 18:53:37 +0530
mime-version: 1.0
from: from@fromdomain.com
to: to@todomain.com
date: 22 Jun 2007 18:53:37 +0530
subject: This is a plain text mail
content-type: text/plain; charset=us-ascii
content-transfer-encoding: quoted-printable
Message-Id: <f0706221908020DDAxxxxxx>
X-Antivirus: AVG for E-mail 7.5.472 [269.9.4/860]
This is a sample body... for default Mail Clients..
'--------------------------------
Here, the "content-type" field value is "text/plain,"
i.e. content-type: text/plain, which specifies that this Mail Client (Outlook
or Eudora) is a plain mail only.
By default the mail message created by System.Net.Mail is a
plain text, whose default value for the Public Property MailMessage.IsBodyHtml
is False. To format a message as Html we need to set this property MailMessage.IsBodyHtml
property to True.
Listing 3 – Sample for HTML mail
Public Sub SendHtmlMail()
'create the mail message
Dim mail As New MailMessage("from@fromdomain.com", "to@todomain.com")
'set the message content
mail.Subject = "This mail has Html Body.."
mail.Body = "This is a sample body with html in it. <b>This is bold</b> <font
color=#336699>This is blue</font>"
mail.IsBodyHtml = True
'send mail
SendMail(mail)
End Sub ' End SendHtmlMail
Private Sub SendMail(ByVal mail As Mail.MailMessage)
'send the message using SMTP client
Dim smtp As New SmtpClient(_mailServer) 'mail Server IP or NAME
smtp.Credentials = CredentialCache.DefaultNetworkCredentials
smtp.Send(Mail)
End Sub ' End SendMail
The MIME Source for above mail is:
Listing 4 – Mime source of HTML mail
'--------------------------------
x-recipient: <to@todomain.com>
Received: xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
(###### #, #, #, #); Fri, 22 Jun 07 18:59:12 +0530
mime-version: 1.0
from: from@fromdomain.com
to: to@todomain.com
date: 22 Jun 2007 18:59:12 +0530
subject: This mail has Html Body..
content-type: text/html; charset=us-ascii
content-transfer-encoding: quoted-printable
Message-Id: <f0707041437531C0Axxxxxx>
X-Antivirus: AVG for E-mail 7.5.475 [269.9.4/860]
This is a sample body with html in it. <b>This is bold</b>
<font color=3D#336699>This=is blue </font>
'--------------------------------
The "content-type" field value is "text/html"
i.e. content-type: text/html, which specifies to the Mail Client (Outlook or Eudora)
that this mail contains html content.