/// <summary> /// Default constructor. /// </summary> /// <param name="pop3">Owner POP3 client.</param> internal POP3_ClientMessageCollection(POP3_Client pop3) { m_pPop3Client = pop3; m_pMessages = new List<POP3_ClientMessage>(); }
/// <summary> /// Starts operation processing. /// </summary> /// <param name="owner">Owner POP3 client.</param> /// <returns>Returns true if asynchronous operation in progress or false if operation completed synchronously.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b> is null reference.</exception> internal bool Start(POP3_Client owner) { if(owner == null){ throw new ArgumentNullException("owner"); } m_pPop3Client = owner; SetState(AsyncOP_State.Active); try{ /* RFC 5034 4. The AUTH Command. AUTH mechanism [initial-response] Arguments: mechanism: A string identifying a SASL authentication mechanism. initial-response: An optional initial client response, as defined in Section 3 of [RFC4422]. If present, this response MUST be encoded as Base64 (specified in Section 4 of [RFC4648]), or consist only of the single character "=", which represents an empty initial response. */ if(m_pSASL.SupportsInitialResponse){ byte[] buffer = Encoding.UTF8.GetBytes("AUTH " + m_pSASL.Name + " " + Convert.ToBase64String(m_pSASL.Continue(null)) + "\r\n"); // Log m_pPop3Client.LogAddWrite(buffer.Length,Encoding.UTF8.GetString(buffer).TrimEnd()); // Start command sending. m_pPop3Client.TcpStream.BeginWrite(buffer,0,buffer.Length,this.AuthCommandSendingCompleted,null); } else{ byte[] buffer = Encoding.UTF8.GetBytes("AUTH " + m_pSASL.Name + "\r\n"); // Log m_pPop3Client.LogAddWrite(buffer.Length,"AUTH " + m_pSASL.Name); // Start command sending. m_pPop3Client.TcpStream.BeginWrite(buffer,0,buffer.Length,this.AuthCommandSendingCompleted,null); } } catch(Exception x){ m_pException = x; m_pPop3Client.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); } // Set flag rise CompletedAsync event flag. The event is raised when async op completes. // If already completed sync, that flag has no effect. lock(m_pLock){ m_RiseCompleted = true; return m_State == AsyncOP_State.Active; } }
/// <summary> /// Disposes message. /// </summary> internal void Dispose() { if(m_IsDisposed){ return; } m_IsDisposed = true; m_Pop3Client = null; }
private void pop3RecvMail() { using (var pop3_client = new POP3_Client()) { pop3_client.Connect(mailAccount.recv_server, mailAccount.recv_port, mailAccount.recv_ssl); pop3_client.Login(mailAccount.account, mailAccount.password); int total = pop3_client.Messages.Count; workInfo.SetInfo("正在接收邮件列表... 请稍后"); List<POP3Mail> mails = new List<POP3Mail>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < pop3_client.Messages.Count; i++) { if (isStop) return; workInfo.SetProgress(total, i); POP3_ClientMessage pop3m = pop3_client.Messages[i]; try { if (uids.Contains(pop3m.UID)) continue; //--通知其他订阅人--// XElement el = new XElement("subscribe"); el.SetAttributeValue("total", total); el.SetAttributeValue("count", i); el.SetAttributeValue("account", mailAccount.account); el.SetAttributeValue("type", "accept_mail"); el.SetAttributeValue("detail", "false"); el.SetAttributeValue("principal", Desktop.instance.loginedPrincipal.loginId); el.SetAttributeValue("principal_name", Desktop.instance.loginedPrincipal.name); el.SetAttributeValue("subject", "正在接收邮件列表..."); MessageManager.instance.publishMessage(mailAccount.pubId, "subscribe", el.ToString()); //-----------// string charset = "GBK"; if (pop3m.HeaderToByte() == null) return; Mail_Message m = Mail_Message.ParseFromByte(pop3m.HeaderToByte()); //过滤黑名单 if (m.From != null && m.From.Count > 0) { if (MailWorker.instance.IsExistBlack(m.From[0].Address)) continue; } if (m.ContentType != null && m.ContentType.Param_Charset != null) charset = m.ContentType.Param_Charset; if (m.Date == DateTime.MinValue && IsMailExists(mailAccount.account, pop3m.UID)) { continue; } else if (m.Date < uidBeginTime) { continue; } POP3Mail mail = new POP3Mail(); if (m.Subject != null && m.Subject.IndexOf("&#") != -1) m.Subject = HttpUtility.HtmlDecode(m.Subject); mail.subject = m.Subject; mail.message = pop3m; mail.charset = charset; mails.Add(mail); } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.StackTrace); workInfo.AddDetail("邮件处理错误:" + ex.Message + ", uid = " + pop3m.UID, Colors.Red); hasError = true; } } if (mails.Count == 0) return; workInfo.SetInfo("正在接收邮件... 请稍后"); int count = 0; total = mails.Count; foreach (POP3Mail mail in mails) { if (isStop) return; workInfo.SetProgress(total, count); count++; try { workInfo.AddDetail("正在接收邮件:" + mail.subject, Colors.Black); //--通知其他订阅人--// XElement el = new XElement("subscribe"); el.SetAttributeValue("total", total); el.SetAttributeValue("count", count); el.SetAttributeValue("account", mailAccount.account); el.SetAttributeValue("type", "accept_mail"); el.SetAttributeValue("detail", "true"); el.SetAttributeValue("principal", Desktop.instance.loginedPrincipal.loginId); el.SetAttributeValue("principal_name", Desktop.instance.loginedPrincipal.name); el.SetAttributeValue("subject", "正在接收邮件:" + mail.subject); MessageManager.instance.publishMessage(mailAccount.pubId, "subscribe", el.ToString()); //-----------// string uid = mail.message.UID; sb.Length = 0; sb.Append(getFilePath(uid)).Append("/").Append(uid).Append(".eml"); string file = sb.ToString(); DirectoryInfo dir = Directory.GetParent(store_path + file); if (!dir.Exists) dir.Create(); using (FileStream fs = new FileStreamWrap(store_path + file, mail.message.Size, workInfo, mailAccount.pubId)) { mail.message.MessageToStream(fs); } Mail_Message m = null; using (FileStream fs = new FileStream(store_path + file, FileMode.Open)) { m = Mail_Message.ParseFromStream(fs, Encoding.GetEncoding(mail.charset)); } ASObject record = saveMail(m, uid, uid, file); try { MailWorker.instance.saveMailRecord(record, "ML_Mail_Temp"); try { //唤醒Syncworker SyncWorker.instance.Notify(total); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.StackTrace); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace); workInfo.AddDetail("邮件处理错误:" + ex.Message + ", uid = " + mail.message.UID, Colors.Red); hasError = true; } } if (mails.Count > 0) { SyncWorker.instance.WorkInfo = workInfo; if (SyncWorker.instance.Thread != null) SyncWorker.instance.Thread.Join(); } } }
/// <summary> /// Default constructor. /// </summary> /// <param name="pop3">Owner POP3 client.</param> /// <param name="seqNumber">Message 1 based sequence number.</param> /// <param name="size">Message size in bytes.</param> internal POP3_ClientMessage(POP3_Client pop3,int seqNumber,int size) { m_Pop3Client = pop3; m_SequenceNumber = seqNumber; m_Size = size; }