SendGrid附件错误:无法从“ System.IO.MemoryStream”转换为“ string”

时间:2018-07-20 12:06:40

标签: c# azure-storage attachment sendgrid

我在天蓝色存储中的文件为PDF。现在,我想将其作为附件附加到邮件中。对于邮件发送,我使用的是SendGrid版本9.8.0.0。但这会产生错误,例如“无法从'System.IO.MemoryStream'转换为'字符串'。

代码如下:

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));

这有什么问题?

1 个答案:

答案 0 :(得分:0)

here看到有方法

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

因此,您需要首先传递文件名,然后传递以base64编码的字符串。代码应如下所示:

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

P.s。有问题的是您在谈论PDF,但附加了PNG。