home » blogs » vivek_iit » How To: Send Email in ASP.NET 2.0
posted 3/12/2009 by vivek_iit
Yet another email sending code :-): I have divided it into 3 parts. First is the main email sending wrapper class (EmailNotification.cs), then is the web.config settings needed, and the last one is the client code to call the EmailNotification class.
Here is the first part:
/// <summary>/// This class is responsible to send email/// notifications to a list of adderesses/// specified in the web.config file, along with/// the server information./// </summary> public class EmailNotification{#region Private Members private SmtpClient _smtpClient; private MailMessage _message; private string _body; private string _subject; private List<string> _toList; private List<string> _ccList; private List<string> _bccList; #endregion #region Public Properties public List<string> BccList { get { return _bccList; } set { _bccList = value; } } public List<string> CcList { get { return _ccList; } set { _ccList = value; } } public string Subject { get { return _subject; } set { _subject = value; } } public string Body{ get { return _body; }set { _body = value; }} public List<string> ToList{get { return _toList; }set { _toList = value; }}#endregion #region Public Methods/// <summary>/// Default constructor/// </summary>public EmailNotification(){ _smtpClient = new SmtpClient();_message = new MailMessage();} /// <summary>/// Sends email to the recipient. Make sure/// that To, CC (optional) and BCC(optional) list /// is set before calling this method./// </summary> public void SendEmail(){ try{ foreach(string toMailAddress in _toList) { _message.To.Add(toMailAddress); } if (_ccList != null) { foreach (string ccMailAddress in _ccList){ _message.CC.Add(ccMailAddress); }} if (_bccList != null){ foreach (string bccMailAddress in _bccList){ _message.Bcc.Add(bccMailAddress);}} _message.Body = _body;_message.Subject = _subject;_message.IsBodyHtml = true;_smtpClient.Send(_message); } catch (Exception ex) { //todo: exception logging as per your needs }}#endregion}//end class
/// <summary>/// This class is responsible to send email/// notifications to a list of adderesses/// specified in the web.config file, along with/// the server information./// </summary>
public class EmailNotification{#region Private Members
private SmtpClient _smtpClient;
private MailMessage _message;
private string _body;
private string _subject;
private List<string> _toList;
private List<string> _ccList;
private List<string> _bccList;
#endregion
#region Public Properties
public List<string> BccList
{
get { return _bccList; }
set { _bccList = value; }
}
public List<string> CcList
get { return _ccList; }
set { _ccList = value; }
public string Subject
get { return _subject; }
set { _subject = value; }
public string Body{ get { return _body; }set { _body = value; }}
public List<string> ToList{get { return _toList; }set { _toList = value; }}#endregion
#region Public Methods/// <summary>/// Default constructor/// </summary>public EmailNotification(){ _smtpClient = new SmtpClient();_message = new MailMessage();}
/// <summary>/// Sends email to the recipient. Make sure/// that To, CC (optional) and BCC(optional) list /// is set before calling this method./// </summary>
{ try{ foreach(string toMailAddress in _toList) { _message.To.Add(toMailAddress); }
if (_ccList != null)
foreach (string ccMailAddress in _ccList){ _message.CC.Add(ccMailAddress); }}
if (_bccList != null){ foreach (string bccMailAddress in _bccList){ _message.Bcc.Add(bccMailAddress);}}
_message.Body = _body;_message.Subject = _subject;_message.IsBodyHtml = true;_smtpClient.Send(_message);
catch (Exception ex) { //todo: exception logging as per your needs }}#endregion}//end class
Now the web.config settings (should go under the main <configuration>)tag:
<system.net> <mailSettings> <smtp from="xxxxxx"> <network host="mail.xxxxx.com" port="25" userName="xxxx@mail.com" password="xxxxxx"/> </smtp> </mailSettings></system.net>
Replace XXX by your mail server settings.
Now the last part, the client code:
/* *Send email */EmailNotification em = new EmailNotification();em.Subject = "Subject goes here";em.Body = "Body goes here ";System.Collections.Generic.List<string> to = new System.Collections.Generic.List<string>();to.Add(someone@mail.com); //similarly add CC and BCC recipientsem.ToList = to;em.SendEmail();
The code is pretty self explanatory and in case someone finds any issues/bugs do let me know!
vivek_iit (Member since: 11/27/2008) I am one of the administrators at CodeAsp.Net and I love programming, architecting solutions, code reviews, teaching and writing about ASP.NET.
View vivek_iit 's profile
Name: *
Email: (your email is kept secure) *
Website:
Comment (No HTML)
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: