Question

In Microsoft Azure website, how can we send email using SendGrid?

Was it helpful?

Solution

You need to get SendGrid UserId & Key from Azure, and then use below code to send email:

public void SendEmail(string emailTo, string emailSubject, string emailBody)
{
    try
    {
        //Create the email object first, then add the properties.
        SendGrid myMessage = SendGrid.GetInstance();
        myMessage.AddTo(emailTo);
        myMessage.From = new MailAddress("abc@xyz.com", "Abc Xyz");
        myMessage.Subject = emailSubject;
        myMessage.Text = emailBody;

        // Create credentials, specifying your user name and password. (Use SendGrid UserId & Password
        var credentials = new NetworkCredential("azure_xxxxxxxxxxxxxxx@azure.com", "xxxxxxxxxxxx");

        // Create an Web transport for sending email.
        var transportWeb = Web.GetInstance(credentials);

        //Send the email.
        transportWeb.DeliverAsync(myMessage);
    }
    catch (Exception ex)
    {
        string msg = ex.Message;
    }
}

More more info you can refer following link http://sendgrid.com/docs/Code_Examples/csharp.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top