public static void SendEmailbytemplateEx(SPListItem item, SPListItem taskItem, List<NameValue> xvariables, SPListItem itemTemplate, string to, string cc, string variables) { List<EmailVariable> designerVariables = null; List<EmailVariable> bodyVariables = null; List<EmailVariable> subjectVariables = null; string subject = string.Empty; bool sendAsPlainMode = false; string body = EnsureAbsoluteUrl(item.Web.Site, itemTemplate["Body"].ToString()); //string body = itemTemplate["Body"].ToString(); if (itemTemplate["Subject"] != null) subject = itemTemplate["Subject"].ToString(); bodyVariables = Parser.GetAndSetVariablesFromTemplate(body, item, taskItem); subjectVariables = Parser.GetAndSetVariablesFromTemplate(subject, item, taskItem); designerVariables = Parser.GetVariablesFromDesigner(variables); subject = Parser.SetValuesToContent(subject, subjectVariables, designerVariables); body = Parser.SetValuesToContent(body, bodyVariables, designerVariables); subject = ParseContentWithVariables(subject, xvariables); body = ParseContentWithVariables(body, xvariables); if (itemTemplate.VerifyFieldAccess("SendAsPlainText")) { sendAsPlainMode = Convert.ToBoolean(itemTemplate["SendAsPlainText"].ToString()); if (sendAsPlainMode) body = body.ToPlainText(); } SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(item.Web.Site.ID)) { using (SPWeb web = site.OpenWeb(item.Web.ID)) { StringDictionary headers = new StringDictionary(); headers = buidEmailHeaders(taskItem, to, cc, subject, sendAsPlainMode); SPUtility.SendEmail(web, headers, body); } } } ); }
public static void SendEmailByTemplateWithAttachments(SPListItem item, MailAddress fromAddress, string to, string reply, string cc, SPListItem itemTemplate, List<Attachment> attachments, SPUser currentUser) { List<EmailVariable> bodyVariables = null; List<EmailVariable> subjectVariables = null; List<EmailVariable> designerVariables = new List<EmailVariable>(); StringDictionary header; string subject = string.Empty; bool sendAsPlainMode = false; //string body = itemTemplate["Body"].ToString(); string body = EnsureAbsoluteUrl(item.Web.Site, itemTemplate["Body"].ToString()); if (itemTemplate["Subject"] != null) subject = itemTemplate["Subject"].ToString(); bodyVariables = Parser.GetAndSetVariablesFromTemplate(body, item, null, null); subjectVariables = Parser.GetAndSetVariablesFromTemplate(subject, item, null, null); subject = Parser.SetValuesToContent(subject, subjectVariables, designerVariables); body = Parser.SetValuesToContent(body, bodyVariables, designerVariables); if (itemTemplate.VerifyFieldAccess("SendAsPlainText")) { sendAsPlainMode = Convert.ToBoolean(itemTemplate[new Guid("1BCADBD1-CC15-40A0-AAF7-6DE222412337")].ToString()); if (sendAsPlainMode) body = body.ToPlainText(); } MailMessage message = new MailMessage { Subject = subject, Body = body, IsBodyHtml = !sendAsPlainMode, }; message.From = fromAddress; // To mail string[] arrTo = to.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string t in arrTo) message.To.Add(t); // CC mail if (!String.IsNullOrEmpty(cc)) { string[] arrCC = cc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string c in arrCC) message.CC.Add(c); } // Reply and body if (!string.IsNullOrEmpty(reply)) { message.ReplyTo = new MailAddress(reply); } else { if (currentUser != null && !string.IsNullOrEmpty(currentUser.Email)) message.ReplyTo = new MailAddress(currentUser.Email); else message.Body += "<br><br><i> System generated email, please do not reply to this message</i>"; } foreach (Attachment a in attachments) message.Attachments.Add(a); // Send message //string server = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address; //SmtpClient client = new SmtpClient(server); SmtpClient client = getSmtpInformation(); client.Send(message); }
public static void SendEmailbytemplate(SPWeb web, SPListItem taskItem, SPListItem itemTemplate, string to, string cc, string variables, bool attachTaskLink) { string subject = string.Empty; //string body = itemTemplate["Body"].ToString(); string body = EnsureAbsoluteUrl(web.Site, itemTemplate["Body"].ToString()); bool sendAsPlainMode = false; if (itemTemplate.VerifyFieldAccess("SendAsPlainText")) //Fields.ContainsFieldWithStaticName("SendAsPlainText") && itemTemplate["SendAsPlainText"] != null) { sendAsPlainMode = Convert.ToBoolean(itemTemplate["SendAsPlainText"].ToString()); } if (itemTemplate["Subject"] != null) subject = itemTemplate["Subject"].ToString(); SendEmailbytemplate(web, taskItem, to, cc, variables, subject, body, sendAsPlainMode); }