private void StartEmailDownload() { using (POP3_Client pop3 = new POP3_Client()) { pop3.Connect(this.qqEmailServer, 995, true); pop3.Login(this.emailAccount, this.password); POP3_ClientMessageCollection messages = pop3.Messages; Console.WriteLine("EmailCount:{0}", messages.Count); for (int i = 0; i < messages.Count; i++) { POP3_ClientMessage message = messages[i]; //转化为POP3 Console.WriteLine("\r\nChecking Email :{0} ...", i + 1); if (message != null) { byte[] messageBytes = message.MessageToByte(); Mail_Message mime_message = Mail_Message.ParseFromByte(messageBytes); string senderAddress = mime_message.From == null ? "<NULL>" : mime_message.From[0].Address; string subject = mime_message.Subject ?? "<NULL>"; Console.WriteLine(subject); DirectoryInfo dir = new DirectoryInfo(this.savePath); if (!dir.Exists) dir.Create(); MIME_Entity[] attachments = mime_message.GetAttachments(true); foreach (MIME_Entity entity in attachments) { if (entity.ContentDisposition != null) { string fileName = entity.ContentDisposition.Param_FileName; string extension = new FileInfo(fileName).Extension; if (!string.IsNullOrEmpty(fileName) && extension == ".pdf") { 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); } } } } } } } }
/// <summary> /// Reads the mail. /// </summary> public MailboxReaderResult ReadMail() { var result = new MailboxReaderResult(); IList<Project> projects = new List<Project>(); LogInfo("MailboxReader: Begin read mail."); try { using (var pop3Client = new POP3_Client()) { // configure the logger pop3Client.Logger = new Logger(); pop3Client.Logger.WriteLog += LogPop3Client; // connect to the server pop3Client.Connect(Config.Server, Config.Port, Config.UseSsl); // authenticate pop3Client.Login(Config.Username, Config.Password); // process the messages on the server foreach (POP3_ClientMessage message in pop3Client.Messages) { var mailHeader = Mail_Message.ParseFromByte(message.HeaderToByte()); if (mailHeader != null) { var recipients = mailHeader.To.Mailboxes.Select(mailbox => mailbox.Address).ToList(); if (mailHeader.Cc != null) { recipients.AddRange(mailHeader.Cc.Mailboxes.Select(mailbox => mailbox.Address)); } if (mailHeader.Bcc != null) { recipients.AddRange(mailHeader.Bcc.Mailboxes.Select(mailbox => mailbox.Address)); } // first check if this is a comment (comments are implemented using plus addressing) // a comment will have a replyto address like [email]+iid-[number]@domain.com bool isProcessed = false; if (HostSettingManager.Get<bool>(HostSettingNames.Pop3AllowReplyToEmail, false)) { isProcessed = ProcessNewComment(recipients, message, mailHeader, result); } if (!isProcessed) { isProcessed = ProcessNewIssue(recipients, message, mailHeader, projects, result); } if (isProcessed) { LogInfo(string.Format( "MailboxReader: Message #{0} processing finished, found [{1}] attachments, total saved [{2}].", message.SequenceNumber, 0, 0)); try { // delete the message?. if (Config.DeleteAllMessages) { message.MarkForDeletion(); } } catch (Exception) { } } else { LogWarning(string.Format("pop3Client: Message #{0} header could not be parsed.", message.SequenceNumber)); } } } } } catch (Exception ex) { LogException(ex); result.LastException = ex; result.Status = ResultStatuses.FailedWithException; } LogInfo("MailboxReader: End read mail."); return result; }
static void pop3() { List<string> gotemailids = new List<string>(); using (LumiSoft.Net.POP3.Client.POP3_Client pop3 = new POP3_Client()) { try { //与pop3服务器建立连接 pop3.Connect("pop.qq.com", 110, false); //验证身份 pop3.Login("*****@*****.**", "myhaiyan"); //获取邮件信息列表 POP3_ClientMessageCollection infos = pop3.Messages; foreach (POP3_ClientMessage info in infos) { //每封email会有一个在pop3服务器范围内唯一的id,检查这个id是否存在就可以知道以前有没有接收过这封邮件 if (gotemailids.Contains(info.UID)) continue; //获取这封邮件的内容 byte[] bytes = info.MessageToByte(); //记录这封邮件的id gotemailids.Add(info.UID); //解析从pop3服务器发送过来的邮件信息 Mime m = Mime.Parse(bytes); if (m != null) { string mailfrom = ""; string mailfromname = ""; if (m.MainEntity.From != null) { for (int i = 0; i < m.MainEntity.From.Mailboxes.Length; i++) { if (i == 0) { mailfrom = (m.MainEntity.From).Mailboxes[i].EmailAddress; } else { mailfrom += string.Format(",{0}", (m.MainEntity.From).Mailboxes[i].EmailAddress); } mailfromname = (m.MainEntity.From).Mailboxes[0].DisplayName != "" ? (m.MainEntity.From).Mailboxes[0].DisplayName : (m.MainEntity.From).Mailboxes[0].LocalPart; } } string mailto = ""; string mailtotocollection = ""; if (m.MainEntity.To != null) { mailtotocollection = m.MainEntity.To.ToAddressListString(); for (int i = 0; i < m.MainEntity.To.Mailboxes.Length; i++) { if (i == 0) { mailto = (m.MainEntity.To).Mailboxes[i].EmailAddress; } else { mailto += string.Format(",{0}", (m.MainEntity.To).Mailboxes[i].EmailAddress); } } } } //获取附件 foreach (MimeEntity entry in m.Attachments) { string filename = entry.ContentDisposition_FileName; //获取文件名称 string path = AppDomain.CurrentDomain.BaseDirectory + @"attch\" + filename; if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"attch")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"attch"); } if (File.Exists(path)) { Random random = new Random(); int newfile = random.Next(1, 100000); path = AppDomain.CurrentDomain.BaseDirectory + @"attch\" + newfile.ToString(); Directory.CreateDirectory(path); path += @"\" + filename; } byte[] data = entry.Data; FileStream pfilestream = null; pfilestream = new FileStream(path, FileMode.Create); pfilestream.Write(data, 0, data.Length); pfilestream.Close(); } } } catch (Exception ex) { } } }
public override void init() { using (POP3_Client popClient = new POP3_Client()) { popClient.Connect(m_host, m_port); popClient.Login(m_user, m_pwd); foreach (POP3_ClientMessage message in popClient.Messages) { LumiSoft.Net.Mail.Mail_Message mime_header = LumiSoft.Net.Mail.Mail_Message.ParseFromByte(message.HeaderToByte()); if (mime_header != null && m_Filter(mime_header.Subject, null)) { m_htmlBody = mime_header.BodyHtmlText; return; } } } }