3
votes

In an Azure hosted MVC app I have mail settings as follows

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network defaultCredentials="false" host="smtp.gmail.com" userName="[email protected]" enableSsl="true" port="587" password="password" />
  </smtp>
</mailSettings>

When I create a new SMTP client like this:

SmtpClient client = new SmtpClient();

All the details are empty and when the client tries to send the email I get an SMTP host not specified exception. This only started happening when upgrading to MVC4 and Azure 2.0 (from Mvc 3 and Azure 1.7) Why is it not using the mail settings from config?

1
Is this issue occurring in the emulator, the actual Windows Azure cloud, or both?MikeWo
I can confirm that I have this same issue. The mail settings are being (even using sendgrid) ignored and the SmtpClient fails with Exception=System.InvalidOperationException: The SMTP host was not specified. at System.Net.Mail.SmtpClient.CheckHostAndPort() at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken) at System.Net.Mail.SmtpClient.SendMailAsync(MailMessage message) even though the settings are in web.config.Razor

1 Answers

3
votes

A simpler setup in the web.config works for me (I am using the SendGrid email app, set up as an app through the Azure portal):

<mailSettings>
    <smtp from="[email protected]">
        <network host="smtp.sendgrid.net" port="587" userName="[email protected]" password="password" />
    </smtp>
</mailSettings>

The new SmtpClient() call is the same as what has been given in the original question. It's working both in the emulator as well as in the Azure hosted website.

In case the problem is arising solely due to the gmail hosting, you could perhaps give SendGrid a try. It's free for Azure users up to a limit, and can be set up directly from within the Azure portal itself.