public static bool sendEmail(emailMessage e) { bool sent = false; try { new SmtpClient { Host = "Smtp.Gmail.com", Port = 587, EnableSsl = true, Timeout = 10000, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(e.getFrom(), e.getPassword()) }.Send(new MailMessage { IsBodyHtml = true, From = new MailAddress(e.getFrom(), e.getFromName()), To = { e.to }, Subject = e.subject, Body = e.message, BodyEncoding = Encoding.UTF8 }); sent = true; } catch (Exception ex) { globals.logdata(ex + ""); } return sent; }
public static string sendEmail(emailMessage msg) { if (logArchitect.IsDebugEnabled) { logArchitect.DebugFormat("SMTP Library - attempting to send message with subject {0}", msg.Subject); } //Smtp client properties SmtpClient client = new SmtpClient(); client.Host = msg.ServerDetails; client.Port = msg.Port; client.EnableSsl = msg.ESSL; client.Timeout = msg.sendTimeOut; client.DeliveryMethod = SmtpDeliveryMethod.Network; if (msg.UseDefaultCredentials) { if (logArchitect.IsDebugEnabled) { logArchitect.DebugFormat("SMTP Library - Using deafult credentials"); } client.UseDefaultCredentials = true; } else { client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(msg.UserName, msg.Password); } //Email Message Properties using (MailMessage mm = new MailMessage()) { mm.BodyEncoding = System.Text.UTF8Encoding.UTF8; mm.Sender = new System.Net.Mail.MailAddress(msg.Sender); mm.Body = msg.Body; mm.From = new System.Net.Mail.MailAddress(msg.Sender); mm.IsBodyHtml = msg.isHtml; mm.Subject = msg.Subject; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; //Adding Recipients and CC list MailAddressCollection tos = new MailAddressCollection(); foreach (string addr in msg.Recipients) { if (logArchitect.IsDebugEnabled) { logArchitect.DebugFormat("SMTP Library - attempting to add address {0}", addr); } mm.To.Add(addr); logArchitect.DebugFormat("SMTP Library - attempting to add address {0}", addr); } MailAddressCollection tosCC = new MailAddressCollection(); foreach (string addr in msg.RecipientsCC) { if (logArchitect.IsDebugEnabled) { logArchitect.DebugFormat("SMTP Library - attempting to add addressCC {0}", addr); } mm.CC.Add(addr); logArchitect.DebugFormat("SMTP Library - attempting to add addressCC {0}", addr); } //Adding attachemets foreach (string att in msg.Attachements) { if (logArchitect.IsDebugEnabled) { logArchitect.DebugFormat("SMTP Library - attempting to add attachement {0}", att); } mm.Attachments.Add(new Attachment(att)); logArchitect.DebugFormat("SMTP Library - attempting to add attachments {0}", att); } //Sending the Email Message and returning errors or success result try { client.Send(mm); client.Dispose(); } catch (Exception e) { logArchitect.Error("SMTP Library - Send SMTP email error. ", e); client.Dispose(); return(e.ToString()); } return("Message Sent Successfully"); } }