简体   繁体   English

SendGrid:如何从 Azure blob 存储附加文件?

[英]SendGrid: How to attach a file from Azure blob storage?

I have blobs in Windows Azure blob storage that I'd like to attach to emails sent with SendGrid.我在 Windows Azure blob 存储中有 blob,我想将其附加到使用 SendGrid 发送的电子邮件中。 I'd like to specify the file name for the attachment (real file names are just mumbo jumbo) which afaik forces me to add the attachment as a stream.我想指定附件的文件名(真正的文件名只是胡说八道),afaik 迫使我将附件添加为 stream。

My code looks like this:我的代码如下所示:

var msg = SendGrid.GetInstance();
// Code for adding sender, recipient etc...
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["storage"].ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(typeName);
var blob = container.GetBlockBlobReference("somefile.png");
var ms = new MemoryStream();
blob.DownloadToStream(ms);
msg.AddAttachment(ms, "originalfilename.png");

The file gets read from the storage to the memory stream and adding the attachment seems to work fine but after receiving the email the attached file is 0 bytes.该文件从存储中读取到 memory stream 并添加附件似乎工作正常但在收到 email 后附件为 0 字节。

Thank you in advance.先感谢您。

This has probably already been solved, but you need to make sure that you 'rewind' the stream back to the beginning using Seek. 这可能已经解决了,但您需要确保使用Seek将流回放到开头。 Sample code below. 示例代码如下。

stream.Seek(0, SeekOrigin.Begin);
sendGrid.AddAttachment(stream, "name");

Even though I am not sure how AddAttachment API works, please note that your MemoryStream's position will be set to its length at the end of the download. 即使我不确定AddAttachment API如何工作,请注意您的MemoryStream的位置将设置为下载结束时的长度。 Hence, you might need to seek it to the beginning before calling AddAttachment. 因此,您可能需要在调用AddAttachment之前将其设置为开头。

var ms = new MemoryStream();
blob.DownloadToStream(ms);
ms.Position = 0;
msg.AddAttachment(ms, "originalfilename.png");

I found a way to stream the file directly from Azure into the SendGrid email message object. Here's the relevant snippet:我找到了一种方法 stream 文件直接从 Azure 进入 SendGrid email 消息 object。这是相关的片段:

var client = new SendGridClient(apiKey);
var mailFrom = new EmailAddress(from.Email, from.Name);
var mailTo = new EmailAddress(to.Email, to.Name);
var msg = MailHelper.CreateSingleEmail(mailFrom, mailTo, subject, bodyText, bodyHtml);
    
// Attach file from Azure if fileName is set
if (fileName != null) {
    // Initialize the Azure blob client
    var container = new BlobContainerClient(_azureConnectionString, _azureContainerName);
    var blob = container.GetBlobClient(fileName);
    
    // Validate the file exists in Azure
    if (!await blob.ExistsAsync()) return NotFound("File not found");
                
    // Stream the file directly from Azure into the email attachment
    await msg.AddAttachmentAsync(fileName, blob.OpenRead());
}
    
var response = await client.SendEmailAsync(msg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM