首页 > 解决方案 > 从我的公司 Azure 订阅通过 Azure Functions 发送电子邮件,使用公司电子邮件地址作为发件人和收件人

问题描述

我想通过托管在我的公司 Azure 订阅上的 Azure Functions 发送电子邮件,同时使用公司电子邮件地址作为发件人和收件人。

是否可以使用如下 SMTP 服务器:

  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);
    }

参考文献

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

如何从 Azure 函数应用发送电子邮件

更新:

如果使用 SMTP 发送电子邮件的 Azure 应用使用专用 IP 地址会怎样?这会消除问题吗?如果不是,还有哪些潜在问题?

标签: azureazure-functionsazure-web-app-serviceazure-function-app

解决方案


更新:

从 2017 年 11 月 15 日开始,从虚拟机 (VM) 直接发送到外部域(例如 Outlook.com 和 gmail.com)的出站电子邮件仅适用于 Microsoft Azure 中的某些订阅类型。使用 TCP 端口 25 的出站 SMTP 连接被阻止。(端口 25 主要用于未经身份验证的电子邮件传递。)

此行为更改仅适用于 2017 年 11 月 15 日以来的新订阅和新部署。

这是文档:

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

Azure 建议使用 SendGrid 等第三方 SMTP 中继来发送电子邮件。

原答案:

我不确定您现在使用的语言,所以我假设您使用的是 C#。

下面是如何在 azure 函数中使用 SendGrid:

例如,如果您想从电子邮件 A 向电子邮件 B 发送电子邮件。

首先,安装包Microsoft.Azure.WebJobs.Extensions.SendGrid

访问sendgrid 网站,创建发件人并验证电子邮件 A:

在此处输入图像描述

之后,电子邮件 A 就可以通过 sendgrid 发送电子邮件了。

现在我们需要生成一个 SendGrid API 密钥并复制和存储 API 密钥:(这将填充到 local.settings.json 的 Values 部分作为环境变量读取。)

在此处输入图像描述

然后,您可以使用它发送电子邮件

使用 SendGrid 绑定:

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"
    }
}

在我这边工作正常:

在此处输入图像描述

顺便说一句,这只能保证成功投递,因为有些邮箱拒绝接受通过sendgrid发送的邮件。这种情况下,你还是会收到200响应,但是收件人不会收到邮件。(这种情况下收件人需要去邮箱设置解除相关限制。)


推荐阅读