最近因为不是太忙,所以就心血来潮的用Lumisoft写了个Web端的邮件收发系统。在此记录开发历程。
一、首先先介绍下这个系统的基本思路:把邮件从服务器下载下来然后保存到本地,查看邮件的时候再加载邮件信息。这里,都是用XML,来存储邮件的基本信息。
二、用到的类
Mail_Item类记录邮件的基本信息
Person类记录的是邮件用户名及名称
Attachment 类记录附件的基本信息
Mail_Summary 邮件信息概要类,用于显示邮件列表
User 用户信息类,记录用户信息
/// <summary>
/// 邮件的基本信息
/// </summary>
public class Mail_Item
{
/// <summary>
/// The unique mail Identity in Mail Server
/// </summary>
public string Mail_Id { get; set; }
/// <summary>
/// Mail sender
/// </summary>
public Person Mail_From { get; set; }
/// <summary>
/// Mail Receiver
/// </summary>
public List<Person> Mail_To = new List<Person>();
/// <summary>
/// Mail copy to
/// </summary>
public List<Person> Mail_CopyTo = new List<Person>();
public string Mail_Subject { get; set; }
/// <summary>
/// Mail content path in localhost
/// </summary>
public string Mail_Body { get; set; }
/// <summary>
/// Mail receive date
/// </summary>
public string Mail_Date { get; set; }
/// <summary>
/// Mail attachments
/// </summary>
public List<Attachment> Mail_Attachment = new List<Attachment>();
/// <summary>
/// mail is read or unread
/// </summary>
public bool IsRead { get; set; }
public bool IsDeleted { get; set; }
}
三、接收邮件
主要步骤有:登录服务器-->判断当前邮件在系统中是否已经存在了->下载邮件,并且保存邮件的基本信息
接收邮件代码:
public static List<Mail_Item> ReceiveMail(User user, out int mailNum)
{
string baseBodyPath = @"Mails\" + user.Server + @"\" + user.Identity + @"\Mails\";
string baseAttPath = @"Mails\" + user.Server + @"\" + user.Identity + @"\Attachments\";
CreateFoder(user);
mailNum = 0;
string _mailServer = user.Server;
string _isHelpAccount = user.Account;
string _isHelpPassword = user.Password;
List<Mail_Item> mails = new List<Mail_Item>();
if (string.IsNullOrEmpty(_mailServer) || string.IsNullOrEmpty(_isHelpAccount) || string.IsNullOrEmpty(_isHelpPassword))
{
return mails;
}
string _mailId = string.Empty;
string _subject = string.Empty; //主题
string _body = string.Empty; //正文
string _receiveDate = string.Empty;//接收时间
// Int64 maxUID = GetXmkUserInfo(_mailServer, _isHelpAccount, _isHelpPassword).MaxMailID; //获取上一次接收邮件最大的ID
//if (maxUID < 0)
//{
// return mails;
//}
using (POP3_Client pop = new POP3_Client())
{
try
{
pop.Connect(_mailServer, 110, false);
pop.Login(_isHelpAccount, _isHelpPassword);
POP3_ClientMessageCollection messages = pop.Messages;
//1415170251.6684.Qmail,S=866235:第一部分是递增的
//最后一份邮件都不是最新的,要提示
// _mailId = messages[messages.Count - 1].UID;
for (int i = messages.Count - 1; 0 <= i; i--)
{
_mailId = message