Freigeben über


Sending an email from Azure App Service using SendGrid

This articles has moved to its new home here: https://benperk.github.io/msdn/2015/2015-02-sending-an-email-from-azure-websites-using-sendgrid.html

A common question I get is, “why doesn’t my SMTP email code that I wrote for my other website not work on Azure Web App?”  Additionally, I sometimes get this exception sent to me, “System.Net.Mail.SmtpException: Transaction failed. The server response was: #.#.# Service unavailable; Client host [*.*.*.*] blocked using; Blocked”.

I also wrote an article here about sending emails using an SMTP server hosted with Office 365.  #O365 #Office365

The reason for the first issue is that currently, a common way to send an email from an Azure Web App is by using SendGrid.  You enable / subscribe to SendGrid via the Market Place by selecting the NEW link on the bottom left of the Azure Management console, similar to that shown in Figure 1.

blog001

Figure 1, setup SendGrid for Azure App Services, SMTP not working

Once configured, you should see it in the list of available Market Place apps.  Clicking on the SendGrid app, will redirect you to a page similar to that illustrated in Figure 2. blog002

Figure 2, Configure SendGrid on an Azure App Services

Clicking on the MANAGE link on the bottom navigation bar, opens the SendGrid website where you can get your user id and password.  Additionally you can find many examples in many languages to get a proof of concept page working on your Azure App Services Web App site. 

Myself, being a strong C# programmer didn’t have much problem getting that example to work, but I did struggle with the PHP one.  Here is a library you can use to get a PHP SendGrid solution up and running pretty quick.  Simply add the code to a PHP file, shown in Listing 1, and deploy it to root directory of the PHP library.

Listing 1, consume PHP SendGrid PHPMailer code, using SendGrid

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                          // Enable verbose debug output

$mail->isSMTP();                                        // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';             // Specify main/backup SMTP servers
$mail->SMTPAuth = true;                           // Enable SMTP authentication
$mail->Username = 'YOUR USERNAME';    // SMTP username
$mail->Password = 'YOUR PASSWORD';    // SMTP password
$mail->SMTPSecure = 'tls';                        // Enable TLS/SSL encryption
$mail->Port = 587;                                      // TCP port to connect to

$mail->From = 'email@contoso.com';
$mail->FromName = 'From SendGrid website';
$mail->addAddress('support@contoso.com', 'Support');     // Add a recipient

$mail->WordWrap = 50;                              // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

?>

The second issue is because the IP address for your Azure App Services Web App has been blacklisted.  Some potential work arounds for this are:

Comments

  • Anonymous
    April 10, 2015
    Hi Benjamin,Cheers mate!  After scouring the internet, including both SendGrid and Azure sites, this is literally the only working solution that I have found.  Much thanks for sharing your solution!

  • Anonymous
    July 29, 2015
    Coolness, thanks.

  • Anonymous
    August 12, 2015
    Great! This was very useful to get my Web App running! Thank you!

  • Anonymous
    December 30, 2015
    Works great. The only solution that has worked for me. Thanks!

  • Anonymous
    February 28, 2017
    Too bad I cannot install SendGrid: "At this time, this offer/item is not available for subscriptions from Microsoft Azure Cloud Solution Providers."Yet another limitation because of going through a CSP... :/ Like it's discouraged.