Generic Send mail method

Here is a generic send mail method.

You just need to pass in a subject and body.
In your web.config you need a key for the from (notification_from), To (notification_to) and mail server (mail_server).
You could easily alter it to move the to (or from) to input variables.

private static void sendNotification(String Subject, String Body)
        {
            MailMessage mailMessage = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            string msg = string.Empty;

            String from = ConfigurationManager.AppSettings["notification_from"];
            String to = ConfigurationManager.AppSettings["notification_to"];
            String mailserver = ConfigurationManager.AppSettings["mail_server"];

            MailAddress fromAddress = new MailAddress(from);
            mailMessage.From = fromAddress;
            mailMessage.To.Add(to);
            mailMessage.Subject = Subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = Body;

            smtpClient.Host = mailserver;

            smtpClient.EnableSsl = false;
            smtpClient.UseDefaultCredentials = true;

            smtpClient.Send(mailMessage);
        }
[tweetmeme only_single=”false”]
Advertisements

One thought on “Generic Send mail method

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s