Code Bye

Lumisoft如何将附件Attachment保存到本地

Lumisoft 获取邮件附件时,使用mime.ContentDisposition.Param_FileName只能得到文件名,无法获得文件路径,因为存储的是文件流。 那么如何将附件保存在本地呢?如下是实例代码:
        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;
        }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Lumisoft如何将附件Attachment保存到本地