iTextSharp的 - 发送内存中的PDF电子邮件附件 [英] iTextSharp - Sending in-memory pdf in an email attachment
问题描述
我问过几个问题在这里,但我仍然有问题。我倒是AP preciate如果你能告诉我什么,我在我的code做错了。我从ASP.Net页运行code以上,并得到无法访问已关闭的流。
VAR DOC =新的文件();MemoryStream的MemoryStream的=新的MemoryStream();PdfWriter.GetInstance(文件,MemoryStream的);
doc.Open();
doc.Add(新段(第一款));
doc.Add(新段(第二段落));doc.Close(); //如果我删除此行的电子邮件附件发送,但有0字节毫米MAILMESSAGE新= MAILMESSAGE(username@gmail.com,username@gmail.com)
{
主题=主题,
IsBodyHtml = TRUE,
身体=身
};mm.Attachments.Add(新附件(的MemoryStream的test.pdf));
SmtpClient SMTP =新SmtpClient
{
主机=smtp.gmail.com
端口= 587,
EnableSsl = TRUE,
证书=新的NetworkCredential(username@gmail.com,MY_PASSWORD)
};smtp.Send(毫米); //无法访问已关闭流的错误在这里抛出
谢谢!
编辑:
只是为了帮助别人寻找这个问题的答案时,code发送附加到电子邮件的PDF文件,而无需实际创建该文件是以下(感谢一番,并Brianng):
VAR DOC =新的文件();
MemoryStream的MemoryStream的=新的MemoryStream();
PdfWriter作家= PdfWriter.GetInstance(文件,MemoryStream的);doc.Open();
doc.Add(新段(第一款));
doc.Add(新段(第二段落));writer.CloseStream = FALSE;
doc.Close();
memoryStream.Position = 0;毫米MAILMESSAGE新= MAILMESSAGE(username@gmail.com,username@gmail.com)
{
主题=主题,
IsBodyHtml = TRUE,
身体=身
};mm.Attachments.Add(新附件(MemoryStream的filename.pdf));
SmtpClient SMTP =新SmtpClient
{
主机=smtp.gmail.com
端口= 587,
EnableSsl = TRUE,
证书=新的NetworkCredential(username@gmail.com,密码)};smtp.Send(毫米);
你试过:
PdfWriter作家= PdfWriter.GetInstance(文件,MemoryStream的);//生成PDF code ...writer.CloseStream = FALSE;
doc.Close();//建立电子邮件memoryStream.Position = 0;
mm.Attachments.Add(新附件(的MemoryStream的test.pdf));
如果我没有记错正确,这解决了类似的问题,在previous项目。
请参阅 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屋!