AddAttachment of MemoryStream

The SendGrid API docs state that you can add attachments from a stream. In the example that it gives, an object is usedFileStream .

I have some drops in Azure Storage that I would like to send as attachments. For this I am trying to use 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 a fine, but when an email arrives, the attachment is similar to it, but it is actually empty. If you look at the source of the email, there is no content to attach.

Uses a MemoryStreamknown limitation when using the SendGrid C # API to send attachments? Or should I approach this in a different way?

+4
4

: :

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

, reset 0 DownloadToStream:

var getBlob = blobContainer.GetBlobReferenceFromServer(fileUploadLink.Name);

if (getBlob != null)
{
    var memoryStream = new MemoryStream();

    getBlob.DownloadToStream(memoryStream);
    memoryStream.Seek(0,SeekOrigin.Begin); // Reset stream back to beginning
    emailMessage.AddAttachment(memoryStream, fileUploadLink.Name);
}

emailTransport.Deliver(emailMessage);

, , , , , Deliver().

+5

API void AddAttachment(Stream stream, String name). , MemoryStream, . , :

memoryStream.Seek(0, SeekOrigin.Begin);
0

, PDF NReco:

    private async Task SendGridasyncBid(string from, string to, string  displayName, string subject, **byte[] PDFBody**, string TxtBody, string HtmlBody)
        {
            ...
            var myStream =  new System.IO.MemoryStream(**PDFBody**);
            myStream.Seek(0, SeekOrigin.Begin);
            myMessage.AddAttachment(myStream, "NewBid.pdf");

            ...
        }

html pdf , ...

    private byte[] getHTML(newBidViewModel model)
    {
        string strHtml = ...;

        HtmlToPdfConverter pdfConverter = new HtmlToPdfConverter();
        pdfConverter.CustomWkHtmlArgs = "--page-size Letter";

        var pdfBytes = pdfConverter.GeneratePdf(strHtml);

        return **pdfBytes**;
    }

I'm not sure how effective this is, but it works for me, and I hope this helps someone else to understand that their affection is understandable.

0
source

All Articles