Thursday 10 May 2012

Send an email with ASP.NET using System.Web.Mail

Well, there are many methods for sending an email by ASP.NET, but most common to send is using System.Net.Mail and System.Web.Mail namespace.
System.Web.Mail is just old version of System.Net.Mail.(Well, I will write more about differences between this two libraries in next post.)
Here I am using System.Net.Mail namespace.
I think it’s better to make a class for sending an email then writing code everywhere. So, Everytime when you need to send an email, just call the class function and pass the arguments:-)
Here I made static class, so we not need to create an object for calling function.
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body, string strFileName)
{
// Instantiate a new instance of MailMessage
MailMessage objMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));

// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
if (!string.IsNullOrEmpty(strFileName))
{
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(strFileName);
// Add the file attachment to this e-mail message.
mMailMessage.Attachments.Add(data);
}
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
Please also don’t forget to set credentials under web.config, in order to allow server to send the mail.
Web.config file:

<system.net>
<mailSettings>
<smtp from=”you@yourwebsite.com”>
<network host=”mail.yourwebsite.com” port=”25″ userName=”you@yourwebsite.com” password=”yourpassword” defaultCredentials=”true”/>
</smtp>
</mailSettings>
</system.net>

Other Process

To authenticate with System.Net.Mail is much more intuitive than it was with System.Web.Mail. No longer do you have to set a fields property. Instead, you simply create a NetworkCredential's object, and set the username and password.

static void Authenticate()
{
//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 the body content of the email.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");

//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret"); 
smtp.Send(mail);

}

1 comment:

  1. Do not take it easy.I have got your address from your blog.Then its easy for me to give a case in police station against you.your all article copied from code project or different Blogs.You have five day in your hand delete your blog or delete all content otherwise you have to penalized to jail and your carrier may be stop.Be serious and delete it as soon as possible.Scott Microsoft Team Member

    ReplyDelete