我在附加内存中创建的 PDF 文件并将其附加到电子邮件模板时遇到问题。
电子邮件没有任何问题..但没有附件。我不明白为什么会这样。
这是该过程的完整代码。
ExtendedEmailTemplate emailTemp = new ExtendedEmailTemplate();
emailTemp.FromAddress = "ABC Ltd <info@abcTechnology.com>";
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);
}
我不知道这段代码出了什么问题...电子邮件没有任何附件。
帮助表示赞赏。