MailBee.NET Objects接收电子邮件(POP3)教程一:接收简单的电子邮件


发布者 Harriet666  发布时间 1497516049930
关键字 分享 
MailBee.NET Objects是一款为创建、发送、接收以及处理电子邮件而设计的健壮、功能丰富的.NET控件。几行代码便可为应用程序添加E-Mail支持,简单高效。具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加到他们的应用程序中。
 
 
本文主要介绍了接收电子邮件(POP3)的代码示例。目前MailBee.NET Objects在线订购享75折优惠正在进行中,欢迎您下载试用版进行运用!

要接收简单的电子邮件,开发人员应使用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")
 
由于所有POP3服务器都需要身份验证,开发人员应指定登录邮箱的登录名和密码,如下所示:
C#:
pop.Login("login", "password");
VB.NET:
pop.Login("login", "password")
 
当开发人员成功登录邮箱时,可以通过调用POP3对象的DownloadEntireMessage方法轻松下载此邮箱包含的任何邮件。调用此方法时,开发人员应指定邮件的索引。例如,以下代码行表示从收件箱下载最后一封邮件:
C#:
MailMessage msg = pop.DownloadEntireMessage(pop.InboxMessageCount);
VB.NET:
Dim msg As MailMessage =  pop.DownloadEntireMessage(pop.InboxMessageCount)
 
其中:
  • pop.InboxMessageCount是一个属性,包含邮箱中存储的邮件总数;
  • msg是一个MailMessage对象,表示单个电子邮件。
注意!如果邮箱中没有邮件,则pop.InboxMessageCount属性为0并调用pop.DownloadEntireMessage(pop.InboxMessageCount)方法将出现错误。
 
如果要从邮箱下载中下载新邮件,开发者必须定义特殊的帮助函数。该功能应该查找已经下载邮件的现有数据库中邮件的UID。如果没有找到指定的UID,则将该邮件视为新邮件。因此,开发人员必须使用数据库引擎来存储所有接收到的邮件UID。
 
最后,当邮箱完成所有必要的操作,并且与POP3服务器的连接无效时,开发人员应该调用POP3对象的Disconnect方法以断开与POP3服务器的连接,如下所示:
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
 
以上就是本次教程的全部内容,接下来会有更多相关教程,敬请关注!您也可以在评论者留下你的经验和建议。

试用、下载、了解更多产品信息请点击"咨询在线客服"   









  开源的 OurJS
OurJS开源博客已经迁移到 OnceOA 平台。

  关注我们
扫一扫即可关注我们:
OnceJS

OnceOA