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)