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 class Webapirestsharp {Private Const string apiwebsite = "https://sendgrid.com"; Private Const string apiurladdress = "Api/mail.send.json"; public static void Sendnormalhelloworldemail () {var client = new Restclient (apiwebsite); var request = new Restrequest (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 var response = client. Execute (Request); var content = Response. Content; Raw content as StrING}}
. NET using the SendGrid WEB API to send mail (with source code)