public class JMailService
{
public delegate void ReceivedMail();
public event ReceivedMail OnReceivedMail;
string userName = string.Empty;
string password = string.Empty;
string pop3MailServer = string.Empty;
int port = 110;
string filePath = string.Empty;
int mailNumber = 0;
int mailSize = 0;
Pop3MailCollection pop3Mails;
#region Attribute
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get
{
return this.userName;
}
set
{
this.userName = value;
}
}
/// <summary>
/// 密码
/// </summary>
public string Password
{
get
{
return this.password;
}
set
{
this.password = value;
}
}
/// <summary>
/// POP3邮件服务器
/// </summary>
public string Pop3MailServer
{
get
{
return this.pop3MailServer;
}
set
{
this.pop3MailServer = value;
}
}
/// <summary>
/// POP3邮件服务器端口
/// </summary>
public int Port
{
get
{
return this.port;
}
set
{
this.port = value;
}
}
/// <summary>
/// 附件文件存放路径
/// </summary>
public string FilePath
{
get
{
return this.filePath;
}
set
{
this.filePath = value;
}
}
/// <summary>
/// 邮件数量
/// </summary>
public int MailNumber
{
get
{
return this.mailNumber;
}
}
/// <summary>
/// 邮件大小
/// </summary>
public int MailSize
{
get
{
return this.mailSize;
}
}
/// <summary>
/// 邮件数据集
/// </summary>
public Pop3MailCollection Pop3Mails
{
get
{
return this.pop3Mails;
}
set
{
this.pop3Mails = value;
}
}
#endregion
public JMailService()
{
pop3Mails = new Pop3MailCollection();
}
/// <summary>
/// 接收邮件
/// </summary>
public void ReceiveMails()
{
jmail.POP3Class popMail = new jmail.POP3Class();
jmail.Message mailMessage;
jmail.Attachments atts;
jmail.Attachment att;
popMail.Connect(userName, password, pop3MailServer, port);
if (popMail.Count > 0)
{
this.mailNumber =popMail.Count;
for(int i=0;i<popMail.Count;i++)
{
mailMessage = popMail.Messages[i];
atts = mailMessage.Attachments;
mailMessage.Charset = "GB2312";
mailMessage.Encoding = "Base64";
mailMessage.ISOEncodeHeaders = false;
Pop3Mail pop3Mail = new Pop3Mail();
pop3Mail.Date = mailMessage.Date;
pop3Mail.From = mailMessage.From;
//pop3Mail.To = mailMessage;
pop3Mail.Subject = mailMessage.Subject;
//pop3Mail.OriginateIp = mailMessage;
pop3Mail.Body = mailMessage.Body;
if (atts.Count > 0)
{
for (int j = 0; j < atts.Count; j++)
{
att = atts[j];
string attFileName = att.Name;
string attFilePath = filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + attFileName;
att.SaveToFile(attFilePath);
pop3Mail.AttachmentFileName += attFileName + ";";
pop3Mail.AttachmentFilePath += attFilePath + ";";
}
if (pop3Mail.AttachmentFileName.Length > 0)
{
pop3Mail.AttachmentFileName = pop3Mail.AttachmentFileName.Substring(0,pop3Mail.AttachmentFileName.Length -1);
pop3Mail.AttachmentFilePath = pop3Mail.AttachmentFilePath.Substring(0,pop3Mail.AttachmentFilePath.Length -1);
}
}
pop3Mails.Add(pop3Mail);
}
if (OnReceivedMail != null)
this.OnReceivedMail();
popMail.DeleteMessages();
}
popMail.Disconnect();
}
}