1

I have an Azure function in which I am trying to send an email:

EmailHelper.SendEmail(myEventHubMessage, notifyFrom, notifyTo, environment);

This is my SendEmail method:

MailMessage mail = new MailMessage(notifyFrom, notifyTo);
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "The actual value of host";
mail.Subject = $"[{environment}]: [{keyvaultEvent.Data.VaultName}] Key Vault Event";
mail.Body = $"An event of type {keyvaultEvent.EventType} in KeyVault {keyvaultEvent.Data.VaultName} occured at {keyvaultEvent.EventTime}";
client.Send(mail);

I tried running this locally and it is working fine. I get an email. After deploying the Azure function I get an error saying No such host is known

The values for environment, notifyfrom and notifyto are coming from app settings.

Any idea on why that is happening?

MAK
  • 1,250
  • 21
  • 50
  • Did you get those app settings values into your azure function? If you set them locally I don't think there's a mechanism to update them in the cloud as well. It doesn't carry over the app settings file into the cloud when you upload/deploy it so you have to set them in azure in order for it to retrieve the values. See here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings – tnw Jul 29 '20 at 16:13
  • @tnw Yes I have them set as configuration values. – MAK Jul 29 '20 at 17:11
  • How are you loading your configs locally? If you're loading it from local.settings.json (you should be) then you can take a look at https://stackoverflow.com/a/63124002/1138731. It gives some explanation on how the configuration works. – lopezbertoni Jul 29 '20 at 18:15
  • @lopezbertoni It is not a configuration issue. Even if I write the config values in the code itself, I get the same error – MAK Jul 30 '20 at 12:13
  • @MAK Ok, just keep in mind that if you're loading setting in the Startup.cs and overriding them with appsettings.json or similar, the values might be lost. Localhost behaves differently. Also, that error message usually means there's something wrong with the azure function, you can run the diagnostics from the portal. – lopezbertoni Jul 30 '20 at 12:56

1 Answers1

1

To send emails from Azure VMs or Apps, use an authenticated SMTP relay service, also known as a Smart Host, with TLS support.

SMTP relay services provide an intermediary SMTP server between the mail servers of the sender and recipient. The TCP connection is established via the secure ports 587 or 443.

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("user@recipient.com", "The Recipient"));
msg.From = new MailAddress("user@sender.com", "The Sender");
msg.Subject = "Test Email from Azure Web App using Office365";
msg.Body = "<p>Test emails on Azure from a Web App via Office365</p>";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("<Office365-username>", "<Office365-password>");#insert your credentials
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;

For more details, you could refer to this article.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30