当前位置: 首页 > 知识库问答 >
问题:

如何通过电子邮件发送生成的pdf附件。网芯

郎聪
2023-03-14

我试图通过电子邮件发送pdf作为附件,但发送的文件已损坏。请问是什么原因造成的?我在下面提供了我正在使用的代码,我正在使用iText7库。

byte[] file = _pdfCreator.GeneratePdf(transactionDetails);
SendMail.SendEmail("xxxx@gmail.com", "test", "Please find attached", file);

这是为了生成pdf

public byte[] GeneratePdf(List<TransactionDetail> trnx)
        {
            
            using (MemoryStream stream = new MemoryStream())

            {
                
                PdfWriter writer = new PdfWriter(stream);
                PdfDocument pdf = new PdfDocument(writer);
                pdf.AddNewPage();
                Document document = new Document(pdf);
                Paragraph header = new Paragraph("Account Statement")
                   .SetTextAlignment(TextAlignment.CENTER)
                   .SetFontSize(20);

                document.Add(header);
                LineSeparator ls = new LineSeparator(new SolidLine());
                document.Add(ls);
                // New line
                Paragraph newline = new Paragraph(new Text("\n"));

                document.Add(newline);
                Paragraph accountName = new Paragraph("paragraph");
                Paragraph accountNumber = new Paragraph("paragraph");
                Paragraph accountType = new Paragraph("paragraph");
                document.Add(accountName);
                document.Add(accountNumber);
                document.Add(accountType);
                document.Add(newline);

                float[] pointColumnWidths = { 150F, 150F, 150F, 150F, 150F };
                Table table = new Table(pointColumnWidths);
                table.AddCell(new Cell().Add(new Paragraph("Date")));
                table.AddCell(new Cell().Add(new Paragraph("Value")));
                table.AddCell(new Cell().Add(new Paragraph("Description")));
                table.AddCell(new Cell().Add(new Paragraph("Type")));
                table.AddCell(new Cell().Add(new Paragraph("Reference")));

                foreach (var t in trnx)
                {
                    table.AddCell(new Cell().Add(new Paragraph(t.Date.ToString())));
                    table.AddCell(new Cell().Add(new Paragraph(t.Value.ToString())));
                    table.AddCell(new Cell().Add(new Paragraph(t.Descr)));
                    table.AddCell(new Cell().Add(new Paragraph(t.Type.ToString())));
                    table.AddCell(new Cell().Add(new Paragraph(t.Reference)));
                }

                document.Add(table);

                stream.Position = 0;
                document.Close();
                
                stream.Close();

                return stream.ToArray();
               
                
            }
        }
     
    }

这会发送电子邮件

public static void SendEmail(string to, string subject, string body, byte[] document)
        {

          
            var attachment = new Attachment(new MemoryStream(document), "statement.pdf", "application/pdf");
            var from = "noreply@xxxxxx.xx";
            string smtpClient = "smtp.xxxxx.xx";
            int port = 2525;
            string username = "xxxxxx";
            string password = "xxxxxx";
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(new MailAddress(to));
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            mailMessage.From = new MailAddress(from);
            mailMessage.IsBodyHtml = false;
            Attachment data = attachment;
            mailMessage.Attachments.Add(data);

            SmtpClient smtp = new SmtpClient(smtpClient)
            {
                Host = smtpClient,
                Port = port,
                UseDefaultCredentials = false,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new System.Net.NetworkCredential(username, password)
            };
            try
            {
                smtp.Send(mailMessage);
            }
            catch (Exception e)
            {
                
            }

我试图通过电子邮件发送pdf作为附件,但发送的文件已损坏。请问是什么原因造成的?我在下面提供了我正在使用的代码,我正在使用iText7库。

共有1个答案

阳德润
2023-03-14

在生成PDF的代码中,删除(或注释掉)流。close()

    ...

stream.Position = 0;
document.Close();
                
//stream.Close();

return stream.ToArray();

    ...

 类似资料:
  • 我正在使用TCPDF库在我的CakePHP应用程序中创建发票。如果我转到,我的view.ctp包括在浏览器中生成和显示pdf的代码。我想添加发送PDF作为电子邮件附件的能力,所以我创建了动作,并将代码从view.ctp复制到电子邮件模板中。 现在我被卡住了,我不知道如何在附加PDF之前先生成它。在我的view.ctp页面模板的末尾,我使用 它将pdf发送到浏览器中查看,而不创建文件。如果使用“F”

  • 问题内容: 我创建了一个函数来获取当前网页的屏幕截图,并使用html2canvas和jsPDF将其保存为PDF。以下是我的代码: 通过使用上面的代码,我可以下载文件并将其保存在本地。但我想直接使用php脚本通过电子邮件发送生成的pdf。 以下是在php脚本中发布图像的代码,该图像将作为电子邮件发送: 但是如何在参数中发布生成的pdf ? 请为此提出任何解决方案。 问题答案: 请检查代码- 我希望它

  • 尝试发送带有pdf附件的电子邮件,尝试使用swickmailer,但没有成功,此代码使用zip但不使用PDF:( 邮件被发送罚款,我得到的邮件:但附件是不存在的,在meial有所有的bas64编码在电子邮件像: onatatent-Type: Application/octet-stream; name="media.pdf"Content-transver-Encode: base 64 Con

  • 问题内容: 我在理解如何使用Python通过电子邮件发送附件时遇到问题。我已成功通过电子邮件将简单消息通过电子邮件发送。有人可以在电子邮件中说明如何发送附件。我知道在线上还有其他帖子,但是作为Python初学者,我很难理解它们。 问题答案: 这是另一个:

  • 问题内容: 在我的中,我具有以下内容: 我的电子邮件代码: 当然,如果通过设置调试服务器,则可以在终端中看到该电子邮件。 但是,我实际上如何不将电子邮件发送到调试服务器,而是发送到user@gmail.com? 阅读你的答案后,让我弄明白一点: 你不能使用localhost(简单的ubuntu pc)发送电子邮件吗? 我认为在django 1.3中已经过时了,取而代之的是? 问题答案: 将电子邮件

  • 我正在使用Azure mobile services后端,我可以通过SendGrid成功地发送电子邮件。但是,每次我尝试添加附件时,它都失败了。我从来没有收到过邮件。经过一点研究,我发现我所需要的只是一个虚拟路径。我修改了路径名,但它仍然不工作。 我想不出为什么会失败。 下面是我的代码: