.NET MailMessage 和 AlternativeViews 中的 DKIM

发布于 2024-11-13 20:35:31 字数 596 浏览 6 评论 0原文

我正在使用 DKIM.NET (https://github.com/dmcgiv/DKIM.Net) 对 MailMessage 进行签名,然后再将其发送给收件人。我面临的问题是,当我以 HTML 和纯文本形式以 AlternativeViews 的形式插入内容时,上面的组件对 MailMessage 的正文 (mailMessage.Body) 进行了签名。

结果是我的 mailMessage.Body 为空,但收到的邮件正文包含我的替代视图,因此 DKIM 无法正确验证。

有什么办法可以解决这个问题吗?也许在将 HTML 和纯文本替代视图分配给 MailMessage 对象之前对其进行签名?或者也许使用另一个组件?

编辑:

自从我开始这个问题以来,我在 https:// 创建了一个项目github.com/yannispsarras/DKIM-AlternativeViews - 这绝不是完整或稳定的,但我将其发布在这里,以防任何人在 .NET 中寻找签名替代视图的解决方案时有任何用处。

I am using DKIM.NET (https://github.com/dmcgiv/DKIM.Net) to sign a MailMessage before sending it to a recipient. The problem i am facing is that the component above signs MailMessage's Body (mailMessage.Body) while I am inserting content as both HTML and plain text in the form of AlternativeViews.

The result is that my mailMessage.Body is null but the received messsage's body contains my alternative views therefore DKIM does not verify correctly.

Is there any way to resolve this problem? Maybe sign the HTML and Plain text alternative views before assigning them to the MailMessage object? Or maybe using another component?

EDIT:

Since I started this question I 've created a project at https://github.com/yannispsarras/DKIM-AlternativeViews - This is by no means complete or stable but I m posting it here in case its of any use to anyone looking to find a solution for signed alternative views in .NET.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-11-20 20:35:31

我在开源的 MimeKit 中添加了对生成和验证 DKIM 签名的全面支持(许可证:MIT)并且完全免费用于商业用途。

如果您还需要 SMTP、POP3 和/或 IMAP 支持,请查看 MailKit,它构建于MimeKit。

由于 MimeKit 和 MailKit 在每次写入流时不会生成一组新的边界字符串,因此它们不会遇到使用 System.Net.Mail 和 DKIM.Net[1](不是 DKIM.Net 的错误,要明确)。

要在 MimeKit 中向邮件添加 DKIM 签名,您需要执行以下操作:

var message = CreateMyMessage ();
var headersToSign = new [] { HeaderId.From, HeaderId.To, 
    HeaderId.Subject, HeaderId.Date };
var signer = new DkimSigner ("C:\my-dkim-key.pem") {
   AgentOrUserIdentifier = "@eng.example.net",
   Domain = "example.net",
   Selector = "brisbane",
};

message.Sign (signer, headersToSign, 
    DkimCanonicalizationAlgorithm.Relaxed, 
    DkimCanonicalizationAlgorithm.Simple);

要使用 MailKit 发送邮件,您需要执行以下操作:

using (var client = new SmtpClient ()) {
    client.Connect ("smtp.gmail.com", 465, true);
    client.Authenticate ("username", "password");
    client.Send (message);
    client.Disconnect (true);
}

注意:

  1. 由于 System.Net.Mail.SmtpClient 会为以下对象生成一组新的边界标记多部分消息(当您有附件或 AlternativeViews 时使用的消息),您不能使用 DKIM.Net 来签署所述消息,因为当您实际发送消息时,签名将被破坏,因为 MIME 格式的消息正文将发生更改。

I've added full support for generating and verifying DKIM signatures in MimeKit which is open source (License: MIT) and completely free for commercial use.

If you also need SMTP, POP3, and/or IMAP support, check out MailKit which is built on top of MimeKit.

Since MimeKit and MailKit do not generate a new set of boundary strings each time they are written to a stream, they do not suffer from the problems you will face using System.Net.Mail and DKIM.Net[1] (not DKIM.Net's fault, to be clear).

To add a DKIM signature to a message in MimeKit, you would do something like this:

var message = CreateMyMessage ();
var headersToSign = new [] { HeaderId.From, HeaderId.To, 
    HeaderId.Subject, HeaderId.Date };
var signer = new DkimSigner ("C:\my-dkim-key.pem") {
   AgentOrUserIdentifier = "@eng.example.net",
   Domain = "example.net",
   Selector = "brisbane",
};

message.Sign (signer, headersToSign, 
    DkimCanonicalizationAlgorithm.Relaxed, 
    DkimCanonicalizationAlgorithm.Simple);

To send the message using MailKit, you would do something like this:

using (var client = new SmtpClient ()) {
    client.Connect ("smtp.gmail.com", 465, true);
    client.Authenticate ("username", "password");
    client.Send (message);
    client.Disconnect (true);
}

Notes:

  1. Since System.Net.Mail.SmtpClient generates a new set of boundary markers for multipart messages (which is what is used when you have attachments or AlternativeViews), you cannot use DKIM.Net to sign said messages because the signature will break when you actually go to send the message because the MIME-formatted message body will have changed.
歌枕肩 2024-11-20 20:35:31

您可以尝试 Mail.dll 电子邮件组件,它支持 DKIM,包括:签名和验证:

http://www.limilabs.com/blog/sign-emails-with-dkim

http://www.limilabs.com/blog/sign-emails-with-dkim 不过,该组件不是免费的,请注意它是我写的。

You can try Mail.dll email component it supports DKIM, both: signing and validation:

http://www.limilabs.com/blog/sign-emails-with-dkim

The component is not free however, please also note that I wrote it.

帅气称霸 2024-11-20 20:35:31

我已更新 DKIM.Net 网站上的自述文件来解释此限制。这基本上是由于 System.Net.Mail.SmtpClient 生成边界来分隔替代视图或附件的方式 - 它们是新的 Guid,因此每次发送消息时边界 ID 都会发生变化 - 如果内容发生变化,则签名失败。该代码攻击 SmptClient,通过使用虚拟流发送 MailMessage 来获取电子邮件的完整内容。

I've updated the read me on the DKIM.Net site to explain this limitation. It's basically due to the way System.Net.Mail.SmtpClient generates boundaries to seperate alternative views or attachments - they are new Guids so each time the message is sent the boundary id changes - if the content changes then the signing fails. The code hacks SmptClient to get the full content of the email by Sending the MailMessage using a dummy stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文