如何使用 C# SMTP 客户端进行 Domainkeys/DKIM 电子邮件签名?

发布于 2024-08-23 08:07:31 字数 120 浏览 4 评论 0原文

我用 C# 编写了一个发送电子邮件的程序。现在我需要使用 Dominkeys/DKIM 签署出站电子邮件,但我不知道该怎么做。

我已经设置了所有密钥,但我不知道如何获取这些密钥以及如何将它们包含在电子邮件标题中。

I have written an program in C# which sends out emails. Now I have a requirement to sign outbound emails using Dominkeys/DKIM, but I'm not sure how to do it.

I have set up all keys, but I don't know how to get those and how to include them in the email header.

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

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

发布评论

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

评论(6

初见你 2024-08-30 08:07:31

尝试使用 System.Net.Mail.MailMessage 和 System.Net.Mail.SmtpClient 进行 DKIM 签名有一个根本问题,即为了对邮件进行签名,您需要刺探 SmtpClient 的内部结构以便对消息正文作为生成 DKIM 签名标头的步骤之一。当您有替代视图或附件时,问题就会出现,因为 SmtpClient 每次写出消息时都会生成新的多部分边界,这会破坏正文哈希,从而破坏 DKIM 签名的有效性。

要解决此问题,您可以使用 MimeKitMailKit .NET 开源库,作为使用 System.Net.Mail 的替代框架。

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

MimeMessage message = MimeMessage.CreateFromMailMessage(mailMessage);
HeaderId[] headersToSign =  new HeaderId[] { HeaderId.From, HeaderId.Subject, HeaderId.Date };

string domain = "example.net";
string selector = "brisbane";

DkimSigner signer = new DkimSigner ("C:\my-dkim-key.pem", domain, selector) 
{
   SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
   AgentOrUserIdentifier = "@eng.example.com",
   QueryMethod = "dns/txt",      
};

// Prepare the message body to be sent over a 7bit transport (such as 
// older versions of SMTP). This is VERY important because the message
// cannot be modified once we DKIM-sign our message!
//
// Note: If the SMTP server you will be sending the message over 
// supports the 8BITMIME extension, then you can use
// `EncodingConstraint.EightBit` instead.
message.Prepare (EncodingConstraint.SevenBit);

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

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

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

希望有帮助。

There is a fundamental problem with trying to do DKIM signatures with System.Net.Mail.MailMessage and System.Net.Mail.SmtpClient which is that in order to sign the message, you need to poke the internals of SmtpClient in order to hash the message body as one of the steps in generating the DKIM-Signature header. The problem comes in when you have alternative views or attachments because SmtpClient will generate new multipart boundaries each time it writes out the message which breaks the body hash and thus the DKIM-Signature validity.

To work around this, you can use the MimeKit and MailKit open source libraries for .NET as an alternative framework to using System.Net.Mail.

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

MimeMessage message = MimeMessage.CreateFromMailMessage(mailMessage);
HeaderId[] headersToSign =  new HeaderId[] { HeaderId.From, HeaderId.Subject, HeaderId.Date };

string domain = "example.net";
string selector = "brisbane";

DkimSigner signer = new DkimSigner ("C:\my-dkim-key.pem", domain, selector) 
{
   SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
   AgentOrUserIdentifier = "@eng.example.com",
   QueryMethod = "dns/txt",      
};

// Prepare the message body to be sent over a 7bit transport (such as 
// older versions of SMTP). This is VERY important because the message
// cannot be modified once we DKIM-sign our message!
//
// Note: If the SMTP server you will be sending the message over 
// supports the 8BITMIME extension, then you can use
// `EncodingConstraint.EightBit` instead.
message.Prepare (EncodingConstraint.SevenBit);

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

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

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

Hope that helps.

梦境 2024-08-30 08:07:31

请参阅 https://github.com/dmcgiv/DKIM.Net 这是一封域名密钥识别邮件 (DKIM ) 用 C# 编写的 .Net 实现 - 它使您能够对 MailMessage 对象进行签名。

see https://github.com/dmcgiv/DKIM.Net it's a DomainKeys Identified Mail (DKIM) implementation for .Net written in C# - it enables you to sign MailMessage objects.

冷了相思 2024-08-30 08:07:31

使用
http://www.mimekit.org

它不仅允许使用 DKIM 进行签名,还可以包含 S /MIME 证书、PGP 证书等。
另外,它是一个非常成熟的库 - 我发现的唯一一个可以正确处理外语(英语除外)的库,因为它完全彻底地使用 unicode 进行编码。

它是免费且开源的。

Use
http://www.mimekit.org

Not only does it allow to use DKIM for signing, also you can include S/MIME certificates, PGP certificates and more.
Also, its a very mature lib - the only one i've found that handles foreign languages (apart from english) correctly, since its completely and thoroughly coded with unicode in mind.

Its free and opensource.

神经大条 2024-08-30 08:07:31

当我使用 Mailenable 作为 SMTP 中继服务器时,这为我解决了这个问题。

http://www.mailenable.com/kb/content/article。 asp?ID=ME020700

在域名上创建 DKIM TXT 记录时,不要忘记使用活动选择器作为前缀 => yourselector._domainkey.yourdomainname.be

This solved it for me when using Mailenable as SMTP relay server.

http://www.mailenable.com/kb/content/article.asp?ID=ME020700

When creating the DKIM TXT record on the domain name don't forget to use the active selector as prefix => yourselector._domainkey.yourdomainname.be

思念满溢 2024-08-30 08:07:31

如果您希望对 MailMessage 正文进行 DKIM 签名,那么 DKIM.NET 非常适合。如果您希望在消息中找到其他观点,那么我无法找到解决方案,因此我编写了自己的解决方案(带有通常免责声明的开源解决方案),可以在 https://github.com/yannispsarras/DKIM-AlternativeViews

我知道这是一个相当老的线程,但我认为它可能对某人有帮助。

If you are looking to DKIM-sign the body of the MailMessage then DKIM.NET is great. If you are looking to have alternative views in your message then I wasnt able to find a solution and wrote my own (open-source with the usual disclaimers) that can be found at https://github.com/yannispsarras/DKIM-AlternativeViews

I understand this is a pretty old thread but I thought it may help someone.

那些过往 2024-08-30 08:07:31

我在这个问题上没有找到太多帮助,但我的问题通过配置 smtp 服务器得到了解决。
我无法发布这些步骤,因为我使用的是第 3 方 smtp 服务器,并且每个服务器都有自己的配置。正确配置后,我的 smtp 自动添加 DM/DKIM 签名。

i didnt find much help on this issue, but my problem got solve by configuring smtp server.
i cant post those steps as i am using 3rd party smtp server and every server has their own configuration. after proper configuration my smtp automatically adds DM/DKIM signature.

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