C# 似乎无法将自定义文本替代视图添加到我的多部分电子邮件中

C# 似乎无法将自定义文本替代视图添加到我的多部分电子邮件中,c#,email,smtpclient,mailmessage,alternateview,C#,Email,Smtpclient,Mailmessage,Alternateview,我正在编写一个向客户发送大量电子邮件的系统,对于每个站点,我们都会存储一个HTML和文本版本的电子邮件,这样,如果用户的邮件客户端不支持HTML,文本视图的格式仍然是正确的,正如我们所希望的那样 我们不想仅仅从HTML版本生成纯文本版本,因为它添加了大量菜单链接和其他未按我们希望的格式格式化的文本 这在ASP classic中可以很好地使用Persits电子邮件组件= 当我们添加HTML字符串作为正文,文本字符串作为正文时 然而,我在C#NET 4.5中复制这一点时遇到了困难 我已经尽可能多地使

我正在编写一个向客户发送大量电子邮件的系统,对于每个站点,我们都会存储一个HTML和文本版本的电子邮件,这样,如果用户的邮件客户端不支持HTML,文本视图的格式仍然是正确的,正如我们所希望的那样

我们不想仅仅从HTML版本生成纯文本版本,因为它添加了大量菜单链接和其他未按我们希望的格式格式化的文本

这在ASP classic中可以很好地使用Persits电子邮件组件= 当我们添加HTML字符串作为正文,文本字符串作为正文时

然而,我在C#NET 4.5中复制这一点时遇到了困难

我已经尽可能多地使用了示例,但是我的方法返回了MailMessage对象,该对象需要传递HTML以查找图像/横幅,然后用ContentID和LinkedResources替换URL,不知何故,它返回了一封在HTML和简单HTML视图中看起来很棒的电子邮件(在Thunderbird中)

然而,无论我做什么,纯文本视图似乎总是对象试图转换为文本的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>

/*我传入一个自定义SiteEmail对象,该对象具有两个属性HTMLEmail和TextEmail,这两个属性都包含电子邮件的两个版本*/
公共邮件解析电子邮件图像(SiteEmail SiteEmail)
{
MailMessage email=新的MailMessage();
//提取HTML版本,因为我们需要解析它,以将URL交换为ContentID/资源和路径等
字符串HTML=sitemail.HTMLEmail;
//这是一个通用列表,用于保存我找到的所有图片资源对象(将图像URL交换为路径和内容ID)
列表图片资源=新列表();
//代码查找所有路径,创建图像资源对象并将它们添加到我的列表中,并修改HTML以引用
//我交换URL的新ContentID使图像嵌入到电子邮件中
//……代码。。。。。
//已完成查找资源对象生成电子邮件部件并返回给调用者
//将每个备选视图添加到消息中。
//使用我新解析的HTML字符串添加HTML视图
ContentType HtmlContentType=新ContentType(“text/html;charset=UTF-8”);
AlternateView altViewHTML=AlternateView.CreateAlternateView-FromString(HTML,HtmlContentType);
altViewHTML.Transferncode=Transferncode.QuotedPrintable;
//当我选中此处时,sitemail.textmail包含我想要的正确文本字符串,但它没有显示在已发送的电子邮件中
ContentType-PlainContentType=新的ContentType(“文本/普通;字符集=UTF-8”);
//因为我们不需要在文本视图中更改任何内容,所以我们可以直接从我的自定义电子邮件对象siteEmail中引用它
AlternateView altViewText=AlternateView.CreateAlternateView-FromString(siteEmail.TextEmail,PlainContentType);
altViewText.Transferncode=Transferncode.QuotedPrintable;
//循环浏览所有“我的图片”资源对象,并确保它们嵌入到HTML电子邮件中
foreach(pictureResources中的LinkedResource pictureResource)
{
pictureResource.Transferncode=Transferncode.Base64;
altViewHTML.LinkedResources.Add(pictureResource);
}
//添加电子邮件的两个部分(文本/html),这两个部分都是消息的备选视图
email.alternativeviews.Add(altViewText);
email.alternativeviews.Add(altViewHTML);
//返回电子邮件对象
回复邮件;
}
//调用方法的一个非常精简的示例
公共bool sendmail()
{
//解析我们的电子邮件对象
MailMessage=this.ParseEmailImages(this.sitemail);
//发送电子邮件
EmailMailMessage.From=新邮件地址(this.SendFromEmail,this.SendFromName);
EmailMailMessage.Subject=this.SendSubject;
//确保阿拉伯语/日语站点的编码正确,并且正文传输方法正确
EmailMailMessage.BodyEncoding=Encoding.UTF8;
EmailMailMessage.BodyTransferncode=Transferncode.QuotedPrintable;
SmtpClient=新的SmtpClient();
//这是一个更复杂的尝试
client.Send(this.emailmessage);
}
我尝试过使用编码格式,只是添加了一个备选视图等等,但似乎无法让它发送与我的旧ASP经典代码相同的电子邮件,例如一封包含两个边界的多部分电子邮件,一封用于我们要使用的文本版本,另一封用于HTML版本。它似乎总是从HTML版本创建自己的纯文本版本,这是我不希望发生的

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


提前感谢您的帮助

答案似乎是MailMessage类中有一个bug。我被告知要在Microsoft.NET论坛上报告此事

问题似乎在于LinkedResources的使用

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


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

为什么HTML视图和简单文本视图都是可选视图?我刚刚结束了我在网上看到的教程,因为这里没有人知道它们在做什么。我认为你必须为电子邮件创建两个部分,一个HTML,一个文本,然后将它们添加到带有边界的多部分电子邮件中。如果我遗漏了什么,请让我知道我应该做些什么,这样我就可以得到我想要的HTML和我想要的文本视图