翻译|使用教程|编辑:吉炜炜|2025-01-10 10:09:22.323|阅读 23 次
概述:本文介绍如何使用 .NET C# 创建 PDF 文档并将其作为电子邮件附件发送。PDF 是使用 TX Text Control .NET Server 组件创建的,电子邮件是使用 System.Net.Mail 命名空间发送的。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
发送附加到电子邮件的 PDF 文档是开发人员的常见且关键的任务。PDF 是文档交换的黄金标准,提供一致的格式和跨平台兼容性,使其成为共享发票、报告、合同和其他业务关键信息的理想选择。从开发人员的角度来看,自动创建和交付这些文档可以简化工作流程、提高效率并确保可靠的通信。
例如,Web 应用程序可以在客户购买后立即自动生成个性化发票并通过电子邮件将其发送给客户。在本文中,我们将探讨如何创建 PDF 文档并将其作为电子邮件附件发送。
为了演示使用 TX 文本控制库实现这一点有多么简单,我们将使用 .NET 控制台应用程序。
确保您下载了.NET 8 SDK附带的最新版本的 Visual Studio 2022 。
先决条件
以下教程需要 ASP.NET 的 TX Text Control .NET Server 试用版。
在 Visual Studio 2022 中,通过选择“创建新项目”来创建新项目。
选择控制台应用程序作为项目模板然后单击下一步确认。
为您的项目选择一个名称然后单击下一步确认。
在下一个对话框中,选择.NET 8 (长期支持)作为框架并通过创建进行确认。
在解决方案资源管理器中,选择您创建的项目,然后从项目主菜单中选择管理 NuGet 包...。
从包源下拉菜单中选择文本控制离线包。
安装以下软件包的最新版本:
在开始创建 PDF 文档之前,我们需要实现一个发送带附件电子邮件的类。该类将用于将生成的 PDF 文档作为附件发送。
在项目中创建一个新的类文件并将其命名为SmtpMail.cs。添加以下代码:
using System.Net.Mail; | |
public static class SmtpMail | |
{ | |
/// <summary> | |
/// Sends an email with the specified recipient, subject, body, and attachment. | |
/// </summary> | |
/// <param name="recipient">The recipient's email address.</param> | |
/// <param name="subject">The subject of the email.</param> | |
/// <param name="body">The body content of the email.</param> | |
/// <param name="attachment">The email attachment.</param> | |
public static void Send(string recipient, string subject, string body, Attachment attachment) | |
{ | |
// Create a new email message with sender and recipient details. | |
MailMessage emailMessage = new MailMessage("sender@yourdomain.com", recipient) | |
{ | |
Subject = subject, // Set the email subject. | |
IsBodyHtml = false, // Specify that the body is plain text (not HTML). | |
Body = body // Set the email body content. | |
}; | |
// Add the specified attachment to the email. | |
emailMessage.Attachments.Add(attachment); | |
// Configure and send the email using an SMTP client. | |
using (SmtpClient client = new SmtpClient()) | |
{ | |
client.Host = "smtp.yourprovider.com"; | |
client.UseDefaultCredentials = false; | |
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; | |
client.Credentials = new System.Net.NetworkCredential("username", "password"); | |
client.Port = 587; | |
client.EnableSsl = true; | |
// Send the email. | |
client.Send(emailMessage); | |
} | |
} | |
} |
现在,我们可以使用 TX Text Control 创建 PDF 文档。将以下代码添加到Program.cs文件:
using System.Net.Mail; | |
// Create an instance of the ServerTextControl class for server-side document processing. | |
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) | |
{ | |
// Initialize the ServerTextControl instance. | |
tx.Create(); | |
// Set the text content for the document. | |
tx.Text = "Hello, this is a PDF document."; | |
// Declare a byte array to hold the generated PDF data. | |
byte[] pdfAttachment; | |
// Save the document content as a PDF into the byte array. | |
tx.Save(out pdfAttachment, TXTextControl.BinaryStreamType.AdobePDF); | |
// Create an email attachment from the PDF byte array. | |
Attachment attachment = new Attachment( | |
new MemoryStream(pdfAttachment), // Stream containing the PDF data. | |
"document.pdf", // Filename for the attachment. | |
"application/pdf" // MIME type for a PDF file. | |
); | |
// Send an email with the attachment. | |
SmtpMail.Send( | |
"recipient@domain.com", // Recipient email address. | |
"Subject", // Email subject. | |
"Body", // Email body. | |
attachment // Attachment to include in the email. | |
); | |
} |
此代码创建了Server Text Control 类的新实例并创建了一个简单的文本。然后使用Save方法将该文档导出为 PDF 文件。
导出的字节数组用于从创建电子邮件附件MemoryStream,然后将其传递给Send我们实现的SmtpMail类的方法。以下屏幕截图显示了附加了 PDF 文档的电子邮件:
创建 PDF 文档并将其作为电子邮件附件发送是开发人员的常见任务。使用 TX Text Control,这项任务很容易,只需几行代码即可实现。TX Text Control 库提供了强大的 API,用于创建、修改和导出文档为各种格式,包括 PDF。
如果您有产品试用下载、价格咨询、优惠获取,或其他任何问题,请联系在线客服。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:慧都网