iDipa
iDipa

Reputation: 345

SendGrid Attachment Error : cannot convert from 'System.IO.MemoryStream' to 'string'

I have file in azure storage as PDF. Now I want to attach it as attachment in mail. For mail sending I am using SendGrid version 9.8.0.0. But it is giving error like 'cannot convert from 'System.IO.MemoryStream' to 'string''.

Code is like below :

CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

            var stream = new MemoryStream();
            blob.DownloadToStream(stream);
            stream.Seek(0, SeekOrigin.Begin);
            ContentType content = new ContentType(MediaTypeNames.Text.Plain);
            stream.Position = 0;

            #endregion

            var msg = new SendGridMessage()
            {
                From = new EmailAddress(email.FromMail),
                Subject = email.Subject,
                HtmlContent = email.MailBody,

            };

            msg.AddAttachment(stream, "originalfilename.png"); <-- here giving error
            msg.AddTo(new EmailAddress(email.Recipient));

What is wrong in this??

Upvotes: 1

Views: 1162

Answers (1)

Renatas M.
Renatas M.

Reputation: 11820

From here I see there is method

public void AddAttachment(string filename, string base64Content, string type = null, string disposition = null, string content_id = null)

So you need to pass file name first and then string encoded in base64. Code should look something like this:

msg.AddAttachment("originalfilename.png", System.Convert.ToBase64String(stream.ToArray()));

P.s. in question you are talking about PDF but attaching PNG.

Upvotes: 1

Related Questions