protected override void Execute(CodeActivityContext context) { String TO, CC, BCC, SUBJECT, BODY; if (TemplatePath.Get(context).Contains(".eml")) { Message emlMessage = ReadMessage(TemplatePath.Get(context).Contains(":\\") ? TemplatePath.Get(context) : System.IO.Directory.GetCurrentDirectory() + '\\' + TemplatePath.Get(context)); TO = emlMessage.To; CC = emlMessage.CC; BCC = emlMessage.BCC; SUBJECT = emlMessage.Subject; BODY = String.IsNullOrEmpty(emlMessage.HTMLBody)?emlMessage.TextBody:emlMessage.HTMLBody; } else if (TemplatePath.Get(context).Contains(".oft") || TemplatePath.Get(context).Contains(".msg")) { try { Outlook.Application app = new Outlook.Application(); Outlook.MailItem template = app.CreateItemFromTemplate(TemplatePath.Get(context).Contains(":\\") ? TemplatePath.Get(context) : System.IO.Directory.GetCurrentDirectory() + '\\' + TemplatePath.Get(context)) as Outlook.MailItem; TO = template.To; CC = template.CC; BCC = template.BCC; SUBJECT = template.Subject; BODY = String.IsNullOrEmpty(template.HTMLBody) ? template.Body : template.HTMLBody; } catch { throw new System.Exception("Error, .oft and .msg files can only be read if you have Outlook installed, try using a .eml as a template"); } } else { throw new System.Exception("Invalid template format, please use a '.oft', '.msg', or '.eml' template."); } SUBJECT = String.IsNullOrEmpty(Subject.Get(context)) ? (String.IsNullOrEmpty(SUBJECT) ? "Untitled" : SUBJECT) : Subject.Get(context); TO = String.IsNullOrEmpty(To.Get(context)) ? TO : To.Get(context); CC = String.IsNullOrEmpty(Cc.Get(context)) ? CC : Cc.Get(context); BCC = String.IsNullOrEmpty(Bcc.Get(context)) ? BCC : Bcc.Get(context); BODY = BODY.Replace("{message}", String.IsNullOrEmpty(Body.Get(context))?"":Body.Get(context)); BODY = BODY.Replace("{table}", DT.Get(context) != null ? (DT.Get(context).Rows.Count > 0 ? GetHTMLTable(DT.Get(context)) : "") : ""); MailMessage mail = new MailMessage(From.Get(context), TO, SUBJECT, BODY); mail.Bcc.Add(BCC); mail.IsBodyHtml = true; mail.CC.Add(CC); SmtpClient client = new SmtpClient { Port = Port.Get(context), DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, EnableSsl = EnableSSL, Host = Server.Get(context), Credentials = new System.Net.NetworkCredential(Email.Get(context), Password.Get(context)), Timeout = 30000 }; client.Send(mail); }
public Application(string templatePath, string subject, string to, string cc, string bcc, string body, string from, int port, DataTable dt, bool ssl, string host, string email, string password, List <string> inputs) { EMAIL = email; PASSWORD = password; HOST = host; SSL = ssl; Port = port; FROM = from; Inputs = inputs; if (templatePath.Contains(".eml")) { Message emlMessage = ReadMessage(templatePath.Contains(":\\") ? templatePath : System.IO.Directory.GetCurrentDirectory() + '\\' + templatePath); TO = emlMessage.To; CC = emlMessage.CC; BCC = emlMessage.BCC; SUBJECT = emlMessage.Subject; BODY = String.IsNullOrEmpty(emlMessage.HTMLBody) ? emlMessage.TextBody : emlMessage.HTMLBody; } else if (templatePath.Contains(".oft") || templatePath.Contains(".msg")) { try { Outlook.Application app = new Outlook.Application(); Outlook.MailItem template = app.CreateItemFromTemplate(templatePath.Contains(":\\") ? templatePath : System.IO.Directory.GetCurrentDirectory() + '\\' + templatePath) as Outlook.MailItem; TO = template.To; CC = template.CC; BCC = template.BCC; SUBJECT = template.Subject; BODY = String.IsNullOrEmpty(template.HTMLBody) ? template.Body : template.HTMLBody; } catch (IOException) { throw; } catch { throw new System.Exception("Error, .oft and .msg files can only be read if you have Outlook installed, try using a .eml as a template"); } } else { throw new System.Exception("Invalid template format, please use a '.oft', '.msg', or '.eml' template."); } SUBJECT = String.IsNullOrEmpty(subject) ? (String.IsNullOrEmpty(SUBJECT) ? "Untitled" : SUBJECT) : subject; TO = String.IsNullOrEmpty(to) ? TO : to; CC = String.IsNullOrEmpty(cc)? CC : cc; BCC = String.IsNullOrEmpty(bcc) ? BCC :bcc; BODY = BODY.Replace("{message}", String.IsNullOrEmpty(body) ? "" : body); BODY = BODY.Replace("{table}", dt != null ? (dt.Rows.Count > 0 ? GetHTMLTable(dt) : "") : ""); }