Signing email with DKIM has issue when opened in Gmail (C# .NET)

Hi,

when I use the the method DKIMSign of ASPOSE.Email to send an email with DKIM, but I open the mail at gmail,
It always show “dkim=neutral (body hash did not verify)”, I need your professional support

There are three point to explain my situation below :

  1. Here is the code I use to sign the email : MyCode.txt
  2. Here is the email original content : MyEDM500.eml
  3. Here is the dns setting (Query result of command line) : DNS Setting.txtEmail DKIM.zip (3.7 KB)

@carlos_liu,

I have observed your above comments and request you to please further elaboration of above point in along with snapshot. Can you please also share information what result you are intending to achieve on your end with this.

You can unzip the file “Email DKIM.zip” first, it contains the following document, and it is more detail then snapshot.

I explain more detail as follows :

  1. Here is the code I use to sign the email : MyCode.txt <-- I use this code to generate the
    DKIM Header, and you can see the header infomation in file “MyEDM500.eml”
  2. Here is the email original content : MyEDM500.eml <-- this is the eml file that I send to gmail with DKIM, and I download from gmail. when you open the file, it shows “dkim=neutral (body hash did not verify)” at the part of “Authentication-Results”
  3. Here is the dns setting (Query result of command line) : DNS Setting.txt <-- I use nslookup to find the dns “txt record” that I set for the DKIM

@carlos_liu,

Thank you for sharing the information. We have created an issue with ID EMAILNET-39462 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

@carlos_liu,
To fix issue (body hash did not verify) you need to change your code in a such way:

		...........................................................
		//MailMessage.HtmlBody = dto.MailMessage.HtmlBody;
		//string FilteredHTML = dto.MailMessage.HtmlBody.Replace("<html>", "").Replace("</html>", "").Replace("\r\n", "\n");
		//var AltViewHTML = AlternateView.CreateAlternateViewFromString("TEST", Encoding.UTF8, System.Net.Mime.MediaTypeNames.Text.Plain);
		//AltViewHTML.TransferEncoding = Aspose.Email.Mime.TransferEncoding.QuotedPrintable;
		//MailMessage.AlternateViews.Add(AltViewHTML);
		MailMessage.Body = "TEST";
		.............................................................

Regards,
Alexander Pavov,
Aspose.Email .Net developer

if the content is plain/text, the the DKIM is pass. the code used as follows :
MailMessage.Body = “TEST”;

but, if the content is html , the DKIM is still fail, the code used as follows:
MailMessage.HtmlBody = “<a href=“www.google.com”>Google web site<a href=“www.ibanff.tw”>Banff web site<a href=“www.dajinsoft.com”>Dajinsoft web site”;

@carlos_liu,

Thank you for sharing additional details. We will share feedback with you as soon as the issue will be fixed.

Need to set IsBodyHtml explicit.Please try:

MailMessage.HtmlBody =

`> “<a href=“www.google.com”>Google web site<a href=“www.ibanff.tw”>Banff web site<a href=“www.dajinsoft.com”>Dajinsoft web site”;`

MailMessage.IsBodyHtml = true;

Regards,
Alexander Pavov,
Aspose.Email .Net developer

I have tried the code : MailMessage.IsBodyHtml = true;

but it still fail, you can see the original content of the eml file with the snapshot

myedm801.png (135.2 KB)

@carlos_liu,

Thank you for sharing the information. I have updated information in our issue tracking system and will get back to you with feedback as soon as possible.

Hi, Do you find any solution for this issue ?

@carlos_liu,

I like to inform that this issue has been resolved in Aspose.Email 19.5. Can you please try to use this version on your end and share feedback with us.

Yes, I would like to try version 19.5

Thanks

The issues you have found earlier (filed as EMAILNET-39462) have been fixed in this update.

I tried the new version Aspose.Email 19.7 , here is another error that it do not signed for domain key 001.png (50.5 KB)
received eml file received001.zip (5.5 KB)

my code :
ps. the file C:\EMAIL_DEMO.html is here EMAIL_DEMO.zip (3.3 KB)

the private key file is here Email.Key.zip (820 Bytes)

private void button6_Click(object sender, EventArgs e)
{
Aspose.Email.License license = new Aspose.Email.License();
//放在內嵌資源
license.SetLicense(“Aspose.Email.lic”);

        try
        {
            MailMessage MailMessage = new MailMessage();

            #region Sender (作為主要退信address)
            MailMessage.Sender = "support@dajinsoft.com";
            MailMessage.Sender.DisplayName = "達璟資訊 Dajinsoft";
            #endregion

            #region From
            MailMessage.From = "crm@dajinsoft.com";
            MailMessage.From.DisplayName = "達璟資訊";
            #endregion

            #region To
            MailMessage.To.Add("carlossy.l@gmail.com");
            MailMessage.To.Add("carlossy.l@hotmail.com");
            #endregion

            #region Content
            MailMessage.Subject = "諮詢回覆通知ABC";
            StringBuilder sb = new StringBuilder();
            StreamReader sr = new StreamReader(@"C:\EMAIL_DEMO.html", Encoding.UTF8);
            sb.Append(sr.ReadToEnd());
            MailMessage.HtmlBody = sb.ToString();
            MailMessage.IsBodyHtml = true;
            #endregion

            MailMessage.BodyEncoding = Encoding.UTF8;
            MailMessage.SubjectEncoding = Encoding.UTF8;
            MailMessage.PreferredTextEncoding = Encoding.UTF8;

            MailMessage = SignedDKIM(MailMessage);
            //MailMessage.Save(FilePath, SaveOptions.DefaultEml);


            SmtpClient client = new SmtpClient();
            client.Host = "exmail.dajinsoft.com";
            client.Username = "username";
            client.Password = "password";
            client.Port = 25;
            //client.UseAuthentication = false;
            //client.SupportedEncryption = Aspose.Email.Clients.Base.EncryptionProtocols.Ssl2;
            client.SecurityOptions = SecurityOptions.None;
            client.Send(MailMessage);
            
        }
        catch (Exception)
        {                
            throw;
        }
    }

    private MailMessage SignedDKIM(MailMessage mailMessage)
    {
        //私鑰
        string privateKeyFile = @"C:\Develop\Dajin\Product\CRM\CommonFiles\Settings\Email.Key.pem";

        //加密演算法, 預設為 rsa
        RSACryptoServiceProvider rsa = PemReader.GetPrivateKey(privateKeyFile);

        //加簽內容
        string selector = "s1233225";
        string domain = "dajinsoft.com";
        DKIMSignatureInfo signInfo = new DKIMSignatureInfo(selector, domain);
        signInfo.HashAlgorithm = DKIMHashAlgorithm.RSASha256;
        signInfo.BodyCanonicalization = CanonicalizationType.Relaxed;
        signInfo.HeaderCanonicalization = CanonicalizationType.Relaxed;
        signInfo.Headers.Add("From");
        //signInfo.Headers.Add("Subject");

        MailMessage signedMsg = mailMessage.DKIMSign(rsa, signInfo);

        return signedMsg;
    }

@carlos_liu,

I have observed the information shared by you and a new issue with ID EMAILNET-39542 has been created in our issue tracking system to investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be addressed.

The issues you have found earlier (filed as EMAILNET-39542) have been fixed in this update.