1

I want to send email via Azure Functions hosted on my company Azure subscription using company email addresses for both sender and recipient.

Is it possible to use SMTP server like below:

  var fromAddress = new MailAddress("from@myfirm", "From Name");
    var toAddress = new MailAddress("to@myfirm", "To Name");
    const string fromPassword = "from password";
    string subject = "Subject";
    string body = "Body";

    var smtp = new SmtpClient
    {
        Host = "my company's smtp server",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    };

    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
    {
        smtp.Send(message);
    }

Refs

https://learn.microsoft.com/en-in/azure/sendgrid-dotnet-how-to-send-email

How do i send email from Azure function app

Update:

What if a dedicated IP address is used for Azure app that sends out email using SMTP? Would this eliminates the issue? If not, what other potential issues?

Pingpong
  • 7,681
  • 21
  • 83
  • 209

1 Answers1

0

Update:

Starting on November 15, 2017, outbound email messages that are sent directly to external domains (such as outlook.com and gmail.com) from a virtual machine (VM) are made available only to certain subscription types in Microsoft Azure. Outbound SMTP connections that use TCP port 25 were blocked. (Port 25 is primarily used for unauthenticated email delivery.)

This change in behavior applies only to new subscriptions and new deployments since November 15, 2017.

This is the doc:

https://learn.microsoft.com/en-us/azure/virtual-network/troubleshoot-outbound-smtp-connectivity

Azure recommend to use third party SMTP relay like SendGrid to send email.

Original Answer:

I am not sure the language you are using now, so I assume you are using C#.

Below is how to use SendGrid in azure function:

For example, if you want to send email from email A to email B.

First of all, install package Microsoft.Azure.WebJobs.Extensions.SendGrid.

go to the sendgrid website, create a sender and verify the email A:

enter image description here

After that, email A is ready to send emails by sendgrid.

Now we need to generate a SendGrid API key and copy and store the API key:(This will be filled in the Values section of local.settings.json as an environment variable to read.)

enter image description here

Then, you can use it to send emails

Use SendGrid Binding:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;

namespace FunctionApp40
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] out SendGridMessage message,
            ILogger log)
        {
            message = new SendGridMessage();
            message.AddContent("text/plain","This is a test.");
            message.SetFrom("emailA@emailA.com");
            message.AddTo("emailB@emailB.com");
            message.SetSubject("This is send from Function app.");
            log.LogInformation("This is a test.");
        }
    }
}

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
      "AzureWebJobsStorage": "UseDevelopmentStorage=true",
      "FUNCTIONS_WORKER_RUNTIME": "dotnet",
      "CustomSendGridKeyAppSettingName": "SG.XQ9uKeO3QCmNH7hA8qP8eA.xxxxxx"
    }
}

Works fine on my side:

enter image description here

By the way, this can only guarantee successful delivery, because some mailboxes refuse to accept mail sent through sendgrid. In this case, you will still get a 200 response, but the recipient will not receive the mail.(In this case, the recipient needs to go to the mailbox settings to lift the relevant restrictions.)

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • Why do you say Azure Function dont allow SMTP server. It is working for me. – Pingpong Nov 24 '20 at 11:31
  • @Pingpong Sorry, I revised my wrong expression in my answer. This restriction does not apply to all subscriptions. Take a look at the precautions for using the Azure platform: https://learn.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview#azure-platform-considerations The reputation of all Azure IPs has been affected due to the abuse of Azure IP by some users, so Azure prohibits the direct use of SMTP for certain subscriptions. (This restriction not only apply to Azure Function, it applies to Azure virtual machines.) – Cindy Pau Nov 25 '20 at 03:03
  • @Pingpong The reason it works for you may be because of your subscription type. In any case, using a third-party SMTP relay is the method recommended by Azure. – Cindy Pau Nov 25 '20 at 03:04
  • Thanks for your info. I added a update to my OP. Please could you have a look. – Pingpong Nov 25 '20 at 09:19
  • @Pingpong Why use dedicated IP address? And eliminates what issue? I think since your subscription will not block the smtp port, you should not encounter problems in this regard. – Cindy Pau Nov 25 '20 at 10:08
  • Please see this for the reason using dedicated IP address. https://social.msdn.microsoft.com/Forums/en-US/7b06155c-83bb-41b2-8678-6a05a83a3390/how-do-i-send-email-from-azure-function-app?forum=AzureFunctions – Pingpong Nov 25 '20 at 12:06