我有一个按需的azure webjob作为控制台应用程序运行。
目标是向客户发送电子邮件。
问题是它不发送电子邮件。
我的代码大量取消msdn部分如何:发送电子邮件。以下是我的代码段。
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与作为控制台应用程序运行的随需webjob合并?
有人可以帮忙吗?
更新
在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
但我没有收到任何电子邮件
答案 0 :(得分:1)
尝试在控制台应用中通过SendGrid发送电子邮件时,您必须采取不同的方式。这是我在控制台应用程序中使用的方法:
/// <summary>
/// Send the email async in a console app.
/// </summary>
public async Task SendAsync()
{
// Create a network credentials object
var credentials = new NetworkCredential(azureUserName, azurePassword);
// Create an Web transport for sending the email
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(this._email).Wait();
}
这是我在非控制台应用中使用的内容:
/// <summary>
/// Send the email async in backend or MVC code.
/// </summary>
public async Task SendAsync()
{
// Create a network credentials object
var credentials = new NetworkCredential(azureUserName, azurePassword);
// Create an Web transport for sending the email
var transportWeb = new Web(credentials);
await transportWeb.DeliverAsync(this._email).ConfigureAwait(false);
}
实际的电子邮件对象包含在this._email
。
答案 1 :(得分:0)
根据你的描述,我也在我身边创建了一个测试演示,它运行良好。我使用的sendgird SDK是9.90。
详细信息代码如下所示:
class Program
{
static void Main(string[] args)
{
string serializedEmail = args[0];
Console.WriteLine($"SerializedEmail - {serializedEmail}");
Customer customer = JsonConvert.DeserializeObject<Customer>(args[0]);
//Customer customer = new Customer() { Name = "aaaa" };
Execute(customer).Wait();
Console.WriteLine(customer.Name);
Console.ReadLine();
}
static async Task Execute(Customer customer)
{
var client = new SendGridClient("apikey");
var from = new EmailAddress("sendgird@xxxxxx.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("sendgird@xxxxxxx.com", "Example User");
var plainTextContent = $"Name : {customer.Name}";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}
}
public class Customer
{
public string Name { get; set; }
}
但我没有收到任何电子邮件
我建议您首先访问此url以检查sendgird是否已将电子邮件发送到正确的电子邮件地址。
结果如下: