-2
using (MemoryStream stream = new MemoryStream())
            {
                compositeLink.PrintingSystem.ExportToPdf(stream);
                Response.Clear();
                Response.Buffer = false;
                Response.AppendHeader("Content-Type", "application/pdf");
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
                Response.BinaryWrite(stream.GetBuffer());
                Response.End();


            }

我得到这个工作正常。下一步是将此 pdf 文件作为附件发送到邮件中

using (MemoryStream stream = new MemoryStream())
            {
                compositeLink.PrintingSystem.ExportToPdf(stream);
                Response.Clear();
                Response.Buffer = false;
                Response.AppendHeader("Content-Type", "application/pdf");
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
                Response.BinaryWrite(stream.GetBuffer());




                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add("someone@example.net");
                message.Subject = "Subject";
                message.From = new System.Net.Mail.MailAddress("someoneelse@example.net");
                message.Body = "Body";
                message.Attachments.Add(Response.BinaryWrite(stream.GetBuffer()));
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("192.168.100.100");
                smtp.Send(message);

                Response.End();

        }

我对这条线有疑问:

message.Attachments.Add(Response.BinaryWrite(stream.GetBuffer()));

任何帮助如何让它工作?谢谢

4

3 回答 3

3

尝试这样的事情;

message.Attachments.Add(New Attachment(stream, "test.pdf", "application/pdf"))
于 2012-07-03T15:14:43.850 回答
3

Response.BinaryWrite 将获取流的内容并将它们写入响应。你不想要那个。

相反,您必须创建一个新的Attachment对象并将其添加到 message.Attachments。

尝试这个:

var ct = new ContentType();
ct.MediaType = MediaTypeNames.Application.Pdf;
ct.Name = "test.pdf";
message.Attachments.Add(new Attachment(stream, ct));

您可以在此处找到更多示例代码。

于 2012-07-03T15:11:43.243 回答
0

Response.BinaryWrite方法返回void,这意味着您没有向该Add方法传递任何内容。

您应该考虑做的是直接使用您的流来创建一个Attachment实例,并将其添加到消息的集合中。这看起来像:

message.Attachments.Add(new Attachment(stream, "file.pdf"));

可以在此处的 MSDN 上找到发送带有附件的邮件的完整示例。希望有帮助。

于 2012-07-03T15:12:53.313 回答