1

我正在编写一个系统来向客户发送批量电子邮件,并且对于每个站点,我们都会存储电子邮件的 HTML 和文本版本,这样如果用户的邮件客户端不支持 HTML,那么文本视图的格式仍然正确且符合我们的要求。

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

这在带有 Persits 电子邮件组件 = http://www.aspemail.com/的 ASP 经典中运行良好, 因为我们将 HTML 字符串添加为正文,将文本字符串添加为 AltBody。

但是我无法在 C# .NET 4.5 中复制它

我已经遵循了尽可能多的示例,但是我的方法返回 MailMessage 对象,该对象需要传递 HTML 以查找图像/横幅,然后用 ContentIDs 和 LinkedResources 替换 URLS 以某种方式返回在 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 Classic 代码相同的电子邮件,例如具有 2 个边界的多部分电子邮件,1 个用于文本版本 WE WANT TO USE 和 HTML 版本之一。它似乎总是从我不想发生的 HTML 版本创建它自己的纯文本版本。

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

提前感谢您的帮助!

4

1 回答 1

2

答案似乎是 MailMessage 类中存在错误。我被告知要在 Microsoft .NET 论坛上报告这一点。

似乎问题在于使用 LinkedResources。

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

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

于 2013-07-29T08:55:55.130 回答