lee_mcmullen lee_mcmullen - 1 year ago 148
C# Question

AddAttachment from MemoryStream

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


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

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

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.

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

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download