使用LumiSoft.NET接收邮件

http://www.lumisoft.ee/lsWWW/download/downloads/Net/info.txt

先把获取方法贴上,SVN地址及只读帐号

https://svn.lumisoft.ee:8443/svn/
User:     readonly
Password: readonly

读取邮件方法,其中MailEntity为自定义实体,不必受它影响

        public IList<MailEntity> Receive( AccountEntity item ) {
            if( null == item || item == AccountEntity.Empty ) throw new ArgumentNullException( "参数不可为空!" );

            IList<MailEntity> list = new List<MailEntity>();
            using( POP3_Client client = new POP3_Client() ) {
                client.Connect( item.POP3Address, item.POP3Port, item.POP3IsUsingSSL );  //连接
                client.Authenticate( item.Username, item.Password, false );           //验证
                if( client.Messages != null && client.Messages.Count > 0 ) {
                    foreach( POP3_ClientMessage message in client.Messages ) {
                        MailEntity entity = _Convert( message );
                        entity.Account = item;
                        list.Add( entity );
                    }
                }
            }
            return list;
        }
 
下边的代码基本都是从LumiSoft.NET定义的实体转换成我自己定义的实体的一些方法,包括邮件附件,备注下~
#region private methods
        private MailEntity _Convert( POP3_ClientMessage message ) {
            MailEntity entity = new MailEntity();
            Mail_Message mime = Mail_Message.ParseFromByte( message.MessageToByte() );

            #region From,To,Cc,Bcc
            if( null != mime.From ) {
                entity.MessageEntity.From = mime.From.ToString();
            }
            else {
                entity.MessageEntity.From = None;
            }

            if( null != mime.To ) {
                entity.MessageEntity.To = mime.To.ToString();
            }
            else {
                entity.MessageEntity.To = None;
            }

            if( null != mime.Cc ) {
                entity.MessageEntity.Cc = mime.Cc.ToString();
            }
            else {
                entity.MessageEntity.Cc = None;
            }

            if( null != mime.Bcc ) {
                entity.MessageEntity.Bcc = mime.Bcc.ToString();
            }
            else {
                entity.MessageEntity.Bcc = None;
            }
            #endregion
            entity.MessageEntity.Size = ( (int)( message.Size / (int)1000 ) );
            entity.MessageEntity.CreateDate = mime.Date;
            entity.MessageEntity.MessageID = mime.MessageID;
            entity.MessageEntity.Subject = mime.Subject ?? None;
            entity.MessageEntity.HtmlBody = mime.BodyHtmlText ?? None;
            entity.MessageEntity.ID = Guid.NewGuid().ToString();
            if( mime.Attachments != null ) {
                foreach( var item in mime.Attachments ) {
                    AttachmentEntity _attachment = _Convert( item );
                    if( _attachment != null ) {
                        _attachment.MessageID = entity.MessageEntity.ID;
                        if( entity != null ) {
                            _attachment.URL = _SaveMail( _attachment, item );
                            entity.Attachments.Add( _attachment );
                        }
                    }
                }
            }
            return entity;
        }

        private AttachmentEntity _Convert( MIME_Entity mime ) {
            if( mime.ContentDisposition != null && mime.ContentDisposition.Param_FileName != null ) {
                AttachmentEntity _entity = new AttachmentEntity();
                _entity.Name = mime.ContentDisposition.Param_FileName;
                _entity.ID = Guid.NewGuid().ToString();
                _entity.URL = mime.ContentDisposition.Param_FileName;
                return _entity;
            }
            return null;
        }

        private string _SaveMail( AttachmentEntity e, MIME_Entity attach ) {
            if( e == null ) throw new ArgumentNullException( "参数不可为空!" );
            if( attach == null ) throw new ArgumentNullException( "附件为空!" );

            string _path = string.Empty;
            _path = Path.Combine( Application.StartupPath, "db" );
            _path = Path.Combine( _path, e.MessageID );
            if( !Directory.Exists( _path ) ) Directory.CreateDirectory( _path );

            string fileName = Path.Combine( _path, e.Name );
            if( File.Exists( fileName ) ) File.Delete( fileName );
            try {
                File.WriteAllBytes( fileName, ( (MIME_b_SinglepartBase)attach.Body ).Data );
            }
            catch( Exception exp ) {
                return string.Empty;
            }
            return fileName;
        }
        #endregion
贴张图晒晒
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值