Ashr Raza
Ashr Raza

Reputation: 41

Send Email From Azure Function App Without using SendGrid and Office 365

I need to send an email from Azure Function app without using sendgrid and office365.

By using outlook and Gmail smtp, the function is giving errors

using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;


public static void Run(Stream myBlob, string name, ILogger log, ExecutionContext context)
{
    log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

    string fromEmail = System.Environment.GetEnvironmentVariable("SmtpUser");
    string toEmail = "[email protected]";
    int smtpPort = Int32.Parse(System.Environment.GetEnvironmentVariable("SmtpPort"));
    bool smtpEnableSsl = true;
    string smtpHost = System.Environment.GetEnvironmentVariable("SmtpHost"); // your smtp host
    string smtpUser = System.Environment.GetEnvironmentVariable("SmtpUser"); // your smtp user
    string smtpPass = System.Environment.GetEnvironmentVariable("SmtpPassword"); // your smtp password
    string subject = System.Environment.GetEnvironmentVariable("EmailSubject");
    string message = "Hello, you are recieving message from Azure Function /n Thanks, /n Pooja";

    log.LogInformation($" Pass : {smtpPort} : Email: {fromEmail} : Host: {smtpHost} : Subject : {subject}");


    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    client.Port = smtpPort;
    client.EnableSsl = smtpEnableSsl;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Host = smtpHost;
    client.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPass);

    MailMessage mail = new MailMessage();
    mail.Subject = subject;
    mail.From = new MailAddress(fromEmail);
    mail.Body=message;
    mail.To.Add(new MailAddress(toEmail));

    try{
        client.Send(mail);

        log.LogInformation("Email sent");

    }

    catch(Exception ex){
        log.LogInformation(ex.ToString());
    }

}

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Upvotes: 2

Views: 6720

Answers (1)

DixitArora-MSFT
DixitArora-MSFT

Reputation: 1811

Sending outbound e-mail to external domains (such as outlook.com, gmail.com, etc) directly from an e-mail server hosted in Azure compute services is not supported due to the elastic nature of public cloud service IPs and the potential for abuse.  As such, the Azure compute IP address blocks are added to public block lists (such as the Spamhaus PBL).  There are no exceptions to this policy."

The only way to use EMAIL functionality as of now on Azure Web App is via an SMTP relay. A third party service such as SendGrid provides these type of services.

In the Azure Web Apps architecture the actual Web Apps sit behind common Front-Ends which are shared by all the sites hosted on that Data Centre. There is a possibility that one of the site hosted on that datacenter is sending SPAM emails and this could have the IP address to be blacklisted by the MAIL Servers. So the e-mails sent from that address will be rejected or considered as SPAM by mail servers

Upvotes: 4

Related Questions