要接收简单的电子邮件,开发人员应使用POP3对象。创建此对象的新实例,如下所示:
C#:Pop3 pop = new Pop3(); |
VB.NET: Dim pop As Pop3 = New Pop3() |
基本属性和方法
要接收电子邮件,MailBee.NET Obiects与POP3服务器通信。要连接到POP3服务器,开发人员只需指定此POP3服务器的主机名(或相同的IP地址),如下所示:C#:pop.Connect("mail.domain.com"); |
VB.NET: pop.Connect("mail.domain.com") |
C#:pop.Connect("127.0.0.1"); |
VB.NET: pop.Connect("127.0.0.1") |
C#:pop.Login("login", "password"); |
VB.NET: pop.Login("login", "password") |
C#:MailMessage msg = pop.DownloadEntireMessage(pop.InboxMessageCount); |
VB.NET: Dim msg As MailMessage = pop.DownloadEntireMessage(pop.InboxMessageCount) |
- pop.InboxMessageCount是一个属性,包含邮箱中存储的邮件总数;
- msg是一个MailMessage对象,表示单个电子邮件。
C#:pop.Disconnect(); |
VB.NET: pop.Disconnect() |
示例代码:
以下示例从指定的邮箱中下载最新的邮件,并显示此邮件的正文。 在使用MailBee.NET Objects之前,请确保它已解锁。
C#:using System; using MailBee; using MailBee.Pop3Mail; using MailBee.Mime;namespace EmailApp { class Class1 { [STAThread] static bool IsNewMessage(string UID) { return true; } static void Main(string[] args) { Pop3 pop = new Pop3(); try { pop.Connect("mail.domain.com"); pop.Login("login", "password"); Console.WriteLine("Successfully logged in."); } catch(MailBeePop3LoginNegativeResponseException) { Console.WriteLine("POP3 server replied with a negative response at login."); } string[] arrIDs = pop.GetMessageUids(); int n = pop.InboxMessageCount; if (IsNewMessage(arrIDs[n])) { MailMessage msg = pop.DownloadEntireMessage(n); if (msg.BodyHtmlText != "") Console.WriteLine(msg.BodyHtmlText); else if (msg.BodyPlainText != "") Console.WriteLine(msg.BodyPlainText); else Console.WriteLine("The body of this message is empty."); } try { pop.Disconnect(); Console.WriteLine("Disconnected successfully."); } catch { Console.WriteLine("Disconnection failed."); } } } } |
VB.NET: Imports System Imports MailBee Imports MailBee.Pop3Mail Imports MailBee.Mime Namespace EmailApp Class Class1 _ Shared Function IsNewMessage(ByVal UID As String) As Boolean Return True End Function Shared Sub Main(ByVal args() As String) Dim pop As Pop3 = New Pop3() Try pop.Connect("mail.domain.com") pop.Login("login", "password") Console.WriteLine("Successfully logged in.") Catch Console.WriteLine("POP3 server replied with a negative response at login.") End Try Dim arrIDs() As String = pop.GetMessageUids() Dim n As Integer = pop.InboxMessageCount If IsNewMessage(arrIDs(n)) Then Dim msg As MailMessage = pop.DownloadEntireMessage(n) If msg.BodyHtmlText <> "" Then Console.WriteLine(msg.BodyHtmlText) Else If msg.BodyPlainText <> "" Then Console.WriteLine(msg.BodyPlainText) Else Console.WriteLine("The body of this message is empty.") End If End If End If Try pop.Disconnect() Console.WriteLine("Disconnected successfully.") Catch Console.WriteLine("Disconnection failed.") End Try End Sub End Class End Namespace |