gpt4 book ai didi

azure - 使用发件人和收件人的公司电子邮件地址通过我公司 Azure 订阅的 Azure Functions 发送电子邮件

转载 作者:行者123 更新时间:2023-12-03 03:54:49 25 4
gpt4 key购买 nike

我想使用发件人和收件人的公司电子邮件地址,通过我公司 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://learn.microsoft.com/en-in/azure/sendgrid-dotnet-how-to-send-email

How do i send email from Azure function app

更新:

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

最佳答案

更新:

Starting on November 15, 2017, outbound email messages that are sentdirectly to external domains (such as outlook.com and gmail.com) froma virtual machine (VM) are made available only to certain subscriptiontypes in Microsoft Azure. Outbound SMTP connections that use TCP port25 were blocked. (Port 25 is primarily used for unauthenticated emaildelivery.)

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

这是文档:

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

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

原始答案:

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

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

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

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

转到sendgrid website ,创建发件人并验证电子邮件A:

enter image description here

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

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

enter image description here

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

使用 SendGrid 绑定(bind):

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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffaf2fef6f3dedffaf2fef6f3deb1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>");
message.AddTo("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8ada5a9a1a48a88ada5a9a1a48ae6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>");
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"
}
}

在我这边工作得很好:

enter image description here

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

关于azure - 使用发件人和收件人的公司电子邮件地址通过我公司 Azure 订阅的 Azure Functions 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64973068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com