Skip Navigation LinksASP.Net 2.0 Training : Sending EMail
Banner for ASP.Net 2.0 training classes in Visual Basic 2005 

Skip Navigation Links
Visit my other site:

certification training

Free Flash tutorials and training

Free Dreamweaver Video Tutorials

Free PHP Training

Visual Basic Windows Training

If you like this site please link to it

Email in ASP.Net 2.0

How Email Works

Email server sends and retrieves your email message
Mostly 2 protocols used

  1. SMTP
    1. Simple Mail Transfer Protocol
    2. Send EMail
  2. POP
    1. Post Office Protocol
    2. Retrieve Email

Mail client software sends email
to a mail server using SMTP protocol

Mail server uses SMTP to send mail to recipient’s mail server
Recipient’s mail client uses POP to retrieve the email from their mail server

MIME protocol
Multi-purpose Internet Mail extension
Defines how the content and attachments are formatted

Create an email message

System.Net.Mail namespace has 4 main classes

  1. MailMessage
    1. The email message
  2. SMTPClient
    1. Sends mail message
  3. MailAttachment
    1. An email attachment
  4. MailAddress
    1. Email address used in
      • From
      • To
      • Cc
      • Bcc

The MailMessage Class

When you use a displayname it is displayed in the mail client’s email list instead of the
email address

Steps in creating an email message:

  • Create a mailMessage object
  • Assign the properties
  • Create a SMTPClient object
  • Specify details about SMTP server
  • Send the mailMessage with the SMTPClient object’s Send method




  • From: The sender of the email in the form of MailAddress instance.
  • To: Indicates direct recipients of the email in the form of MailAddress instances.
  • CC: The carbon copy recipients of the email in the form of MailAddress instances.
  • Bcc: The blind carbon copy recipients of the email in the form of MailAddress instances.
  • Subject: The subject of the message.
  • Body: The body of the message.
  • IsBodyHtml: Boolean value if the message body is HTML or text.
  • ReplyTo: Address where all replies are directed
  • Priority: Priority of the email message
    • Values-Normal, high, low


Dim message As New System.Net.Mail.MailMessage(" ",  " ")
message.Subject = "test"
message.Body = "This is a test"
Example 2:
Dim message As New System.Net.Mail.MailMessage()
message.From = New System.Net.Mail.MailAddress("")
‘To must be set with the to.add as shown
message.To.Add(New System.Net.Mail.MailAddress(""))
message.Subject = "Comments from"
message.Body = String.Concat("Name:", txtName.Text, "Email:", txtEmail.Text, "Comments:", txtComment.Text)

Send email message

 Use the send() method of the SMTPClient Class to send your message after you create it


SMTPClient()       when setting up in web.config
SMTPClient(name)       name of smtp server


Send(message)   sends mailmessage object
Send(from, to, subject, body)

Dim client as new SMTPClient()

Dim client as new smtpClient(“emailservername”)
Client.send(“”, txtTo.text, txtSubject.text,txtBody.text)

Dim client as new System.Net.Mail.smtpClient()

Set up SMTP server setting in the web.config

You must specify the hostname and optionally the port which is normally 25

      <network host="" 
      userName="email@" password="password" />


Sending messages to more than one email address:
Create a MailAddress object for each To property
Dim message as New MailMessage()
Message.from=new MailAddress(“”)“”)“”)

Constructors of MailAddress

Create email with CC
Dim fromAddress as new MailAddress(txtfrom.text,txtDisplay.text)
Dim toAddress as new MailAddress(txtTo.text)
Dim ccAddress as new MailAddress(txtCC.text)
Dim message as new MailMessage(fromAddress,toAddress)

Alternative Method
Dim message as New MailMessage(txtfrom.text, txtTo.text, txtSubject.text,txtBody.text) MailMessage(ccAddress)

Attachments-collection of attachment objects

Add Attachment to email message
Attachment is file sent with email message

SMTP is designed to send text messages not binary files
You must convert them to text before it can be sent
Then it is converted back to binary when received
By default it uses UUEncode to convert it
New attachment(filename)
Dim myfilename as string
Dim myattach as new attachment(filename)
Dim message as new mailMessage(txtfrom.text, txtTo.text, txtSubject.text,txtBody.text)

Alternative method
Dim myfilename as string
Message.attachments.add(new attachment(myfilename))

Using FileUpload control to add attachments

Add a fileupload control to the page
Add this code:

If FileUpload1.HasFile Then
Try FileUpload1.SaveAs(String.Concat("D:\yourpath\attachments\", FileUpload1.FileName))
Catch ex As Exception
   ‘add error handling here
End Try
Dim filename As String
filename = (String.Concat("D:\yourpath\attachments\", FileUpload1.FileName))
Dim myattach As New System.Net.Mail.Attachment(filename)
End If

Create an HTML message

Set the isBodyHTML to true
Then you can use HTML in the message that you assign to the body property

message.IsBodyHtml = True
message.Body = String.Concat("Name:", txtName.Text, "<br>Email:", txtEmail.Text, "<br>Comments:", txtComment.Text)

      ASP.Net 2.0 tutorials & training