简体   繁体   English

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

[英]How to Domainkeys/DKIM email signing using the C# SMTP client?

I have written an program in C# which sends out emails.我用 C# 编写了一个发送电子邮件的程序。 Now I have a requirement to sign outbound emails using Dominkeys/DKIM, but I'm not sure how to do it.现在我需要使用 Dominkeys/DKIM 签署出站电子邮件,但我不知道如何去做。

I have set up all keys, but I don't know how to get those and how to include them in the email header.我已经设置了所有密钥,但我不知道如何获取这些密钥以及如何将它们包含在电子邮件标题中。

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.尝试使用 System.Net.Mail.MailMessage 和 System.Net.Mail.SmtpClient 进行 DKIM 签名存在一个基本问题,即为了对消息进行签名,您需要戳 SmtpClient 的内部以散列消息正文作为生成 DKIM-Signature 标头的步骤之一。 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.当您有替代视图或附件时,问题就会出现,因为 SmtpClient 每次写出破坏正文哈希的消息时都会生成新的多部分边界,从而破坏 DKIM 签名的有效性。

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.要解决此问题,您可以使用 .NET 的MimeKitMailKit开源库作为使用 System.Net.Mail 的替代框架。

To add a DKIM signature to a message in MimeKit, you would do something like this:要在 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);

To send the message using MailKit, you would do something like this:要使用 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);
}

Hope that helps.希望有帮助。

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

我想也知道我只是找到一个dkim工具,但我不能运行成功-_- http://tinisles.blogspot.com/2009/09/sending-dkim-email-from-c.html

Use http://www.mimekit.org使用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.它不仅允许使用 DKIM 进行签名,还可以包含 S/MIME 证书、PGP 证书等。 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.此外,它是一个非常成熟的库 - 我发现唯一一个可以正确处理外语(除了英语)的库,因为它完全彻底地使用了 unicode 编码。

Its free and opensource.它是免费和开源的。

This solved it for me when using Mailenable as SMTP relay server.当使用 Mailenable 作为 SMTP 中继服务器时,这为我解决了这个问题。

http://www.mailenable.com/kb/content/article.asp?ID=ME020700http://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在域名上创建 DKIM TXT 记录时,不要忘记使用活动选择器作为前缀 => yourselector._domainkey.yourdomainname.be

If you are looking to DKIM-sign the body of the MailMessage then DKIM.NET is great.如果您希望对 MailMessage 的正文进行 DKIM 签名,那么 DKIM.NET 很棒。 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如果您希望在您的消息中有其他观点,那么我无法找到解决方案并编写了我自己的(带有通常免责声明的开源),可以在https://github.com/yannispsarras/DKIM-AlternativeViews找到

I understand this is a pretty old thread but I thought it may help someone.我知道这是一个很旧的线程,但我认为它可能对某人有所帮助。

i didnt find much help on this issue, but my problem got solve by configuring smtp server.我在这个问题上没有找到太多帮助,但是我的问题通过配置 smtp 服务器得到了解决。 i cant post those steps as i am using 3rd party smtp server and every server has their own configuration.我无法发布这些步骤,因为我使用的是 3rd 方 smtp 服务器,并且每个服务器都有自己的配置。 after proper configuration my smtp automatically adds DM/DKIM signature.正确配置后,我的 smtp 会自动添加 DM/DKIM 签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM