/* * 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 { //- $AssignBcc -// private static void AssignBcc(MailMessage email, String bcc) { if (!String.IsNullOrEmpty(bcc)) { foreach (String address in bcc.Split(",".ToCharArray())) { email.Bcc.Add(address); } } } //- $AssignCc -// private static void AssignCc(MailMessage email, String cc) { if (!String.IsNullOrEmpty(cc)) { foreach (String address in cc.Split(",".ToCharArray())) { email.CC.Add(address); } } } //- $AssignAttachments -// private static void AssignAttachments(MailMessage email, List attachmentPaths) { if (attachmentPaths != null) { foreach (String file in attachmentPaths) { email.Attachments.Add(new Attachment(file)); } } } //- $InternalSend -// private static void InternalSend(String from, String to, String cc, String bcc, String subject, DateTime datetime, String body, List attachmentPaths, Boolean useSecureGmail) { 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(); if (useSecureGmail) { mailClient.Host = "smtp.gmail.com"; mailClient.Port = 587; mailClient.Credentials = new NetworkCredential(StaticConfiguration.SmtpSecureGmailUserName, StaticConfiguration.SmtpSecureGmailPassword); mailClient.EnableSsl = true; } else { 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); } } //- @SendViaSecureGmail -// public static void SendViaSecureGmail(String from, String to, String subject, String body) { InternalSend(from, to, null, null, subject, DateTime.Now, body, null, true); } //- @SendViaSecureGmail -// public static void SendViaSecureGmail(String from, String to, String cc, String bcc, String subject, DateTime datetime, String body, List attachmentPaths) { InternalSend(from, to, cc, bcc, subject, datetime, body, attachmentPaths, true); } //- @Send -// public static void Send(String from, String to, String subject, String body) { if (StaticConfiguration.AlwaysUseSecureGmail) { InternalSend(from, to, null, null, subject, DateTime.Now, body, null, true); } else { InternalSend(from, to, null, null, subject, DateTime.Now, body, null, false); } } //- @Send -// public static void Send(String from, String to, String cc, String bcc, String subject, DateTime datetime, String body, List attachmentPaths) { if (StaticConfiguration.AlwaysUseSecureGmail) { InternalSend(from, to, cc, bcc, subject, datetime, body, attachmentPaths, true); } else { InternalSend(from, to, cc, bcc, subject, datetime, body, attachmentPaths, false); } } //- @WrapAndSend -// 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(""); //+ if (StaticConfiguration.AlwaysUseSecureGmail) { Notification.SendViaSecureGmail(from, to, subject, builder.ToString()); } else { Notification.Send(from, to, subject, builder.ToString()); } } } }