我在 Windows Azure Blob 存储中有 Blob,我想将其附加到使用 SendGrid 发送的电子邮件中。我想指定附件的文件名(真实的文件名只是胡言乱语),据我所知,这迫使我将附件添加为流。
我的代码如下所示:
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 字节。
先感谢您。
这个问题可能已经解决了,但是您需要确保使用 Seek 将流“倒回”到开头。下面的示例代码。
stream.Seek(0, SeekOrigin.Begin);
sendGrid.AddAttachment(stream, "name");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)