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("附件已被下载"); } } } }
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)); } }