似乎不能添加自定义文本替代视图到我的多部分电子邮件

本文关键字:视图 我的 多部 电子邮件 不能 添加 自定义 文本 | 更新日期: 2023-09-27 18:18:49

我正在写一个系统发送批量电子邮件给客户端和每个网站,我们存储的电子邮件的HTML和文本版本,这样,如果用户邮件客户端不支持HTML文本视图仍然格式化正确,我们想要的。

我们不想只从HTML版本生成纯文本版本,因为它添加了许多菜单链接和其他文本,这些文本不是我们想要的格式。

这在ASP经典的Persits电子邮件组件= http://www.aspemail.com/中工作得很好当我们将HTML字符串添加为Body,将文本字符串添加为AltBody。

然而,我在c# .NET 4.5

中复制此功能时遇到了麻烦

我已经遵循了尽可能多的例子,但我的方法,返回MailMessage对象,需要通过HTML寻找图像/横幅,然后用contentid和LinkedResources替换url,以某种方式返回一个在HTML和简单HTML视图(在雷鸟)中看起来很棒的电子邮件。

然而,无论我做什么,纯文本视图似乎总是对象试图转换为文本而不是的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经典代码,例如一个多部分的电子邮件有2个边界,1为文本版本我们想要使用和一个HTML版本。它似乎总是从HTML版本创建自己的纯文本版本,这是我不希望发生的。

任何帮助或想法都将不胜感激。

提前感谢您的帮助!

似乎不能添加自定义文本替代视图到我的多部分电子邮件

答案似乎是MailMessage类中有一个错误。我被告知要在microsoft.net论坛上如实报告。

问题似乎出在LinkedResources的使用上。

如果我删除将LinkedResources添加到HTML替代视图的代码,那么代码就可以正常工作,并且我可以在邮件客户端中看到纯文本和HTML视图。

因此,当用户在其电子邮件客户端按下任何"加载远程内容"按钮时,我必须将图像作为加载到电子邮件中的外部链接资源。