使用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
贴张图晒晒
------------------------------------------
除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
BUY ME COFFEE
![](https://images.cnblogs.com/cnblogs_com/kkun/212569/o_200616035329WechatIMG237.png)
![](https://images.cnblogs.com/cnblogs_com/kkun/212569/o_200616035334WechatIMG238.jpeg)