-
-
Save chimpinano/ecd90b72261e9c4b0204 to your computer and use it in GitHub Desktop.
a script to send emails via SendGrid C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using System.Net; | |
| using System.Net.Mail; | |
| using SendGrid | |
| namespace SendGridMail | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Create the email object first, then add the properties. | |
| var myMessage = SendGrid.GetInstance(); | |
| // Add the message properties. | |
| myMessage.From = new MailAddress("from@youremail.com"); | |
| // This address will be ignored but must be specified | |
| myMessage.AddTo("email@example.com"); | |
| // Add multiple addresses to the To field. | |
| List<String> recipients = new List<String> | |
| { | |
| @"name2 <email2@sendgrid.com>", | |
| @"name3 <email3@sendgrid.com>", | |
| @"name4 <email4@sendgrid.com>" | |
| }; | |
| myMessage.Header.SetTo(recipients); | |
| myMessage.Subject = "Testing the SendGrid Library"; | |
| //Add the HTML and Text bodies | |
| myMessage.Html = "<p>Hello World! This is a test with CCs</p>"; | |
| myMessage.Text = "Hello World plain text!"; | |
| // Create network credentials to access your SendGrid account. | |
| var username = "sendgrid_username"; | |
| var pswd = "SendGrid_password"; | |
| var credentials = new NetworkCredential(username, pswd); | |
| // Create an SMTP transport for sending email. | |
| var transportWeb = new Web(credentials); | |
| // Send the email. | |
| transportWeb.Deliver(myMessage); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment