public static Dictionary <string, string> GetPathByImage(MIME_Entity[] entitys) { Dictionary <string, string> dic = new Dictionary <string, string>(); string path = Directory.GetCurrentDirectory() + "\\SendingPic"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (var one in entitys) { if (one.ContentType.SubType.ToLower() == "jpeg" || one.ContentType.SubType.ToLower() == "gif" || one.ContentType.SubType.ToLower() == "png") { try { string savePath = string.Format("{0}\\{1}.jpg", path, Guid.NewGuid()); MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)one.Body; MemoryStream stream = new MemoryStream(byteObj.Data); System.Drawing.Image img = System.Drawing.Image.FromStream(stream); img.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); string key = one.ContentID.Replace("<", "cid:"); key = key.Replace(">", string.Empty); dic.Add(key, savePath); } catch (Exception ex) { } } } return(dic); }
private void button2_Click(object sender, EventArgs e) { int index = 0; if (listView1.SelectedItems.Count > 0) { index = this.listView1.SelectedItems[0].Index; string aa = listView1.Items[index].SubItems[0].Text; } for (int i = 0; i < mailnumber; i++) { if (index + 1 == mailreceive[i].num) { foreach (MIME_Entity entity in mailreceive[i].attachment) { DirectoryInfo dir = new DirectoryInfo(@"D:\"); if (!dir.Exists) { dir.Create(); } string path = Path.Combine(dir.FullName, mailreceive[i].filename); MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body; Stream decoded = byteObj.GetDataStream(); using (FileStream fs = new FileStream(path, FileMode.Create)) { LumiSoft.Net.Net_Utils.StreamCopy(decoded, fs, 4000); } liststatus.Items.Add("附件已被下载"); } } } }
public override Boolean GetMailAttachment(Int32 mailIndex, String receiveBackpath) { if (mailIndex == 0) { return(false); } else if (mailIndex > _mailTotalCount) { return(false); } else { try { byte[] messageBytes = _pop3MessageList[mailIndex - 1].MessageToByte(); Mail_Message mMessage = Mail_Message.ParseFromByte(messageBytes); if (mMessage == null) { return(false); } //LumiSoft.Net.Mail.Mail_Message MMessage = Mail_Message.ParseFromByte(pop3MessageList[mailIndex - 1].HeaderToByte()); foreach (MIME_Entity entity in mMessage.GetAttachments(true, true)) { if (entity.ContentDisposition != null && entity.ContentDisposition.Param_FileName != null) { String fileName = entity.ContentDisposition.Param_FileName; String fileFullName = receiveBackpath + "\\" + fileName; //FileInfo fileInfo = new FileInfo(fileFullName); //if (fileInfo.Exists) fileInfo.Delete(); MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body; if (byteObj != null) { FileInfo fileInfo = new FileInfo(fileFullName); FileStream fs = null; //声明一个文件流对象. fs = new FileStream(fileInfo.FullName, FileMode.Create); fs.Write(byteObj.Data, 0, byteObj.Data.Length); fs.Close(); //FileUtil.CreateFile(filePath, byteObj.Data); //fileSize = byteObj.Data.Length; //entity.ContentDisposition.DispositionType == MIME_DispositionTypes.Attachment return(true); } } } ErrorMessage = "获取附件失败,请重试!"; return(false); } catch (Exception ex) { ErrorMessage = ex.Message; return(false); } } }
public static List <Stream> GetEmlImageStream(MIME_Entity[] entitys) { List <Stream> streamList = new List <Stream>(); foreach (var one in entitys) { if (one.ContentType.SubType == "jpeg") { try { MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)one.Body; MemoryStream stream = new MemoryStream(byteObj.Data); streamList.Add(stream); } catch (Exception ex) { } } } return(streamList); }
public static void ConvertToMailMessage(this MailMessage msg, POP3_ClientMessage message) { Mail_Message mime_header = Mail_Message.ParseFromByte(message.HeaderToByte()); if (mime_header.From != null) { msg.From = new MailAddress(mime_header.From[0].Address, mime_header.From[0].DisplayName, Encoding.UTF8);; } if (mime_header.To != null) { foreach (Mail_t_Mailbox recipient in mime_header.To.Mailboxes) { msg.To.Add(new MailAddress(recipient.Address, recipient.DisplayName, Encoding.UTF8)); } } if (mime_header.Cc != null) { foreach (Mail_t_Mailbox recipient in mime_header.Cc.Mailboxes) { msg.CC.Add(new MailAddress(recipient.Address, recipient.DisplayName, Encoding.UTF8)); } } msg.Subject = mime_header.Subject; Mail_Message mime_message = Mail_Message.ParseFromByte(message.MessageToByte()); if (mime_message == null) { return; } msg.Body = mime_message.BodyText; //info.Body = mime_message.BodyText; try { if (!string.IsNullOrEmpty(mime_message.BodyHtmlText)) { msg.Body = mime_message.BodyHtmlText; } } catch { //屏蔽编码出现错误的问题,错误在BodyText存在而BodyHtmlText不存在的时候,访问BodyHtmlText会出现 } #region 邮件附件内容 foreach (MIME_Entity entity in mime_message.GetAttachments(true, true)) { if (entity.ContentDisposition != null && entity.ContentDisposition.Param_FileName != null) { if (entity.ContentDisposition.DispositionType == MIME_DispositionTypes.Attachment) { string fileName = entity.ContentDisposition.Param_FileName; MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body; if (byteObj != null) { //可将字节保存到文件 int fileSize = byteObj.Data.Length; } //msg.Attachments.Add(new Attachment(fileName, System.Net.Mime.MediaTypeNames.Application.Rtf)); } } } #endregion 邮件附件内容 }
private void ReceiveMail(string OApop3Server, Int32 OApop3ServerPort, string OAUsername, string OAUserPwd, string OutPath, string EmailSubject, string EmailAddress) { using (POP3_Client pop3 = new POP3_Client()) { try { pop3.Connect(OApop3Server, OApop3ServerPort, true); } catch (Exception ex) { } pop3.Connect(OApop3Server, OApop3ServerPort, true); pop3.Login(OAUsername, OAUserPwd);//两个参数,前者为Email的账号,后者为Email的密码 POP3_ClientMessageCollection messages = pop3.Messages; LogHelper.WriteLog(LogLevel.LOG_LEVEL_INFO, string.Format("共{0}封邮件", messages.Count), typeof(AMSDownloadForm)); for (int i = 0; i < messages.Count; i++) { POP3_ClientMessage message = messages[i];//转化为POP3 LogHelper.WriteLog(LogLevel.LOG_LEVEL_DEBUG, string.Format("正在检查第{0}封邮件...", i + 1), typeof(AMSDownloadForm)); if (message != null) { byte[] messageBytes = null; try { messageBytes = message.MessageToByte(); } catch (Exception ex) { LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, string.Format("第{0}封邮件,message.MessageToByte() 失败 错误日志参考下一条信息", i), typeof(AMSDownloadForm)); LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(AMSDownloadForm)); } Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes); string sender = mime_message.From == null ? "sender is null" : mime_message.From[0].DisplayName; string senderAddress = mime_message.From == null ? "senderAddress is null" : mime_message.From[0].Address; string subject = mime_message.Subject ?? "subject is null"; string recDate = mime_message.Date == DateTime.MinValue ? "date not specified" : mime_message.Date.ToString(); string content = mime_message.BodyText ?? "content is null"; LogHelper.WriteLog(LogLevel.LOG_LEVEL_DEBUG, string.Format("正在检查第{0}封邮件, 邮件发送时间{1}", i + 1, mime_message.Date), typeof(AMSDownloadForm)); //Console.WriteLine("内容为{0}", content); // 判断此邮件是否是需要下载的邮件 // 发件人 & 发件主体 && 收件时间 DateTime dt = DateTime.Now; if (string.Equals(senderAddress, EmailAddress) && string.Equals(subject, EmailSubject.Replace("yyyyMMdd", dt.ToString("yyyyMMdd"))) && mime_message.Date.Date == dt.Date) { MIME_Entity[] attachments = mime_message.GetAttachments(true, true); foreach (MIME_Entity entity in attachments) { if (entity.ContentDisposition != null) { string fileName = entity.ContentDisposition.Param_FileName; if (!string.IsNullOrEmpty(fileName)) { DirectoryInfo dir = new DirectoryInfo(OutPath); if (!dir.Exists) { dir.Create(); } DirectoryInfo dir2 = new DirectoryInfo(OutPath + @"\" + DateTime.Now.ToString("yyyyMMdd") + @"\"); if (!dir2.Exists) { dir2.Create(); } string path = Path.Combine(dir.FullName, fileName); MIME_b_SinglepartBase byteObj = (MIME_b_SinglepartBase)entity.Body; Stream decodedDataStream = byteObj.GetDataStream(); using (FileStream fs = new FileStream(path, FileMode.Create)) { LumiSoft.Net.Net_Utils.StreamCopy(decodedDataStream, fs, 4000); } LogHelper.WriteLog(LogLevel.LOG_LEVEL_INFO, string.Format("{0}已经被下载。", fileName), typeof(AMSDownloadForm)); // 下载完成之后解压 (new FastZip()).ExtractZip(dir.FullName + fileName, dir2.FullName, ""); if (dir2.GetFiles().Length > 0 && dir2.GetDirectories().Length > 0) { MessageDxUtil.ShowTips("下载解压文件成功"); } } } } if (MessageDxUtil.ShowTips("下载成功") == DialogResult.OK) { Process.Start("explorer.exe", OutPath); } break; } // 时间超过了也退出 if (mime_message.Date.Date < dt.Date) { MessageDxUtil.ShowTips("根网清算文件未到,请稍后处理"); break; } } } LogHelper.WriteLog(LogLevel.LOG_LEVEL_INFO, "执行结束", typeof(AMSDownloadForm)); } }