来自MemoryStream的AddAttachment

来自MemoryStream的AddAttachment

本文介绍了来自MemoryStream的AddAttachment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

SendGrid API文档指定您可以从Stream添加附件. 该示例使用了FileStream对象.

The SendGrid API docs specify you can add attachments from a Stream. The example it gives uses a FileStream object.

我在Azure存储中有一些Blob,我想通过电子邮件将其作为附件发送.为此,我尝试使用MemoryStream:

I have some blobs in Azure Storage which I'd like to email as attachments. To achieve this I'm trying to use a MemoryStream:

var getBlob = blobContainer.GetBlobReferenceFromServer(fileUploadLink.Name);
if(getBlob != null)
{
  // Get file as a stream
  MemoryStream memoryStream = new MemoryStream();
  getBlob.DownloadToStream(memoryStream);
  emailMessage.AddAttachment(memoryStream, fileUploadLink.Name);
}
emailTransport.Deliver(emailMessage);

它发送正常,但是当电子邮件到达时,附件似乎在那里,但实际上是空的.查看电子邮件来源,没有附件的内容.

It sends fine but when the email arrives, the attachment appears to be there but it's actually empty. Looking at the email source, there is no content for the attachment.

在使用SendGrid C#API发送附件时,使用MemoryStream是已知限制吗?还是应该以其他方式解决这个问题?

Is using a MemoryStream a known limitation when using the SendGrid C# API to send attachments? Or should I be approaching this in some other way?

推荐答案

我最终得到了以下解决问题的方法:

I ended up with the following which fixed the issue for me:

fileByteArray = new byte[getBlob.Properties.Length];
getBlob.DownloadToByteArray(fileByteArray, 0);
attachmentFileStream = new MemoryStream(fileByteArray);
emailMessage.AddAttachment(attachmentFileStream, fileUploadLink.Name);

这篇关于来自MemoryStream的AddAttachment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:28