Sending an email using ASP.NET
Posted by - NA - on 08 June 2006 03:36 AM

Sending mail with ASP.NET

The following information is taken from

Both of these websites contain a wealth of information and are well worth visiting.

.NET 1.1 Framework

What is System.Web.Mail?

      System.Web.Mail (SWM) is the .NET namespace used to send email in .NET Framework applications.


SWM contains three classes:


      MailMessage : Used for creating and manipulating the mail message contents.


      MailAttachments: Used for creating a mail attachment to be added to the mail message.


      SmtpMail: Used for sending email to the relay mail server.

[ C# ]

      MailMessage mail = new MailMessage();


      mail.To = "";


      mail.From = "";


      mail.Subject = "this is a test email.";


      mail.Body = "this is my test email body.";


      mail.Headers.Add( "Reply-To", "" );


      SmtpMail.SmtpServer = "localhost"; SmtpMail.Send( mail );

[ VB.NET ]

      Dim mail As New MailMessage()


      mail.To = ""


      mail.From = ""


      mail.Subject = "this is a test email."


      mail.Body = "this is my test email body."


      mail.Headers.Add("Reply-To", "")


      SmtpMail.SmtpServer = "localhost" SmtpMail.Send(mail)

.NET 2.0 Framework ** Recommended method

What is System.Net.Mail?

      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.

      The following example demonstrates sending a simple plain text email.

[ C# ]

      //create the mail message


      MailMessage mail = new MailMessage();


      //set the addresses


      mail.From = new MailAddress("");




      //set the content


      mail.Subject = "This is an email";


      mail.Body = "this is a sample body";


      //send the message. Replace with your smtp server.


      SmtpClient smtp = new SmtpClient("");


      // Using authentication to connect to above smtp server to send email.


      // Replace txtSMTPUser and txtSMTPPass with your appropriate details.


      System.Net.NetworkCredential SMTPUserInfo = new


      System.Net.NetworkCredential(txtSMTPUser, txtSMTPPass);


      smtp.UseDefaultCredentials = false;


      smtp.Credentials = SMTPUserInfo;



[ VB.NET ]

      'create the mail message


      Dim mail As New MailMessage()


      'set the addresses


      mail.From = New MailAddress("")




      'set the content


      mail.Subject = "This is an email"


      mail.Body = "this is a sample body"


      'send the message


      Dim smtp As New SmtpClient("")



ASPEMAIL sample code ** Not recommended as it is not using a dedicated SMTP server.





      Set Mail = Server.CreateObject("Persits.MailSender")


      Mail.Host = "" ' Specify a valid SMTP server examples: or localhost if configured properly.


      Mail.From = "" ' Specify sender's address


      Mail.FromName = "VeryHotCakes Sales" ' Specify sender's name


      Mail.AddAddress "", "Andrew Johnson, Jr."


      Mail.AddAddress "" ' Name is optional


      Mail.AddReplyTo ""


      Mail.AddAttachment "c:\images\cakes.gif"


      Mail.Subject = "Thanks for ordering our hot cakes!"


      Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _


      "Thank you for your business."


      On Error Resume Next




      If Err <> 0 Then


      Response.Write "Error encountered: " & Err.Description


      End If




(349 vote(s))
Not helpful