我正在使用 Azure Webjobs 执行各种任务。我已经使我的代码可以发送电子邮件,如下所示:
public void ProcessEmailMessage(
[QueueTrigger(AzureConstants.queueEmailsToSend)] string message,
IBinder binder, ILogger logger) {
logger.LogInformation(message);
BlobServiceClient blobServiceClient = new BlobServiceClient(_appSettings.AzureWebJobsStorage);
var blobContainer = blobServiceClient.GetBlobContainerClient(_appSettings.DataStoreRoot); //set to "rafflegamesstore"..
var blobClient = blobContainer.GetBlobClient(message);
MemoryStream mailBlobstream = new MemoryStream();
blobClient.DownloadTo(mailBlobstream);
mailBlobstream.Position = 0;
MimeMessage messageToSend = MimeMessage.Load(mailBlobstream);
// the SmtpClient class is the one from Mailkit not the framework!
using (var emailClient = new SmtpClient())
{
//The last parameter here is to use SSL (Which you should!)
emailClient.Connect(EmailConstants.SmtpServer, EmailConstants.SmtpPort, true);
//Remove any OAuth functionality as we won't be using it.
emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
emailClient.Authenticate(EmailConstants.SmtpUsername, EmailConstants.SmtpPassword);
emailClient.Send(messageToSend);
emailClient.Disconnect(true);
}
我知道目前它非常简单,但这是一个原理演示......而不是使用 Blob 客户端等的所有恶作剧,我在这里的 MS Docs 中读到我应该能够将流传递到代码中. 因此将函数签名更改为:
public void ProcessEmailMessage(
[QueueTrigger(AzureConstants.queueEmailsToSend)] string message,
[Blob("rafflegamesstore/{queueTrigger}", FileAccess.Read)] Stream mailBlob,
IBinder binder, ILogger logger)
{
...
我认为应该从队列中的消息中读取 Blob 并结合容器名称?(这是'rafflegamestore'...... _appSettings.DataStoreRoot 在 appsettings.json 中也设置为此......
"DataStoreRoot": "rafflegamesstore",
这很好用,但是当我在上面使用它来尝试获取“mailBlob”时它失败了,说明 BlobPath 无效。我尝试更改字符串以将斜杠作为''上的正向并将其转义为'\',但均无效。
我确定我只是在做一些愚蠢的事情……但我真的很想让这项工作有什么想法吗?