Quantcast
Channel: C# – Random Technical Thoughts
Viewing all articles
Browse latest Browse all 17

Generic Send mail method

$
0
0

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”]


Viewing all articles
Browse latest Browse all 17

Trending Articles