title: Signing email in C#
body: <p>Based on this <a href="https://stackoverflow.com/questions/2358095/how-to-domainkeys-dkim-email-signing-using-the-c-sharp-smtp-client">question</a> I decided to sign emails send from ASP.NET.MVC  to decrease SPAM score of emails, but I have some bug somewhere.</p>
  
  <p>Code: </p>
  
  <pre><code>    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);
          }          
      }
  </code></pre>
  
  <p>I check the result on <a href="http://www.isnotspam.com" rel="noreferrer">http://www.isnotspam.com</a>, the output is following:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My DNS record is:</p>
  
  <pre><code>@ IN TXT "v=dkim1; s=mail; p=migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdnov2pxnjmghdpxw5wpypk1rf7 kxs+5ouvh6f0hraryncku6wbvq+xovbgxz1kuddcb/s9o8wquftxrlffniik3wbm qc+upm+ndloxcxwy0bb2iktbgnmndjiexm/z0npaviwzebr2k6vqdzbp+lmcuece bwasqgw2fki5ospb4qidaqab"
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>I fix some issues in DNS record and I have found better online checker at <a href="http://dkimcore.org/c/keycheck" rel="noreferrer">dkimcore.org</a></p>
  
  <p>I still face validation issue of my public key. I generated 1024 RSA using puttyGen (ppk) and convert it to RSA format. The original file from PuttyGen is:</p>
  
  <pre><code>---- BEGIN SSH2 PUBLIC KEY ----
  Comment: "rsa-key-20170606"
  AAAAB3NzaC1yc2EAAAABJQAAAIEAiyEwx+Idlf/Qp2fTYrQMwV3MuF9W7yaKDMHk
  hzoH+MqWKtNDngQoJcmbyrkMeF0VLYo246ma3gPZh9cDL7i8ygOYKagbyUjgtZFz
  y+et0tY/+G/IZNaHiQp0QuG/J71uZrl4Jlgkq+0s5bZxpRR45aRpcG1HQMIm6Ku7
  lgmOt88=
  ---- END SSH2 PUBLIC KEY ----
  </code></pre>
  
  <p>So I just copy the content (except the commented lines) to DNS record and I got following output from checker:</p>
  
  <pre><code>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.
  </code></pre>