翻譯|使用教程|編輯:吉煒煒|2025-01-10 10:09:22.323|閱讀 109 次
概述:本文介紹如何使用 .NET C# 創建 PDF 文檔并將其作為電子郵件附件發送。PDF 是使用 TX Text Control .NET Server 組件創建的,電子郵件是使用 System.Net.Mail 命名空間發送的。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
發送附加到電子郵件的 PDF 文檔是開發人員的常見且關鍵的任務。PDF 是文檔交換的黃金標準,提供一致的格式和跨平臺兼容性,使其成為共享發票、報告、合同和其他業務關鍵信息的理想選擇。從開發人員的角度來看,自動創建和交付這些文檔可以簡化工作流程、提高效率并確保可靠的通信。
例如,Web 應用程序可以在客戶購買后立即自動生成個性化發票并通過電子郵件將其發送給客戶。在本文中,我們將探討如何創建 PDF 文檔并將其作為電子郵件附件發送。
為了演示使用 TX 文本控制庫實現這一點有多么簡單,我們將使用 .NET 控制臺應用程序。
確保您下載了附帶的最新版本的 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@fc6vip.cn
文章轉載自:慧都網