首页 > 解决方案 > 在 .NET 中向电子邮件添加内容描述

问题描述

我正在尝试在电子邮件中设置 MIME 内容描述字段。我使用 AlternateView 设置了 Content-Type 和 Content-Transfer-Encoding,但我找不到添加 Content-Description 的方法。

我可以做的是使用自定义标头设置 Content-Description,但这似乎为整个电子邮件插入单个 Content-Description,而不是 MIME 对象。

using( MailMessage mailMessage = new MailMessage() )
{
    mailMessage.From = new MailAddress( _configuration.FromAddress );
    mailMessage.BodyEncoding = Encoding.UTF8;
    mailMessage.To.Add( to );
    mailMessage.Subject = subject;

    // add the MIME data
    var irisB2View = AlternateView.CreateAlternateViewFromString( body );
    irisB2View.ContentType = new ContentType( "Application/EDI-consent" ); // Content-Type
    irisB2View.TransferEncoding = TransferEncoding.Base64; // Content-Tranfer-Encoding
    mailMessage.AlternateViews.Add( irisB2View );

    // this adds Content-Description, but it appears before the MIME data
    mailMessage.Headers.Add( "Content-Description", "IRIS/B2/Z" );

    client.Send( mailMessage );
}

这会产生如下形式的电子邮件,在 MIME 对象之前带有 Content-Description:

Content-Description: IRIS/B2/Z
MIME-Version: 1.0
From: xxxxxx@xxxxxx.xx
To: xxxxxx@xxxxxx.xx
Date: 28 Sep 2018 13:49:51 +0200
Subject: subject
Content-Type: Application/EDI-consent
Content-Transfer-Encoding: base64

有人知道我如何设法将 Content-Description 放入 MIME 内容中吗?

同样,邮件没有边界(我只有一个 MIME 对象),那么 Content-Description 在 MIME 对象之前是否真的很重要?

提前致谢。

标签: c#.netemailmime

解决方案


RFC 2045 指定在单个部分消息中,MIME 标头字段在常规 RFC 822 标头的上下文中使用。RFC 822 没有对标头字段施加顺序。因此 Content-Description 字段的位置并不重要。


推荐阅读