JMail接受邮件

 

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();
        }


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值