【发布时间】:2018-02-15 05:33:00
【问题描述】:
我有一个作为控制台应用程序运行的按需 azure webjob。
目标是向客户发送电子邮件。
问题是它不发送电子邮件。
我的代码大量来自msdn 部分如何:发送电子邮件。以下是我的代码 sn-p。
class Program
{
static void Main(string[] args)
{
Execute(Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS")).Wait();
}
static async Task Execute(string email)
{
DeserializedCustomer customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
var client = new SendGridClient(apiKey);
SendGridMessage msg = new SendGridMessage()
{
From = new EmailAddress(customer.Email, "From Email"),
Subject = $"Inquiry by - {customer.FirstName} {customer.LastName}",
PlainTextContent = customer.Comments + Environment.NewLine + $"Phone : {customer.Phone}",
};
msg.AddTo(new EmailAddress(ToMail, ToMailName));
if (!String.IsNullOrWhiteSpace(customer.Attachment))
{
List<Attachment> attachments = new List<Attachment>()
{
new Attachment()
{
Content = customer.Attachment,
Type = customer.AttachmentType,
Filename = customer.AttachmentFileName,
Disposition = "inline",
ContentId = customer.AttachmentFileName + Guid.NewGuid()
}
};
msg.Attachments = attachments;
}
await client.SendEmailAsync(msg);
}
}
搜索我发现了一个 SO post 给 sendgrid 时间在控制台应用程序中发送电子邮件的地方
增加时间也无济于事。
如果从作为控制台应用程序运行的 azure webjobs 发送电子邮件有一些奇怪之处,那么我不知道。
进一步搜索我发现this SO 帖子他们成功测试了在控制台应用程序中发送电子邮件,所以我认为它可能会有所帮助,但它没有发送电子邮件。
以上所有示例都已有一年多的历史,目前都没有工作。
我尝试使用早期版本的 SendGrid 库,但我卡在了
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(myMessage);
因为 SendGrid 库已更新。
我在 github 上找到了 this,它明确指出控制台应用程序仅适用于 transportWeb.DeliverAsync(myMessage).Wait
因此我正在尝试使用 transportWeb。
Azure 与作为控制台应用程序运行的按需 Web 作业是否合并?
谁能帮忙?
更新
在 Randy Minder 的帮助下,我将代码更新为以下内容
static async Task Execute(string email)
{
try
{
DeserializedCustomer customer = new DeserializedCustomer();
customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
Console.WriteLine(customer);
SendGridMessage msg = new SendGridMessage();
msg.From = new MailAddress(customer.EmailAddress, "From Email");
msg.Subject = $"Inquiry by - {customer.FirstName} {customer.LastName}";
msg.Text = customer.Comments + Environment.NewLine + $"Phone : {customer.Phone}";
msg.AddTo(ToMail);
// Create a network credentials object
var credentials = new NetworkCredential(Environment.GetEnvironmentVariable("UserName"), Environment.GetEnvironmentVariable("Password"));
var transportWeb = new SendGrid.Web(credentials);
transportWeb.DeliverAsync(msg).Wait();
}
catch (Exception ex)
{
DateTime now = DateTime.Now;
Trace.TraceError($"{now.ToLongDateString()} {now.ToLongTimeString()}" + Environment.NewLine + new ExceptionSerializer(ex));
}
}
我正在使用 SendGrid 6.1.0
<package id="Sendgrid" version="6.1.0" targetFramework="net47" />
我没有遇到任何异常,我的 webjob 运行成功
[09/06/2017 17:48:42 > 1a8d37: SYS INFO] Run script 'SaSRizqTechCloudWhizEngineering.Backgr.exe' with script host - 'WindowsScriptHost'
[09/06/2017 17:48:42 > 1a8d37: SYS INFO] Status changed to Running
[09/06/2017 17:48:42 > 1a8d37: INFO] SerializedEmail - {"FirstName":"adsfkh","LastName":"adfkjladf","EmailAddress":"jamilakhtar@gmail.com","Phone":"","Comments":"lkjadf ","AttachmentType":"","AttachmentFileName":"","Attachment":null}
[09/06/2017 17:48:42 > 1a8d37: INFO] FirstName adsfkh - LastName adfkjladf - EmailAddress jamilakhtar@gmail.com - Phone - Comments lkjadf - Attachment - AttachmentType - - AttachmentFileName
[09/06/2017 17:48:44 > 1a8d37: SYS INFO] Status changed to Success
但是我没有收到任何电子邮件
【问题讨论】:
标签: azure email sendgrid azure-webjobs ondemand