iTextSharp - 在电子邮件附件中发送内存中的pdf [英] iTextSharp - Sending in-memory pdf in an email attachment

查看:149
本文介绍了iTextSharp - 在电子邮件附件中发送内存中的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了几个问题,但仍然有问题。如果你能告诉我我在代码中做错了什么,我将不胜感激。我从ASP.Net页面运行上面的代码,并获得无法访问已关闭的流。

  var doc = new Document(); 

MemoryStream memoryStream = new MemoryStream();

PdfWriter.GetInstance(doc,memoryStream);
doc.Open();
doc.Add(新段落(第一段));
doc.Add(新段落(第二段));

doc.Close(); //如果我删除这行,电子邮件附件被发送但是0字节

MailMessage mm =新的MailMessage(username@gmail.com,username@gmail.com)
{
Subject =subject,
IsBodyHtml = true,
Body =body
};

mm.Attachments.Add(new Attachment(memoryStream,test.pdf));
SmtpClient smtp = new SmtpClient
{
Host =smtp.gmail.com,
Port = 587,
EnableSsl = true,
Credentials =新的NetworkCredential(username@gmail.com,my_password)
};

smtp.Send(mm); //无法访问已关闭的流错误在此抛出

谢谢!!!



编辑:



只是为了帮助某人寻找这个问题的答案,发送一个pdf文件的代码附在一个电子邮件,而不必物理地创建文件在下面(感谢Ichiban和Brianng):

  var doc = new Document() ; 
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc,memoryStream);

doc.Open();
doc.Add(新段落(第一段));
doc.Add(新段落(第二段));

writer.CloseStream = false;
doc.Close();
memoryStream.Position = 0;

MailMessage mm = new MailMessage(username@gmail.com,username@gmail.com)
{
主题=主题,
IsBodyHtml = true,
Body =body
};

mm.Attachments.Add(new Attachment(memoryStream,filename.pdf));
SmtpClient smtp = new SmtpClient
{
Host =smtp.gmail.com,
Port = 587,
EnableSsl = true,
Credentials =新的NetworkCredential(username@gmail.com,密码)

};

smtp.Send(mm);


解决方案

您尝试过:

  PdfWriter writer = PdfWriter.GetInstance(doc,memoryStream); 

//构建pdf代码...

writer.CloseStream = false;
doc.Close();

//生成电子邮件

memoryStream.Position = 0;
mm.Attachments.Add(new Attachment(memoryStream,test.pdf));

如果我的内存正确地服务于我,这在以前的项目中解决了类似的问题。

请参阅 http://forums.asp.net/t /1093198.aspx


I've asked a couple of questions here but am still having issues. I'd appreciate if you could tell me what I am doing wrong in my code. I run the code above from a ASP.Net page and get "Cannot Access a Closed Stream".

var doc = new Document();

MemoryStream memoryStream = new MemoryStream();

PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
doc.Add(new Paragraph("First Paragraph"));
doc.Add(new Paragraph("Second Paragraph"));

doc.Close(); //if I remove this line the email attachment is sent but with 0 bytes 

MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com")
{
    Subject = "subject",
    IsBodyHtml = true,
    Body = "body"
};

mm.Attachments.Add(new Attachment(memoryStream, "test.pdf"));
SmtpClient smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    Credentials = new NetworkCredential("username@gmail.com", "my_password")
};

smtp.Send(mm); //the "Cannot Access a Closed Stream" error is thrown here

Thanks!!!

EDIT:

Just to help somebody looking for the answer to this question, the code to send a pdf file attached to an email without having to physically create the file is below (thanks to Ichiban and Brianng):

var doc = new Document();
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

doc.Open();
doc.Add(new Paragraph("First Paragraph"));
doc.Add(new Paragraph("Second Paragraph"));

writer.CloseStream = false;
doc.Close();
memoryStream.Position = 0;

MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com")
{
    Subject = "subject",
    IsBodyHtml = true,
    Body = "body"
};

mm.Attachments.Add(new Attachment(memoryStream, "filename.pdf"));
SmtpClient smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    Credentials = new NetworkCredential("username@gmail.com", "password")

};

smtp.Send(mm);

解决方案

Have you tried:

PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

// Build pdf code...

writer.CloseStream = false;
doc.Close();

// Build email

memoryStream.Position = 0;
mm.Attachments.Add(new Attachment(memoryStream, "test.pdf"));

If my memory serves me correctly, this solved a similar problem in a previous project.

See http://forums.asp.net/t/1093198.aspx

这篇关于iTextSharp - 在电子邮件附件中发送内存中的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆