[英]Cannot seem to add a custom text alternative view to my multipart email
我正在編寫一個向用戶發送大量電子郵件的系統,並且對於每個站點,我們都存儲電子郵件的HTML和文本版本,這樣,如果用戶的郵件客戶端不支持HTML,則仍然可以按照我們的要求正確設置文本視圖的格式。
我們不希望僅從HTML版本生成純文本版本,因為它會添加許多菜單鏈接和其他未按我們要求格式化的文本。
在ASP經典中,使用Persits Email Component = http://www.aspemail.com/可以正常工作,因為我們將HTML字符串添加為Body,將文本字符串添加為AltBody。
但是我在C#.NET 4.5中復制此文件時遇到問題
我已經遵循了盡可能多的示例,但是返回MailMessage對象(該方法需要傳遞HTML來查找圖像/橫幅,然后將URLS替換為ContentIDs和LinkedResources)的方法在某種程度上返回了在HTML和Simple HTML View中看起來不錯的電子郵件(在Thunderbird中)。
但是無論我做純文本的觀點似乎永遠是對象試圖RATHER轉換成文本比文本字符串我們已經預先格式化,並希望使用HTML的版本。
如果我調試代碼,在將字符串添加到備用視圖之前,我可以看到字符串是正確的,因此我不知道還需要做什么。
在解析HTML,添加鏈接的資源並返回MailMessage對象的方法中,我具有以下代碼:
<pre>
/* I pass in a custom SiteEmail object with 2 properties HTMLEmail and TextEmail that hold both versions of the email */
public MailMessage ParseEmailImages(SiteEmail siteEmail)
{
MailMessage email = new MailMessage();
// extract the HTML version as we need to parse it to swap URLs for ContentID/Resources and paths etc
string HTML = siteEmail.HTMLEmail;
// this is a generic list to hold all my picture resource objects that I find (swapping image URLs to paths and contentIDs)
List<LinkedResource> pictureResources = new List<LinkedResource>();
// code to find all the paths, create image resource objects and add them to my list - and modify the HTML to reference
// the new ContentIDs I swap the URLs for so the images are embedded in the email
// ..... code .....
// finished finding resource objects build email parts and return to caller
// Add each alternate view to the message.
// add the HTML view using my newly parsed HTML string
ContentType HtmlContentType = new ContentType("text/html; charset=UTF-8");
AlternateView altViewHTML = AlternateView.CreateAlternateViewFromString(HTML, HtmlContentType);
altViewHTML.TransferEncoding = TransferEncoding.QuotedPrintable;
// when I check here the siteEmail.TextEmail holds the CORRECT textual string I want BUT it's not displaying in the sent email
ContentType PlainContentType = new ContentType("text/plain; charset=UTF-8");
// as we didn't need to change anything in the text view we can just reference it straight out my custom email object siteEmail
AlternateView altViewText = AlternateView.CreateAlternateViewFromString(siteEmail.TextEmail, PlainContentType);
altViewText.TransferEncoding = TransferEncoding.QuotedPrintable;
// loop through all my picture resource objects and ensure they are embedded into the HTML email
foreach (LinkedResource pictureResource in pictureResources)
{
pictureResource.TransferEncoding = TransferEncoding.Base64;
altViewHTML.LinkedResources.Add(pictureResource);
}
// add both parts of the email (text/html) which are both alternative views to message
email.AlternateViews.Add(altViewText);
email.AlternateViews.Add(altViewHTML);
// return email object
return email;
}
// a very cut down example of the calling method
public bool SendEmail()
{
// parse our email object
MailMessage EmailMailMessage = this.ParseEmailImages(this.SiteEmail);
// send email
EmailMailMessage.From = new MailAddress(this.SendFromEmail, this.SendFromName);
EmailMailMessage.Subject = this.SendSubject;
// ensure encoding is correct for Arabic/Japanese sites and body transfer method is correct
EmailMailMessage.BodyEncoding = Encoding.UTF8;
EmailMailMessage.BodyTransferEncoding = TransferEncoding.QuotedPrintable;
SmtpClient client = new SmtpClient();
// this in a try catch and more complex
client.Send(this.EmailMailMessage);
}
</pre>
我嘗試使用編碼格式,只是添加了一個替代視圖,依此類推,但似乎無法發送與我的舊ASP Classic代碼相同的電子郵件,例如包含2個邊界的多部分電子郵件,其中1個用於文本版本WE想要使用,並且帶有HTML版本。 它似乎總是從HTML版本創建它自己的純文本版本,我不想發生這種情況。
任何幫助或想法將不勝感激。
在此先感謝您的幫助!
答案似乎是MailMessage類中存在錯誤。 我被告知要在Microsoft .NET論壇上進行此類報告。
看來問題出在LinkedResources的使用上。
如果刪除將LinkedResources添加到HTML備用視圖的代碼,則該代碼可以正常工作,並且可以在郵件客戶端中同時看到“純文本”視圖和HTML視圖。
因此,當用戶按下其電子郵件客戶端中的任何“加載遠程內容”按鈕時,我必須將圖像保留為外部鏈接資源,這些資源已加載到電子郵件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.