代码之家  ›  专栏  ›  技术社区  ›  iDipa

SendGrid附件错误:无法从“系统”转换。伊奥。MemoryStream'到'string'

  •  1
  • iDipa  · 技术社区  · 8 年前

    我在azure存储中有PDF格式的文件。现在我想把它作为附件附在邮件中。对于邮件发送,我使用SendGrid 9.8.0.0版。但它给出了类似“无法从”系统转换的错误。伊奥。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 回复  |  直到 8 年前
        1
  •  1
  •   Renatas M.    8 年前

    从…起 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()));
    

    问题是,你说的是PDF,但附加PNG。