Google Search

Google
 

Saturday, December 8, 2007

Sending Email with ASP.NET 2.0

In ASP.Net 2.0 use System.Net.Mail class.

System.Net.Mail is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework.

Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop. Thus, it is not dependant upon other COM libraries. System.Net.Mail introduces brand new classes for creating and sending email.

Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.

Dim message As MailMessage = New MailMessage()
message.From = New MailAddress("from@server.com", "From Name")

message.ReplyTo = New MailAddress("reply@server.com", "ReplyTo Name")

message.To.Add(New MailAddress("to1@server.com", "Name One"))
message.To.Add(New MailAddress("to2@server.com", "Name Two"))
message.CC.Add(New MailAddress("cc@server.com", "CC Name"))
message.Bcc.Add(New MailAddress("bcc@server.com", "BCC Name"))

message.Subject = "Subject"
message.Body = "This is a plain text"
message.IsBodyHtml = False

'Priority property can have next values: Low, Normal and High.
message.Priority = MailPriority.Normal

Dim emailClient As New SmtpClient("mail.server.com")
emailClient.Send(message)