解决方法
尝试使用System.Net.Mail.MailMessage和System.Net.Mail.SmtpClient执行DKIM签名有一个根本的问题,即为了签署消息,您需要戳出SmtpClient的内部以便将消息体作为生成DKIM-Signature标题的步骤之一.当您有备用视图或附件时,问题出现,因为SmtpClient会在每次写出断开主体散列的消息并因此产生DKIM签名有效性时生成新的多边形边界.
为了解决这个问题,您可以使用MimeKit和MailKit开源库作为使用System.Net.Mail的替代框架.
要在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",}; // 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 SmtpClient ()) { client.Connect ("smtp.gmail.com",465,true); client.Authenticate ("username","password"); client.Send (message); client.Disconnect (true); }
希望有帮助.