avoir un problème avec la connexion du fichier PDF à partir d’un stream de mémoire en utilisant ITextSharp

J’ai des problèmes pour joindre le fichier PDF créé en mémoire et le joindre au modèle de courrier électronique.

l’email se passe sans problème..Mais il n’y a aucune pièce jointe. Je ne comprends pas pourquoi cela se passe.

voici le code complet pour le processus.

ExtendedEmailTemplate emailTemp = new ExtendedEmailTemplate(); emailTemp.FromAddress = "ABC Ltd "; 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); 

Créer un PDF en mémoire

 public static MemoryStream CreateProductGuaranteeCertificatePDF(ssortingng custName, ssortingng guaranteeCode, ssortingng productName) { MemoryStream memoryStream = new MemoryStream(); ssortingng guaranteedUntil = DateTime.Now.AddYears(3).ToSsortingng("dd-MM-yyyy"); ssortingng 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; } 

Générer MailMessage

 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; } 

Envoie un email

 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); } 

Je ne sais pas ce qui ne va pas avec ce code … l’email est sans pièce jointe.

aide appréciée.

Je ne vois pas ce que vous faites mal, mais je fais quelque chose de similaire, bien que je crée mon pdf en analysant une page Web. Voici ce que je fais:

  public static Attachment GetPDfAttachmentFromUrl(ssortingng url) { ssortingng download = new WebClient().DownloadSsortingng(url); MemoryStream ms = new MemoryStream(); Document document = new Document(PageSize.A4, 80, 50, 30, 65); PdfWriter writer = PdfWriter.GetInstance(document, ms); try { SsortingngReader ssortingngReader = new SsortingngReader(download); List parsedList = HTMLWorker.ParseToList(ssortingngReader, null); document.Open(); foreach (object item in parsedList) { document.Add((IElement)item); } document.Close(); ssortingngReader.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; } 

Ensuite, dans un endroit séparé, je l’envoie comme ceci:

  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); 

Est-ce que ceci est d’une aide quelconque?