2

我在附加内存中创建的 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);
        }

我不知道这段代码出了什么问题...电子邮件没有任何附件。

帮助表示赞赏。

4

1 回答 1

2

我看不出您做错了什么,但我正在做类似的事情,尽管通过解析网页来创建我的 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);

这有帮助吗?

于 2012-08-02T09:08:20.510 回答