C#:将PDF下载并附加到MailMessage已损坏

时间:2014-11-20 07:18:57

标签: c# pdf-generation mailmessage

我想首先将pdf作为字符串下载,然后将其附加到MailMessage。这是我到目前为止所做的事情

string htmlAttachment = webClient.DownloadString((HttpUtility.HtmlDecode(dictIterator.Value)));
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(htmlAttachment);
writer.Flush();
stream.Position = 0;
msg.Attachments.Add(new Attachment((Stream)stream, dictIterator.Key));

但每当我打开pdf附件时,它会说Insufficient data for an image'. Is it something related to encoding and should I directly download it as a stream rather than first getting it as string` ??

1 个答案:

答案 0 :(得分:0)

是的,你应该用二进制文件下载文件(作为字符串..不涉及编码..只是纯二进制文件。)


PS - 在连接之前,尝试将文件本地保存到磁盘。然后从磁盘打开它。 它将消除MailMessage成为问题的不太可能的可能性。