SendGrid:如何从Azure Blob存储附加文件? [英] SendGrid: How to attach a file from Azure blob storage?

查看:67
本文介绍了SendGrid:如何从Azure Blob存储附加文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Windows Azure Blob存储中的Blob附加到通过SendGrid发送的电子邮件中.我想指定附件的文件名(实际文件名只是Mumbo jumbo),这是afaik强制我将附件添加为流的方式.

I have blobs in Windows Azure blob storage that I'd like to attach to emails sent with 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.

我的代码如下:

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");

该文件从存储器中读取到内存流中,添加附件似乎可以正常工作,但是在收到电子邮件后,附件为0字节.

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.

谢谢.

推荐答案

这可能已经解决,但是您需要确保使用Seek将流倒带"回到开头.下面的示例代码.

This has probably already been solved, but you need to make sure that you 'rewind' the stream back to the beginning using Seek. Sample code below.

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

这篇关于SendGrid:如何从Azure Blob存储附加文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆