Sunday, January 13, 2008

Sending mail in ASP .NET

Sending e-mails from a website is very common nowadays, especially while registering on a website, an automated e-mail will be sent to your mailbox for verification and confirmation purpose.

ASP .NET simplified the work of developers with the introduction of a namespace called System.Web.Mail. By using this namespace, developers can easily sending a plain text mail, HTML mail, or sending attachments.

Sending Plain Text Mail
Let’s start with a simple plain text mail first. See the codes below.

using System.Web.Mail;
.
.
.
string To = "youremailaddress@yahoo.com";
string From = "webmail@mail.com";
string Subject = "Hi";
string Body = "Hello World!";

SmtpMail.Send(From, To, Subject, Body);
The output of this will look like the screenshot below.


This is very simple since it only uses SmtpMail.Send method in the System.Web.Mail namespace. SmtpMail.Send method contains 4 parameters: From, To, Subject and BodyText.

Sending HTML Mail
In sending a HTML mail, we need to use MailMessage class, which provides a lot of useful properties. Here is a list of the properties:

  1. Attachments - Used for sending e-mails with attachments
  2. From - Sender’s email address
  3. To - Recipient’s email address
  4. Cc - Recipient’s email address (Carbon Copy)
  5. Bcc - Recipient’s email address (Blind Carbon Copy)
  6. Body - Text of the email address
  7. BodyFormat - Format of an email (Text or HTML)
  8. Priority - Priority of an email (High, Low, or normal)
  9. Subject - Title of an email
  10. Headers - Denotes a collection of acceptable headers (E.g. Reply-To)
  11. BodyEncoding - Method of encoding an e-mail message (Base64 or UUEncode)
Below is a sample code for sending a simple HTML mail.

using System.Web.Mail;
.
.
.
MailMessage mail = new MailMessage();

mail.To = "youremailaddress@yahoo.com";
mail.Cc = "ccemailaddress@yahoo.com";
mail.From = "webmail@mail.com";
mail.Subject = "Hi, another mail from me";
mail.BodyFormat = MailFormat.Html;

string strBody = "<html><body><b>Hello World</b><br>" +
" <font color=\"red\">A HTML mail</font></body></html>";
mail.Body = strBody;

SmtpMail.Send(mail);
The output of this will look like the screenshot below.


Sending Attachments
As you already seen in the list of MailMessage properties, by sending an attachment, you can use the Attachments property. See the codes below.

using System.Web.Mail;
.
.
.
MailMessage mail = new MailMessage();

mail.To = "youremailaddress@yahoo.com";
mail.From = "webmail@mail.com";
mail.Subject = "Attachment from me";

mail.BodyFormat = MailFormat.Text;
mail.Body = "There is an attachment!";
mail.Attachments.Add(new MailAttachment("c:\\Test Message.doc"));

SmtpMail.Send(mail);
The output of this will look like the screenshot below.

1 comments:

Anonymous said...

emm.. luv this text )

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template