/* * This work is licensed under the Creative Commons Attribution 2.5 License. * To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, * San Francisco, California, 94105, USA. * * Original developer: David Betz * */ using System; using System.Text; using System.Net; using System.Net.Mail; using System.Collections.Generic; using System.Net.Mime; using System.IO; namespace General.Mail { public static class Notification { public static void WrapAndSend(String from, String to, String subject, String body) { StringBuilder builder = new StringBuilder( ); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine("" + StaticConfiguration.Subject + " Notification"); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(""); builder.AppendLine(body); builder.AppendLine(""); builder.AppendLine(""); Notification.Send(from, to, subject, builder.ToString( )); } public static void Send(String from, String to, String subject, String body) { Send(from, to, null, null, subject, DateTime.Now, body, null); } public static void Send(String from, String to, String cc, String bcc, String subject, DateTime datetime, String body, List attachmentPaths) { using (MailMessage email = new MailMessage(from, to)) { email.Subject = subject; email.Body = body; email.IsBodyHtml = true; AssignCc(email, cc); AssignBcc(email, bcc); AssignAttachments(email, attachmentPaths); SmtpClient mailClient = new SmtpClient( ); String userName = StaticConfiguration.SmtpUserName; String password = StaticConfiguration.SmtpPassword; String server = StaticConfiguration.SmtpServer; NetworkCredential basicAuthenticationInfo = new NetworkCredential(userName, password); mailClient.Host = server; mailClient.UseDefaultCredentials = false; mailClient.Credentials = basicAuthenticationInfo; mailClient.Send(email); } } private static void AssignBcc(MailMessage email, String bcc) { if (!String.IsNullOrEmpty(bcc)) { foreach (String address in bcc.Split(",".ToCharArray( ))) { email.Bcc.Add(address); } } } private static void AssignCc(MailMessage email, String cc) { if (!String.IsNullOrEmpty(cc)) { foreach (String address in cc.Split(",".ToCharArray( ))) { email.CC.Add(address); } } } private static void AssignAttachments(MailMessage email, List attachmentPaths) { if (attachmentPaths != null) { foreach (String file in attachmentPaths) { email.Attachments.Add(new Attachment(file)); } } } } }