C# 使用ITextSharp从内存流附加PDF文件时出现问题

C# 使用ITextSharp从内存流附加PDF文件时出现问题,c#,asp.net,itextsharp,memorystream,C#,Asp.net,Itextsharp,Memorystream,我在附加内存中创建的PDF文件并将其附加到电子邮件模板时遇到问题 电子邮件没有任何问题,但没有附件。我不明白为什么会这样 以下是该过程的完整代码 ExtendedEmailTemplate emailTemp = new ExtendedEmailTemplate(); emailTemp.FromAddress = "ABC Ltd <info@abcTechnology.com>"; emailTemp.ToAddress = custEmail; emailTemp.Body

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

这有帮助吗?

如果我正在使用PDFWriter,这当然有帮助。在我的例子中,我使用的是PDFStamper..我可以看到的主要区别是:MemoryStream pdfstream=newmemoryStream(ms.ToArray());然后使用那个内存流。也许试试看?而不是传递给母版的内存流?
MemoryStream pdfstream=newmemoryStream(ms.ToArray())是关键。很高兴能理解为什么这是有效的,而另一个是无效的。。。
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);
        }
  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);