翻譯|使用教程|編輯:吉煒煒|2025-01-10 10:09:22.323|閱讀 109 次
概述:本文介紹如何使用 .NET C# 創(chuàng)建 PDF 文檔并將其作為電子郵件附件發(fā)送。PDF 是使用 TX Text Control .NET Server 組件創(chuàng)建的,電子郵件是使用 System.Net.Mail 命名空間發(fā)送的。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
發(fā)送附加到電子郵件的 PDF 文檔是開發(fā)人員的常見且關(guān)鍵的任務(wù)。PDF 是文檔交換的黃金標(biāo)準(zhǔn),提供一致的格式和跨平臺(tái)兼容性,使其成為共享發(fā)票、報(bào)告、合同和其他業(yè)務(wù)關(guān)鍵信息的理想選擇。從開發(fā)人員的角度來(lái)看,自動(dòng)創(chuàng)建和交付這些文檔可以簡(jiǎn)化工作流程、提高效率并確保可靠的通信。
例如,Web 應(yīng)用程序可以在客戶購(gòu)買后立即自動(dòng)生成個(gè)性化發(fā)票并通過(guò)電子郵件將其發(fā)送給客戶。在本文中,我們將探討如何創(chuàng)建 PDF 文檔并將其作為電子郵件附件發(fā)送。
為了演示使用 TX 文本控制庫(kù)實(shí)現(xiàn)這一點(diǎn)有多么簡(jiǎn)單,我們將使用 .NET 控制臺(tái)應(yīng)用程序。
確保您下載了附帶的最新版本的 Visual Studio 2022 。
先決條件
以下教程需要 ASP.NET 的 TX Text Control .NET Server 試用版。
在 Visual Studio 2022 中,通過(guò)選擇“創(chuàng)建新項(xiàng)目”來(lái)創(chuàng)建新項(xiàng)目。
選擇控制臺(tái)應(yīng)用程序作為項(xiàng)目模板然后單擊下一步確認(rèn)。
為您的項(xiàng)目選擇一個(gè)名稱然后單擊下一步確認(rèn)。
在下一個(gè)對(duì)話框中,選擇.NET 8 (長(zhǎng)期支持)作為框架并通過(guò)創(chuàng)建進(jìn)行確認(rèn)。
在解決方案資源管理器中,選擇您創(chuàng)建的項(xiàng)目,然后從項(xiàng)目主菜單中選擇管理 NuGet 包...。
從包源下拉菜單中選擇文本控制離線包。
安裝以下軟件包的最新版本:
在開始創(chuàng)建 PDF 文檔之前,我們需要實(shí)現(xiàn)一個(gè)發(fā)送帶附件電子郵件的類。該類將用于將生成的 PDF 文檔作為附件發(fā)送。
在項(xiàng)目中創(chuàng)建一個(gè)新的類文件并將其命名為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); | |
} | |
} | |
} |
現(xiàn)在,我們可以使用 TX Text Control 創(chuàng)建 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. | |
); | |
} |
此代碼創(chuàng)建了Server Text Control 類的新實(shí)例并創(chuàng)建了一個(gè)簡(jiǎn)單的文本。然后使用Save方法將該文檔導(dǎo)出為 PDF 文件。
導(dǎo)出的字節(jié)數(shù)組用于從創(chuàng)建電子郵件附件MemoryStream,然后將其傳遞給Send我們實(shí)現(xiàn)的SmtpMail類的方法。以下屏幕截圖顯示了附加了 PDF 文檔的電子郵件:
創(chuàng)建 PDF 文檔并將其作為電子郵件附件發(fā)送是開發(fā)人員的常見任務(wù)。使用 TX Text Control,這項(xiàng)任務(wù)很容易,只需幾行代碼即可實(shí)現(xiàn)。TX Text Control 庫(kù)提供了強(qiáng)大的 API,用于創(chuàng)建、修改和導(dǎo)出文檔為各種格式,包括 PDF。
如果您有產(chǎn)品試用下載、價(jià)格咨詢、優(yōu)惠獲取,或其他任何問(wèn)題,請(qǐng)聯(lián)系。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)