如何使用SendGrid在Microsoft Azure中发送电子邮件

时间:2014-04-17 04:35:26

标签: email azure sendgrid

在Microsoft Azure网站中,我们如何使用SendGrid发送电子邮件?

1 个答案:

答案 0 :(得分:5)

你需要获得SendGrid UserId&来自Azure的密钥,然后使用下面的代码发送电子邮件:

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;
    }
}

您可以参考以下链接http://sendgrid.com/docs/Code_Examples/csharp.html

了解更多信息