Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  102 Posts | 1 Stories | 317 Comments | 66 Trackbacks

News



Archives

ASP.NET Ventures

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

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 recipients
em.ToList = to;
em.SendEmail();

The code is pretty self explanatory and in case someone finds any issues/bugs do let me know!

 
posted on Monday, April 09, 2007 12:11 PM

Feedback

# Links (4/9/2007) 4/25/2007 9:40 PM Member Blogs
.NET All About Iterators Why all the love for lists? Defining bit field enums InternalsVisibleTo: Testing

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 4 and type the answer here: