基于此question我决定签署从ASP.NET.MVC发送的电子邮件,以降低电子邮件的SPAM分数,但我在某处有一些错误。
代码:
public void SendEmail(MailMessage mailMessage)
{
string domain = "kup-nemovitost.cz";
var message = MimeMessage.CreateFromMailMessage(mailMessage);
HeaderId[] headers = new HeaderId[] { HeaderId.From, HeaderId.Subject, HeaderId.Date };
DkimCanonicalizationAlgorithm headerAlgorithm = DkimCanonicalizationAlgorithm.Relaxed;
DkimCanonicalizationAlgorithm bodyAlgorithm = DkimCanonicalizationAlgorithm.Relaxed;
string dkimPath = Path.Combine(ConfigHelper.GetDataPath(), "DKIM");
string privateKey = Path.Combine(dkimPath, "kup-nemovitost.cz.private.rsa");
DkimSigner signer = new DkimSigner(privateKey, domain, "mail")
{
SignatureAlgorithm = DkimSignatureAlgorithm.RsaSha1,
AgentOrUserIdentifier = "@" + domain,
QueryMethod = "dns/txt",
};
message.Prepare(EncodingConstraint.SevenBit);
message.Sign(signer, headers, headerAlgorithm, bodyAlgorithm);
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("localhost", 25, false);
client.Send(message);
client.Disconnect(true);
}
}
我在http://www.isnotspam.com检查结果,输出如下:
DKIM check details:
----------------------------------------------------------
Result: invalid
ID(s) verified: header.From=no-reply@kup-nemovitost.cz
Selector=mail
domain=kup-nemovitost.cz
DomainKeys DNS Record=mail._domainkey.kup-nemovitost.cz
我的DNS记录是:
@ IN TXT "v=dkim1; s=mail; p=migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdnov2pxnjmghdpxw5wpypk1rf7 kxs+5ouvh6f0hraryncku6wbvq+xovbgxz1kuddcb/s9o8wquftxrlffniik3wbm qc+upm+ndloxcxwy0bb2iktbgnmndjiexm/z0npaviwzebr2k6vqdzbp+lmcuece bwasqgw2fki5ospb4qidaqab"
更新:
我修复了DNS记录中的一些问题,我在dkimcore.org
找到了更好的在线检查功能我仍然面临公钥的验证问题。我使用puttyGen(ppk)生成1024 RSA并将其转换为RSA格式。 PuttyGen的原始文件是:
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20170606"
AAAAB3NzaC1yc2EAAAABJQAAAIEAiyEwx+Idlf/Qp2fTYrQMwV3MuF9W7yaKDMHk
hzoH+MqWKtNDngQoJcmbyrkMeF0VLYo246ma3gPZh9cDL7i8ygOYKagbyUjgtZFz
y+et0tY/+G/IZNaHiQp0QuG/J71uZrl4Jlgkq+0s5bZxpRR45aRpcG1HQMIm6Ku7
lgmOt88=
---- END SSH2 PUBLIC KEY ----
所以我只是将内容(注释行除外)复制到DNS记录中,我从检查器得到以下输出:
p= AAAAB3NzaC1yc2EAAAABJQAAAIEAiyEwx+Idlf/Qp2fTYrQMwV3MuF9W7yaKDMHkhzoH+MqWKtNDngQoJcmbyrkMeF0VLYo246ma3gPZh9cDL7i8ygOYKagbyUjgtZFzy+et0tY/+G/IZNaHiQp0QuG/J71uZrl4Jlgkq+0s5bZxpRR45aRpcG1HQMIm6Ku7lgmOt88=
This doesn't seem to be a valid RSA public key: RSA.xs:178: OpenSSL error: wrong tag at blib/lib/Crypt/OpenSSL/RSA.pm (autosplit into blib/lib/auto/Crypt/OpenSSL/RSA/new_public_key.al) line 91.
答案 0 :(得分:4)
正如我们在评论中发现的那样 - 您的代码很好,但是您的公钥存在问题。首先,它在dns txt记录中有空格,它不能有。然后,您的公钥格式错误(SSH2)。验证签名的工具需要不同的格式(常规PEM RSA),它应如下所示:
--- BEGIN RSA PUBLIC KEY ---
MII ....
--- END RSA PUBLIC KEY ---
因此,与您的私钥相同。您可以使用
将SSH2密钥转换为PEMssh-keygen -f output.pub -m 'PEM' -e > public.pem
其中output.pub
是SSH2格式的公钥。使用这样的密钥(并且在DNS记录中没有空格)它应该可以正常工作,假设你将它放入正确的dns记录 - key_selector._domainkey.your.domain。