Spire.Email 教程:在C#,VB.NET中搜索电子邮件

时间:2022-11-22 14:00:26

Spire.Email允许开发人员搜索符合给定搜索条件的电子邮件的邮箱,本文介绍了如何使用Spire.Email组件搜索电子邮件。

详细步骤:

Step 1:创建一个ImapClient实例。

ImapClient imap = new ImapClient();

Step 2:设置主机,端口,认证和连接协议。

imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;

Step 3:连接imap服务器。

imap.Connect();

Step 4:选择收件箱文件夹。

imap.Select("Inbox");

Step 5:搜索符合搜索条件的文件夹中的电子邮件。

//Search email messages sent from “Alice”
ImapMessageCollection messages = imap.Search("'From' Contains 'Alice'");
Console.WriteLine("Number of messages sent from Alice: " + messages.Count);
//Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'");
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count);

截图:

Spire.Email 教程:在C#,VB.NET中搜索电子邮件

完整代码:

[C#]

//Create an ImapClient instance
ImapClient imap = new ImapClient();
//Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;
//Connect the imap server
imap.Connect();
//Select Inbox folder
imap.Select("Inbox");
//Search email messages sent from "Alice"
ImapMessageCollection messages = imap.Search("'From' Contains 'Alice'");
Console.WriteLine("Number of messages sent from Alice: " + messages.Count);
//Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'");
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count);

[VB.NET]

'Create an ImapClient instance
Dim imap As New ImapClient()
'Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com"
imap.Port = 143
imap.Username = "LeonDavisLD@outlook.com"
imap.Password = "password"
imap.ConnectionProtocols = ConnectionProtocols.Ssl
'Connect the imap server
imap.Connect()
'Select Inbox folder
imap.[Select]("Inbox")
'Search email messages sent from "Alice"
Dim messages As ImapMessageCollection = imap.Search("'From' Contains 'Alice'")
Console.WriteLine("Number of messages sent from Alice: " + messages.Count)
'Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'")
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count)

慧都控件网