2017-06-03 122 views
10

基於此question我決定簽署從ASP.NET.MVC發送的電子郵件以降低郵件的垃圾郵件分數,但我在某處存在一些錯誤。使用C#簽名電子郵件

代碼:

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: [email protected] 
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" 

UPDATE

我修復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. 
+1

好的,所以你有一個地方的錯誤。與實際發生的事情相對應的期望行爲是什麼? – Nkosi

+0

希望的行爲是,http://www.isnotspam.com應確認該電子郵件是使用結果通過DKIM簽署的。 – qub1n

+1

像SendGrid這樣的服務有一個非常慷慨的免費層,負責處理垃圾郵件中沒有郵件的大部分負擔。 –

回答

3

正如我們在評論想通了 - 你的代碼很好,但你有公鑰問題。首先,它在dns txt記錄中有空格,這是它不能擁有的。然後,您的公鑰格式不正確(SSH2)。驗證簽名的工具需要不同的格式(常規PEM RSA),其格式應該如下所示:

--- BEGIN RSA PUBLIC KEY --- 
MII .... 
--- END RSA PUBLIC KEY --- 

因此,與您的私鑰相同。您可以SSH2密鑰轉換爲PEM與

ssh-keygen -f output.pub -m 'PEM' -e > public.pem 

output.pub是SSH2格式的公鑰。使用這樣的密鑰(並且在DNS記錄中沒有空格),它應該可以正常工作,假設您將其放入正確的dns record - key_selector._domainkey.your.domain。