我在附加内存中创建的 PDF 文件并将其附加到电子邮件模板时遇到问题。
电子邮件没有任何问题......但没有附件。我不明白为什么会发生这种情况。
这是该过程的完整代码。
ExtendedEmailTemplate emailTemp = new ExtendedEmailTemplate();
emailTemp.FromAddress = "ABC Ltd <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd94939b92bd9c9f9ea9989e95939291929a84d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>>";
emailTemp.ToAddress = custEmail;
emailTemp.Body = "This is an Test Email"
emailTemp.IsHTML = true;
// getting the memorystream of cretaed PDF file in memory
MemoryStream pdfStream = MWProductGuaranteedHelper.CreateProductGuaranteeCertificatePDF(custName, guranteeCode, productName);
// getting the MailMessage by passing the memorystream and attach the PDF
MailMessage emailMessage = ExtendedEmailTemplate.GenerateMailMessage(emailTemp, pdfStream);
// sending an email with by passing the (MailMessage)
emailTemp.SendGuaranteeCertificateAttachmentEmail(emailMessage);
在内存中创建 PDF
public static MemoryStream CreateProductGuaranteeCertificatePDF(string custName, string guaranteeCode, string productName)
{
MemoryStream memoryStream = new MemoryStream();
string guaranteedUntil = DateTime.Now.AddYears(3).ToString("dd-MM-yyyy");
string fromFile = Server.MapPath(guaranteeCertificateFilePath);
PdfReader reader = new PdfReader(fromFile);
PdfStamper stamper = new PdfStamper(reader, memoryStream);
AcroFields fields = stamper.AcroFields;
// AcroFields setting CODE EMITTED
stamper.Writer.CloseStream = false; // making sure that stream stays open after closing the stamper
stamper.FormFlattening = false;
stamper.Close();
reader.Close();
memoryStream.Position = 0; // reset the position of the stream, so that attachment works right
return memoryStream;
}
生成邮件消息
public static MailMessage GenerateMailMessage(ExtendedEmailTemplate template, MemoryStream _ms)
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(template.FromAddress);
mailMessage.To.Add(template.ToAddress);
mailMessage.Subject = template.Subject;
mailMessage.Body = template.Body;
mailMessage.Attachments.Add(new Attachment(_ms, "ABC-Certificate.Pdf", "application/pdf"));
mailMessage.IsBodyHtml = template.IsHTML;
return mailMessage;
}
发送电子邮件
public void SendGuaranteeCertificateAttachmentEmail(MailMessage _message)
{
EmailClient.Send(_message);
}
public static void Send(MailMessage mailMessage) // SMTP Settings CODE Emitted.
{
//SEND THE MAIL MESSAGE
smtpClient.Send(mailMessage);
}
我不知道这段代码出了什么问题...电子邮件没有任何附件。
感谢帮助。
最佳答案
我看不出你做错了什么,但我正在做类似的事情,尽管通过解析网页创建我的pdf。 这就是我所做的:
public static Attachment GetPDfAttachmentFromUrl(string url)
{
string download = new WebClient().DownloadString(url);
MemoryStream ms = new MemoryStream();
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
try
{
StringReader stringReader = new StringReader(download);
List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null);
document.Open();
foreach (object item in parsedList)
{
document.Add((IElement)item);
}
document.Close();
stringReader.Close();
MemoryStream pdfstream = new MemoryStream(ms.ToArray());
//create attachment
Attachment attachment = new Attachment(pdfstream, "transaction.pdf");
return attachment;
}
catch (Exception exc)
{
Console.Error.WriteLine(exc.Message);
}
return null;
}
然后在一个单独的地方我将其发送如下:
MailMessage mm = new MailMessage(from, to);
mm.Body = body;
mm.Subject = subject;
mm.IsBodyHtml = true;
mm.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient();
smtp.Send(mm);
这有帮助吗?
关于c# - 使用 ITextSharp 从内存流附加 PDF 文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774211/