跳過到頁腳內容
.NET幫助

Mailkit C#(開發者的工作原理)

如何使用 MailKit

對於任何希望提升行銷水準的企業而言,MailKit 是管理電子郵件和簡訊通訊的全面且功能強大的工具。 舉例來說,MailKit 可讓您建立範本,並自選資料來源自動產生電子郵件,這表示您可以頻繁傳送更新的電子郵件,而不必手動建立或傳送您的訊息。

在本指南中,我們將告訴您如何安裝並開始使用 MailKit,以及如何將 MailKit 與 IronPDF 整合,以建立功能強大的電子郵件與 PDF 產生程式。

什麼是 MailKit?

MailKit 是一個開放原始碼專案,已成為 .NET 應用程式開發中不可或缺的工具。 這是一個全面的電子郵件框架,支援透過 SMTP 和 IMAP 通訊協定傳送和接收電子郵件。 它能讓開發人員輕鬆地與郵件伺服器互動、傳送 HTML 電子郵件,以及管理安全性設定,對於需要電子郵件功能的 .NET 應用程式來說非常重要。

IronPDF 可在 .NET 應用程式中生成、渲染和處理 PDF 文件。它簡化了 HTML 模板轉換為 PDF 以及複雜文件的建立,使其成為管理 PDF 與網頁資料的理想工具。

開始使用 MailKit

在您的專案中安裝 MailKit

若要開始在您的應用程式中使用 MailKit,您需要安裝 MailKit 套件。 可透過 MailKit on NuGet 完成,NuGet 是 .NET 的套件管理程式。 您可以這樣做

  • 在 Visual Studio 中開啟您的 C# 專案
  • 導航至"解決方案總管",在您的專案上按一下滑鼠右鍵,然後選擇管理 NuGet 套件
  • 在 NuGet 套件管理員中搜尋"MailKit"並進行安裝。

為電子郵件作業設定 MailKit

安裝完成後,您就可以開始在您的應用程式中設定 MailKit。 這包括設定 SMTP 伺服器以傳送電子郵件,以及選擇性地設定 IMAP 伺服器以接收電子郵件。 以下是基本設定:

using MailKit.Net.Smtp;
using MimeKit;

public class EmailService
{
    public void SendEmail(string recipientAddress, string subject, string body)
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Your Name", "your@email.com"));
        message.To.Add(new MailboxAddress("", recipientAddress));
        message.Subject = subject;

        // Set the email body as plain text
        message.Body = new TextPart("plain")
        {
            Text = body
        };

        using (var client = new SmtpClient())
        {
            // Connect to the SMTP server
            client.Connect("smtp.server.com", 587, false);
            // Authenticate using your email credentials
            client.Authenticate("your@email.com", "yourpassword");
            // Send the email
            client.Send(message);
            // Disconnect from the server
            client.Disconnect(true);
        }
    }
}
using MailKit.Net.Smtp;
using MimeKit;

public class EmailService
{
    public void SendEmail(string recipientAddress, string subject, string body)
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Your Name", "your@email.com"));
        message.To.Add(new MailboxAddress("", recipientAddress));
        message.Subject = subject;

        // Set the email body as plain text
        message.Body = new TextPart("plain")
        {
            Text = body
        };

        using (var client = new SmtpClient())
        {
            // Connect to the SMTP server
            client.Connect("smtp.server.com", 587, false);
            // Authenticate using your email credentials
            client.Authenticate("your@email.com", "yourpassword");
            // Send the email
            client.Send(message);
            // Disconnect from the server
            client.Disconnect(true);
        }
    }
}
Imports MailKit.Net.Smtp
Imports MimeKit

Public Class EmailService
	Public Sub SendEmail(ByVal recipientAddress As String, ByVal subject As String, ByVal body As String)
		Dim message = New MimeMessage()
		message.From.Add(New MailboxAddress("Your Name", "your@email.com"))
		message.To.Add(New MailboxAddress("", recipientAddress))
		message.Subject = subject

		' Set the email body as plain text
		message.Body = New TextPart("plain") With {.Text = body}

		Using client = New SmtpClient()
			' Connect to the SMTP server
			client.Connect("smtp.server.com", 587, False)
			' Authenticate using your email credentials
			client.Authenticate("your@email.com", "yourpassword")
			' Send the email
			client.Send(message)
			' Disconnect from the server
			client.Disconnect(True)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

設定 SMTP 和 IMAP 伺服器。

設定 SMTP 伺服器以傳送電子郵件

要使用 MailKit 傳送電子郵件,您需要設定 SMTP 伺服器。 SMTP(簡單郵件傳輸協定)伺服器負責將您的電子郵件傳送給預期的收件人。 以下是在應用程式中設定 SMTP 伺服器的指南:

  • 選擇 SMTP 服務:您可以使用熱門的電子郵件服務,例如 Gmail、Outlook 或任何其他提供 SMTP 支援的服務。
  • SMTP 伺服器詳細資訊:取得您所選擇的電子郵件服務的 SMTP 伺服器位址、連接埠號碼和必要的驗證詳細資訊 (使用者名稱和密碼)。

範例:Gmail 的 SMTP 配置

以下是設定 SMTP 用戶端使用 Gmail 的 SMTP 伺服器傳送電子郵件的範例:

using MailKit.Net.Smtp;

// Connecting and authenticating to Gmail's SMTP server
using (var smtpClient = new SmtpClient())
{
    smtpClient.Connect("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
    smtpClient.Authenticate("yourgmail@gmail.com", "yourpassword");
    // Send your message here
    smtpClient.Disconnect(true);
}
using MailKit.Net.Smtp;

// Connecting and authenticating to Gmail's SMTP server
using (var smtpClient = new SmtpClient())
{
    smtpClient.Connect("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
    smtpClient.Authenticate("yourgmail@gmail.com", "yourpassword");
    // Send your message here
    smtpClient.Disconnect(true);
}
Imports MailKit.Net.Smtp

' Connecting and authenticating to Gmail's SMTP server
Using smtpClient As New SmtpClient()
	smtpClient.Connect("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls)
	smtpClient.Authenticate("yourgmail@gmail.com", "yourpassword")
	' Send your message here
	smtpClient.Disconnect(True)
End Using
$vbLabelText   $csharpLabel

設定 IMAP 伺服器以接收電子郵件

要接收和閱讀電子郵件,請設定 IMAP(網際網路訊息存取通訊協定)伺服器。 IMAP 可讓您直接在電子郵件伺服器上存取和管理電子郵件,因此成為電子郵件用戶端的熱門選擇。

連接至 IMAP 伺服器。

要連接到 IMAP 伺服器,您需要伺服器位址、連接埠號碼和帳號憑證。 以下是基本的連接設定:

using MailKit.Net.Imap;

// Connecting and authenticating to Gmail's IMAP server
using (var imapClient = new ImapClient())
{
    imapClient.Connect("imap.gmail.com", 993, true);
    imapClient.Authenticate("yourgmail@gmail.com", "yourpassword");
    // Access and manage your inbox here
    imapClient.Disconnect(true);
}
using MailKit.Net.Imap;

// Connecting and authenticating to Gmail's IMAP server
using (var imapClient = new ImapClient())
{
    imapClient.Connect("imap.gmail.com", 993, true);
    imapClient.Authenticate("yourgmail@gmail.com", "yourpassword");
    // Access and manage your inbox here
    imapClient.Disconnect(true);
}
Imports MailKit.Net.Imap

' Connecting and authenticating to Gmail's IMAP server
Using imapClient As New ImapClient()
	imapClient.Connect("imap.gmail.com", 993, True)
	imapClient.Authenticate("yourgmail@gmail.com", "yourpassword")
	' Access and manage your inbox here
	imapClient.Disconnect(True)
End Using
$vbLabelText   $csharpLabel

進階電子郵件處理與建立完整的電子郵件應用程式

整合進階 MailKit 功能

一旦您設定好使用 MailKit 收發電子郵件的基本功能,就該探索其進階功能了。

這些功能包括處理 HTML 電子郵件、使用 HTML 電子郵件範本、附加檔案,以及在電子郵件收件匣內執行用戶端排序和搜尋。

using MimeKit;

// Creating a MimeMessage for an HTML email
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "your@email.com"));
message.To.Add(new MailboxAddress("", "recipient@email.com"));
message.Subject = "Your Subject Here";

// Build the HTML body
var builder = new BodyBuilder
{
    HtmlBody = @"<html><body><h1>Hello, World!</h1></body></html>"
};

// Set the message body
message.Body = builder.ToMessageBody();
using MimeKit;

// Creating a MimeMessage for an HTML email
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "your@email.com"));
message.To.Add(new MailboxAddress("", "recipient@email.com"));
message.Subject = "Your Subject Here";

// Build the HTML body
var builder = new BodyBuilder
{
    HtmlBody = @"<html><body><h1>Hello, World!</h1></body></html>"
};

// Set the message body
message.Body = builder.ToMessageBody();
Imports MimeKit

' Creating a MimeMessage for an HTML email
Private message = New MimeMessage()
message.From.Add(New MailboxAddress("Your Name", "your@email.com"))
message.To.Add(New MailboxAddress("", "recipient@email.com"))
message.Subject = "Your Subject Here"

' Build the HTML body
Dim builder = New BodyBuilder With {.HtmlBody = "<html><body><h1>Hello, World!</h1></body></html>"}

' Set the message body
message.Body = builder.ToMessageBody()
$vbLabelText   $csharpLabel

實作 HTML 範本

您也可以使用 HTML 範本來撰寫電子郵件內容,讓電子郵件更具動態與視覺吸引力。 這些範本可以從外部檔案或內嵌資源載入,提供管理電子郵件內容的彈性。

建立完整的電子郵件應用程式

有了基礎,下一步就是使用 MailKit 建立完整的電子郵件應用程式。 這包括

  • 建立使用者介面:為您的電子郵件用戶端開發友善的使用者介面,可讓使用者輕鬆撰寫、傳送、接收和閱讀電子郵件。
  • 整合 MailKit 功能:將 MailKit 的全部功能整合到您的應用程式中,例如 SMTP 和 IMAP 伺服器、不同的內容類型支援以及電子郵件組織。
  • 使用者互動與回饋:實現使用者互動的功能,例如傳送電子郵件、檢視收件匣資料夾以及將電子郵件轉換為 PDF 的按鈕。 提供回饋並處理例外狀況,以確保順暢的使用者體驗。
  • 測試與部署:徹底測試您的電子郵件應用程式,以確保所有功能都能如預期般運作。 部署應用程式供使用者在其裝置上安裝和使用。

如何使用 MailKit 和 IronPDF。

IronPDF 是專為 Web 開發人員設計的輕量級 .NET PDF 函式庫。 它讓 PDF 檔案的讀取、寫入和處理變得輕鬆簡單,能夠將各種檔案類型轉換成 PDF 內容,而且您可以在桌面和網頁的 .NET 專案中使用它。 最棒的是 - 可在開發環境中免費試用。

您可以同時使用 MailKit 和 IronPDF 進行業界領先的電子郵件至 PDF 轉換。 以下是基本的實作:

using IronPdf;

// Render an HTML string as a PDF
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hey, Chandler!</h1></body></html>");

// Save the PDF document
pdf.SaveAs("EmailContent.pdf");
using IronPdf;

// Render an HTML string as a PDF
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hey, Chandler!</h1></body></html>");

// Save the PDF document
pdf.SaveAs("EmailContent.pdf");
Imports IronPdf

' Render an HTML string as a PDF
Private renderer = New IronPdf.ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<html><body><h1>Hey, Chandler!</h1></body></html>")

' Save the PDF document
pdf.SaveAs("EmailContent.pdf")
$vbLabelText   $csharpLabel

IronPDF 很容易使用,但安裝更容易。 有幾種方法可以做到這一點:

方法 1:NuGet 套件管理員控制台

在 Visual Studio 中,在 Solution Explorer 中,右鍵按一下 References,然後按一下 Manage NuGet Packages。 點擊瀏覽並搜尋 'IronPdf,並安裝最新版本。 如果您看到這個,它就成功了:

Mailkit Csharp Guide 1 related to 方法 1:NuGet 套件管理員控制台

您也可以前往 Tools -> NuGet Package Manager -> Package Manager Console,然後在 Package Manager Tab 中輸入以下一行:

Install-Package IronPdf

最後,您可以直接從 IronPDF 在 NuGet 上的頁面取得 IronPDF。 從右邊的功能表中選擇 Download Package 選項,然後按兩下您下載的檔案以自動安裝,再重新載入"解決方案"以開始在您的專案中使用。

沒有用? 您可以在我們的進階 NuGet 安裝說明中找到特定平台的說明。

方法 2:使用 DLL 檔案

您也可以直接從我們這裡取得 IronPDF DLL 檔案,並將其手動新增至 Visual Studio。 如需完整說明以及 Windows、MacOS 和 Linux DLL 套件的連結,請參閱我們專屬的 《IronPdf.Linux 安裝指南》

結論

透過整合 MailKit 與 IronPDF,您可以建立一個多功能的電子郵件用戶端,能夠處理各種電子郵件相關的工作,包括將電子郵件轉換為 PDF。 此應用程式不僅是電子郵件溝通的強大工具,也展示了這些函式庫在實際情境中的實際應用。

準備好使用 IronPdf 了嗎? 您可以從我們的 IronPDF 30 天免費試用開始。 它還可以完全免費用於開發目的,讓您真正能一窺其真面目。 如果您喜歡,IronPdf 的起價低至 $799。 若要節省更多費用,請參閱 Iron軟體套件授權選項,您可以兩個工具的價格獲得全部九個 Iron 軟體工具。 祝您編碼愉快!

Mailkit Csharp Guide 2 related to 結論

常見問題解答

如何使用 MailKit 在 C# 中傳送電子郵件?

若要使用 C# 中的 MailKit 來傳送電子郵件,您需要建立一個 MimeMessage 物件,設定 SMTP 用戶端與伺服器的詳細資訊,並使用 SmtpClient.Send 方法來傳送電子郵件。

使用 MailKit 接收電子郵件的流程為何?

使用 MailKit 接收電子郵件涉及使用 ImapClient 連線至 IMAP 伺服器、使用使用者認證進行驗證,以及存取信箱以取得和管理電子郵件。

我可以使用 .NET 函式庫將電子郵件轉換為 PDF 嗎?

是的,您可以使用 IronPDF 透過渲染電子郵件的 HTML 內容,將電子郵件轉換成 PDF 檔案。IronPDF 提供了在 .NET 應用程式中無縫處理 PDF 轉換的方法。

在 MailKit 中使用 HTML 模板有哪些優點?

在 MailKit 中使用 HTML 範本可建立動態且視覺豐富的電子郵件內容,提升使用者的參與。範本可以從檔案或資源載入,並整合到電子郵件正文中。

如何使用 MailKit 處理電子郵件中的附件?

MailKit 可透過使用 BodyBuilder 類別將附件新增至 MimeMessage 來處理附件。您可以透過指定檔案路徑和 MIME 類型來附加檔案。

是否可以使用 IronPDF 執行電子郵件到 PDF 的轉換?

是的,IronPDF 提供電子郵件至 PDF 的轉換功能,允許開發人員將電子郵件內容渲染為 PDF。此功能對於歸檔電子郵件或建立可列印版本的電子郵件通訊非常有用。

IronPDF 這類 .NET PDF 函式庫的安裝流程為何?

若要安裝 IronPDF,請使用 Visual Studio 中的 NuGet 套件管理員搜尋並安裝「IronPDF」,或從網站下載 DLL 並將其手動新增至專案中。

MailKit 如何用於電子郵件排序與搜尋?

MailKit 支援收件匣內的用戶端電子郵件排序與搜尋,允許開發人員使用 IMAP 功能實作自訂排序條件與有效率的搜尋機制。

在 .NET 應用程式中結合 MailKit 和 IronPDF 有什麼好處?

結合 MailKit 和 IronPDF 可提供管理電子郵件通訊和文件處理的強大工具組。此整合可讓開發人員建立多功能的電子郵件用戶端,並能執行多樣化的任務,包括電子郵件至 PDF 的轉換。

如何排除 MailKit 中的 SMTP 連線問題?

若要排除 MailKit 中的 SMTP 連線問題,請驗證伺服器詳細資訊,例如位址、連接埠和憑證。確保網路允許 SMTP 流量,並檢查防火牆設定是否有可能阻擋。

Jacob Mellor, Team Iron 首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。

Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。

他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。