跳過到頁腳內容
.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 的起價低至$999 。 若要節省更多費用,請參閱 Iron軟體套件授權選項,您可以兩個工具的價格獲得全部九個 Iron 軟體工具。 祝您編碼愉快!

Mailkit Csharp Guide 2 related to 結論

常見問題解答

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

要在 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將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

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

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我