. NET using the SendGrid WEB API to send mail (with source code)

Source: Internet
Author: User

SendGrid is a third-party solution to the mail delivery service provider, which is used more widely abroad. Similar services in the country are sendcloud.
SendGrid provides two main ways of sending mail, one is SMTP APIand one is Web API. SMTP API is a relatively simple way, as long as we have a mail message ready to send directly to the SendGrid mail server can be, SendGrid mail server will help us deliver. The other is the way the Web API works.

In general, many of the three-party server providers will disable the link to the external 25 port, so you have no way to connect the SendGrid SMTP server to send mail. In this case, the Web API is a good choice. Senggrid official has a more detailed SMTP API Demo. Demo address is https://github.com/sendgrid/sendgrid-csharp because there is no Web API demo, I took the time to write a copy, and now share out https://github.com/ Justrun1983/sendgrid-csharp-webapi

The code uses the restsharp, a very handy in. NET to access the Restful API Toolkit. A complete code for sending the message is as follows, including CC, BCC, and attachments.

 Public classWebapirestsharp {Private Const stringApiwebsite ="https://sendgrid.com"; Private Const stringApiurladdress ="Api/mail.send.json";  Public Static voidSendnormalhelloworldemail () {varClient =Newrestclient (Apiwebsite); varRequest =Newrestrequest (apiurladdress, method.post); Request. Addparameter ("Api_user", Config.sendgridname); Request. Addparameter ("Api_key", Config.sendgridpassword); Request. Addparameter ("to[]", Config.toemail); Request. Addparameter ("cc[]", Config.toemail); Request. Addparameter ("bcc[]", Config.toemail); Request. Addparameter ("subject","Test"); Request. Addparameter (" from","[email protected]"); Request. Addparameter ("text","HelloWorld1"); Request. AddFile ("Files[2.txt]",@"C:\1.txt"); //Execute the request          varResponse =client.          Execute (Request); varContent = Response. Content;//raw content As String      }  }

. NET using the SendGrid WEB API to send mail (with source code)

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.