C # send an email signed by DKIM

Source: Internet
Author: User

DKIM (DomainKeys Identified Mail) is an email verification standard-domain name key recognition Mail standard, based on which the Mail system can determine whether the Mail is spam.

It is used to generate a private key/public key pair and store the public key in the DNS server as a TXT record, when sending an email, select the corresponding email field (such as From, To, and Subject) To generate the signature information through the private key and append it To the mail header for sending. After receiving the email, the target email server obtains the public key from the DNS query based on the sender's domain name, and then uses the public key to verify that the signature information in the email header is valid.

We successfully sent the email signed by DKIM through the C # code. The procedure is as follows:

1. Use the tool provided by dkimcore.org to generate a private key and a record containing the public key to be added to DNS.

A) Open http://dkimcore.org/tools/, enter the Domain Name of the email Sending address in Domain Name, and click Generate;

B) if the domain name is cnblogs. biz, we will get the three data shown in -- Selector (1362200600. cnblogs), Private key, DNS record (Name: 1362200600. cnblogs. _ domainkey, value: v = DKIM1; the part ending with a colon [does not contain a colon, not shown in the figure]).

2. Add TXT records to the DNS server

3. Check whether the DNS record is successfully added.

Open the http://dkimcore.org/tools/, enter Selector and Domain in the Check a published DKIM Core Key column, and click check; if added successfully, "This is a valid DKIM key record" is displayed ". See:

4. Write the C # code to send the email signed by DKIM.

A) Check out the source code of DKIM. Net (https://github.com/dmcgiv/DKIM.Net) from github and compile it to get DKIM. Net. dll;

B) add the reference of DKIM. Net. dll to the project and write the code for sending the email in C #. The sample code is as follows:

Static void SendDkimMail () {var privateKey = @ "----- begin rsa private key ----- encrypt + encrypt/decrypt/V/encrypt/decrypt/fRHgOJ/encrypt/decrypt + lVPNU2bGhc/QQ7YpgMUga3b + MP7C3w/decrypt + cjTd67sgjBk77Fbb8kN1nzXCq/QbqB + gwmq8YMMdHLxD/oUCQHG55lZhOc7/+ examples // 3v4XTj1tIE4Ux/Signature + Signature = ----- end rsa private key -----"; var domain = "cnblogs. biz "; var selector =" 1362200600. cnblogs "; var headers = new string [] {" From "," To "," Subject "}; var mailMsg = new MailMessage (); mailMsg. to. add (new MailAddress ("recipient email"); mailMsg. from = new MailAddress ("test@cnblogs.biz", "test"); mailMsg. subject = "test"; mailMsg. body = "test"; // DKIM signature for the email var domainKeySigner = new DomainKeySigner (PrivateKeySigner. create (privateKey), domain, selector, headers); mailMsg. domainKeySign (domainKeySigner); var smtpClient = new SmtpClient ("mail. cnblogs. biz "); smtpClient. send (mailMsg );}

C) run the code to send an email

5. Receive the email and check whether the email has been verified by DKIM.

During the test, the mail is sent to the gmail mailbox. In the gmail reading window, select "Show Original mail". If the email has passed the DKIM verification, the following information is displayed:

DomainKey-Signature: a=rsa-sha1; c=simple; d=cnblogs.biz; h=From:To:Subject; q=dns; s=1362200600.cnblogs; b=JCzfhjomQPJK6m5GCLt/I/K4oQ7...;

Note: If you want to use a friendly Selector name (1362200600. cnblogs in this article), you can set it as long as the code is consistent with the DNS record. For example, if you use the dkim name, the name is dkim. _ domainkey when adding a record in DNS, and the code is var selector = "dkim ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.