using System; using System.Configuration; using System.Net.Mail; using System.Text; namespace VecernjiList.program.Helpers { public class MailHelper { public static bool SendTo(string fromEmail, string toEmail, string subject, string body, string textVersion) { MailMessage msg = new MailMessage(); msg.From = new MailAddress(fromEmail); msg.To.Add(toEmail); msg.Subject = subject; msg.BodyEncoding = Encoding.GetEncoding("utf-8"); if (textVersion == null) { msg.Body = body; msg.IsBodyHtml = true; } else { AlternateView plainView = AlternateView.CreateAlternateViewFromString (textVersion, null, "text/plain"); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); msg.AlternateViews.Add(plainView); msg.AlternateViews.Add(htmlView); } //default server je (localhost); inače onaj koji je specificiran u AppSettings["MailServer"] string MailServer = ConfigurationManager.AppSettings["MailServer"]; if (String.IsNullOrEmpty(MailServer)) { MailServer = "localhost"; } SmtpClient smtp = new SmtpClient(MailServer); int SMTPPort = Prepare.ForDb_int(ConfigurationManager.AppSettings["MailServerPort"]); if (SMTPPort > 0) { smtp.Port = SMTPPort; } try { smtp.Send(msg); return true; } catch (Exception ex) { return false; } } public static bool SendTo(string fromEmail, string toEmail, string subject, string body, string textVersion, string imagePathAttachment) { MailMessage msg = new MailMessage(); msg.From = new MailAddress(fromEmail); msg.To.Add(toEmail); msg.Subject = subject; msg.BodyEncoding = Encoding.GetEncoding("utf-8"); Attachment MyAttachment = new Attachment(imagePathAttachment); msg.Attachments.Add(MyAttachment); if (textVersion == null) { msg.Body = body; msg.IsBodyHtml = true; } else { AlternateView plainView = AlternateView.CreateAlternateViewFromString (textVersion, null, "text/plain"); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); msg.AlternateViews.Add(plainView); msg.AlternateViews.Add(htmlView); } //default server je (localhost); inače onaj koji je specificiran u AppSettings["MailServer"] string MailServer = ConfigurationManager.AppSettings["MailServer"]; if (String.IsNullOrEmpty(MailServer)) { MailServer = "localhost"; } SmtpClient smtp = new SmtpClient(MailServer); int SMTPPort = Prepare.ForDb_int(ConfigurationManager.AppSettings["MailServerPort"]); if (SMTPPort > 0) { smtp.Port = SMTPPort; } try { smtp.Send(msg); return true; } catch (Exception ex) { return false; } } } }