2

我需要在不使用 sendgrid 和 office365 的情况下从 Azure Function 应用程序发送电子邮件。

通过使用 Outlook 和 Gmail smtp,该功能给出错误

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 = "abc@gmail.com";
    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:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。

4

1 回答 1

4

不支持直接从托管在 Azure 计算服务中的电子邮件服务器向外部域(例如 Outlook.com、gmail.com 等)发送出站电子邮件,因为公共云服务 IP 的弹性性质和潜在的虐待。因此,Azure 计算 IP 地址块被添加到公共块列表(例如 Spamhaus PBL)中。这项政策没有例外。”

到目前为止,在 Azure Web App 上使用 EMAIL 功能的唯一方法是通过 SMTP 中继。SendGrid 等第三方服务提供这些类型的服务。

在 Azure Web 应用架构中,实际的 Web 应用位于通用前端之后,这些前端由该数据中心上托管的所有站点共享。该数据中心上托管的站点之一可能正在发送垃圾邮件,这可能具有被MAIL 服务器列入黑名单的 IP 地址。因此,从该地址发送的电子邮件将被邮件服务器拒绝或视为垃圾邮件

于 2019-08-04T09:37:44.797 回答