Knowledgebase
Knowledgebase
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
http://www.systemwebmail.com/
http://www.systemnetmail.com/

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 = "me@mycompany.com";

 

      mail.From = "you@yourcompany.com";

 

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

 

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

 

      mail.Headers.Add( "Reply-To", "alternate_email@mycompany.com" );

 

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



[ VB.NET ]

      Dim mail As New MailMessage()

 

      mail.To = "me@mycompany.com"

 

      mail.From = "you@yourcompany.com"

 

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

 

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

 

      mail.Headers.Add("Reply-To", "alternate_email@mycompany.com")

 

      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("me@mycompany.com");

 

      mail.To.Add("you@yourcompany.com");

 

      //set the content

 

      mail.Subject = "This is an email";

 

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

 

      //send the message. Replace mail.domain.co.nz with your smtp server.

 

      SmtpClient smtp = new SmtpClient("mail.domain.co.nz");

 

      // 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;

 

      smtp.Send(mail);


[ VB.NET ]

      'create the mail message

 

      Dim mail As New MailMessage()

 

      'set the addresses

 

      mail.From = New MailAddress("me@mycompany.com")

 

      mail.To.Add("you@yourcompany.com")

 

      'set the content

 

      mail.Subject = "This is an email"

 

      mail.Body = "this is a sample body"

 

      'send the message

 

      Dim smtp As New SmtpClient("127.0.0.1")

 

      smtp.Send(mail)



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

Reference:

      http://www.aspemail.com/manual.html

 

      <%

 

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

 

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

 

      Mail.From = "sales@veryhotcakes.com" ' Specify sender's address

 

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

 

      Mail.AddAddress "andy@andrewscompany.net", "Andrew Johnson, Jr."

 

      Mail.AddAddress "paul@paulscompany.com" ' Name is optional

 

      Mail.AddReplyTo "info@veryhotcakes.com"

 

      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

 

      Mail.Send

 

      If Err <> 0 Then

 

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

 

      End If

 

      %>

 

(348 vote(s))
Helpful
Not helpful