/// <summary> /// Creates the aspNET EmailMessage object based on mail information /// we've alread got. Construct the message from html, and then spesify /// that the images should be attached inline. /// </summary> /// <param name="mailInfo">Mail info.</param> /// <returns></returns> private EmailMessage CreateMessage(MailInformation mailInfo) { if (_log.IsDebugEnabled()) { _log.Debug("Begin CreateMessage. Base Url: {0}", mailInfo.BaseUrl); } EmailMessage email = new EmailMessage(); HtmlUtility utility = new HtmlUtility(email); // Swallow exceptions if configured if (Configuration.NewsLetterConfiguration.DisableExceptionsInMailRendering == true) { email.ThrowException = false; } // Troubleshooting needs logging if (Configuration.NewsLetterConfiguration.ExtendedLogFile != null) { // Be careful with logging, it will generate large amounts of log data email.LogInMemory = false; email.LogPath = EPiServer.Global.BaseDirectory + Configuration.NewsLetterConfiguration.ExtendedLogFile; email.LogDebugStatements = true; email.LogBody = true; email.Logging = true; } // set all relative links contained in the html to this url string baseUrl = SiteDefinition.Current.SiteUrl.ToString(); if (string.IsNullOrEmpty(mailInfo.BaseUrl) == false) { baseUrl = mailInfo.BaseUrl; } // Resolves Hrefs to their absolute value, this means // no urls in the html will be relative (which can be a pain // depending on the markup utility.ResolveHrefs = true; // Clean html, remove tags and content we do not want or need utility.HtmlRemovalOptions = HtmlRemovalOptions.AppletTag | HtmlRemovalOptions.EmbedTag | HtmlRemovalOptions.NoScriptTag | HtmlRemovalOptions.ObjectTag | HtmlRemovalOptions.ParamTag | HtmlRemovalOptions.Scripts | HtmlRemovalOptions.ViewState | HtmlRemovalOptions.EventArgument | HtmlRemovalOptions.EventTarget; // Load the html mail utility.LoadString(mailInfo.BodyHtml, baseUrl); // Set the UrlContent base utility.SetUrlContentBase = false; // Set the basetag in the html utility.SetHtmlBaseTag = true; // Embed the images into the email message, using the // content id as a src reference. Change this if you do // not want images to be part of the message utility.EmbedImageOption = EmbedImageOption.ContentId; // If you have problems loading images, disable exceptions // if (utility.ParentMessage != null) // utility.ParentMessage.ThrowException = false; //render the Html so it is properly formatted for email utility.Render(); //render an EmailMessage with appropriate text and html parts email = utility.ToEmailMessage(); //load remaining properties from web.config email.LoadFromConfig(); // Try to get these encodings correct email.ContentTransferEncoding = MailEncoding.QuotedPrintable; // Using utf-8 would have been nice, but utf-8 is not widely // adopted by email clients it seems. // email.CharSet = "utf-8"; // Using iso 8859-1 hotmail will show Norwegian characters // even if the user runs with english settings // Hotmail needs this email.CharSet = "iso-8859-1"; // Override with our own values email.Subject = mailInfo.Subject; email.From = mailInfo.From; email.TextBodyPart = mailInfo.BodyText; if (_log.IsDebugEnabled()) { _log.Debug("Rendered Mail Message: {0}", email.Subject); } return(email); }
/// <summary> /// Creates the aspNET EmailMessage object based on mail information /// we've alread got. Construct the message from html, and then spesify /// that the images should be attached inline. /// </summary> /// <param name="mailInfo">Mail info.</param> /// <returns></returns> private EmailMessage CreateMessage(MailInformation mailInfo) { if (_log.IsDebugEnabled()) _log.Debug("Begin CreateMessage. Base Url: {0}", mailInfo.BaseUrl); EmailMessage email = new EmailMessage(); HtmlUtility utility = new HtmlUtility(email); // Swallow exceptions if configured if (Configuration.NewsLetterConfiguration.DisableExceptionsInMailRendering == true) email.ThrowException = false; // Troubleshooting needs logging if (Configuration.NewsLetterConfiguration.ExtendedLogFile != null) { // Be careful with logging, it will generate large amounts of log data email.LogInMemory = false; email.LogPath = EPiServer.Global.BaseDirectory + Configuration.NewsLetterConfiguration.ExtendedLogFile; email.LogDebugStatements = true; email.LogBody = true; email.Logging = true; } // set all relative links contained in the html to this url string baseUrl = SiteDefinition.Current.SiteUrl.ToString(); if (string.IsNullOrEmpty(mailInfo.BaseUrl) == false) baseUrl = mailInfo.BaseUrl; // Resolves Hrefs to their absolute value, this means // no urls in the html will be relative (which can be a pain // depending on the markup utility.ResolveHrefs = true; // Clean html, remove tags and content we do not want or need utility.HtmlRemovalOptions = HtmlRemovalOptions.AppletTag | HtmlRemovalOptions.EmbedTag | HtmlRemovalOptions.NoScriptTag | HtmlRemovalOptions.ObjectTag | HtmlRemovalOptions.ParamTag | HtmlRemovalOptions.Scripts | HtmlRemovalOptions.ViewState | HtmlRemovalOptions.EventArgument | HtmlRemovalOptions.EventTarget; // Load the html mail utility.LoadString(mailInfo.BodyHtml, baseUrl); // Set the UrlContent base utility.SetUrlContentBase = false; // Set the basetag in the html utility.SetHtmlBaseTag = true; // Embed the images into the email message, using the // content id as a src reference. Change this if you do // not want images to be part of the message utility.EmbedImageOption = EmbedImageOption.ContentId; // If you have problems loading images, disable exceptions // if (utility.ParentMessage != null) // utility.ParentMessage.ThrowException = false; //render the Html so it is properly formatted for email utility.Render(); //render an EmailMessage with appropriate text and html parts email = utility.ToEmailMessage(); //load remaining properties from web.config email.LoadFromConfig(); // Try to get these encodings correct email.ContentTransferEncoding = MailEncoding.QuotedPrintable; // Using utf-8 would have been nice, but utf-8 is not widely // adopted by email clients it seems. // email.CharSet = "utf-8"; // Using iso 8859-1 hotmail will show Norwegian characters // even if the user runs with english settings // Hotmail needs this email.CharSet = "iso-8859-1"; // Override with our own values email.Subject = mailInfo.Subject; email.From = mailInfo.From; email.TextBodyPart = mailInfo.BodyText; if (_log.IsDebugEnabled()) _log.Debug("Rendered Mail Message: {0}", email.Subject); return email; }