Email in ASP.Net 2.0

How Email Works
Email server sends and retrieves your email message
Mostly 2 protocols used
- SMTP
- Simple Mail Transfer Protocol
- Send EMail
- POP
- Post Office Protocol
- 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
- MailMessage
- The email message
- SMTPClient
- Sends mail message
- MailAttachment
- An email attachment
- MailAddress
- Email address used in
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
Constructors
MailMessage()
MailMessage(from,to)
MailMessage(from,to,subject,body)
Properties
- 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
Example:
Dim message As New System.Net.Mail.MailMessage("jim@starkstate.edu ", " jo@prowebdesigners.com ")
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("jo@prowebdesigners.com")
‘To must be set with the to.add as shown
message.To.Add(New System.Net.Mail.MailAddress("jim@starkstate.edu"))
message.Subject = "Comments from ProwebDesigners.com"
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
Constructors
SMTPClient() when setting up in web.config
SMTPClient(name) name of smtp server
SMTPClient(name,port)
Methods
Send(message) sends mailmessage object
Send(from, to, subject, body)
Ex:
Dim client as new SMTPClient()
Client.Send(message)
Dim client as new smtpClient(“emailservername”)
Client.send(“jo@prowebdesigners.com”, txtTo.text, txtSubject.text,txtBody.text)
Example:
Dim client as new System.Net.Mail.smtpClient()
Client.send(message)

Set up SMTP server setting in the web.config
You must specify the hostname and optionally the port which is normally 25
<system.net>
<mailSettings>
<smtp>
<network host="mail.yoursite.com"
userName="email@ yoursite.com" password="password" />
</smtp>
</mailSettings>
</system.net>

MailAddress
Sending messages to more than one email address:
Create a MailAddress object for each To property
Ex:
Dim message as New MailMessage()
Message.from=new MailAddress(“from@prowebdesigners.com”)
Message.to.Add(“Address2@prowebdesigners.com”)
Message.to.Add(“Address1@prowebdesigners.com”)
Constructors of MailAddress
MailMessage(address)
MailAddress(address,displayName)
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)
Message.Subject=txtSubject.text
Message.body=txtBody.text
Message.cc.add(ccAddress)
Alternative Method
Dim message as New MailMessage(txtfrom.text, txtTo.text, txtSubject.text,txtBody.text)
Message.cc.add(new 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
Ex:
New attachment(filename)
Ex:
Dim myfilename as string
Myfilename=”C:\myfiles\Document.doc”
Dim myattach as new attachment(filename)
Dim message as new mailMessage(txtfrom.text, txtTo.text, txtSubject.text,txtBody.text)
Message.attachments.add(myattach)
Alternative method
Dim myfilename as string
Myfilename=”C:\myfiles\Document.doc”
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)
message.Attachments.Add(myattach)
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)
