我在azure存储中有一个PDF文件。现在我想把它作为附件邮寄。对于邮件发送,我使用SendGrid版本9.8.0.0。但它给出的错误类似于"无法从"System.IO.MemoryStream"转换为"string"。
代码如下:
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));
这是怎么回事??
从这里我看到了方法
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。