[csharp] view plain copy
- //開始循環取郵件資料
- m_pImap.Fetch(
- false,
- IMAP_t_SeqSet.Parse("1:*"),
- new IMAP_t_Fetch_i[]{
- new IMAP_t_Fetch_i_Envelope(),
- new IMAP_t_Fetch_i_Flags(),
- new IMAP_t_Fetch_i_InternalDate(),
- new IMAP_t_Fetch_i_Rfc822Size(),
- new IMAP_t_Fetch_i_Uid(),
- new IMAP_t_Fetch_i_Rfc822()
- },
- this.m_pImap_Fetch_MessageItems_UntaggedResponse
上面的代碼是直接收取郵件,包括頭字段和郵件正文。
- new IMAP_t_Fetch_i_Rfc822()
注意,這句話是表示接收郵件的正文,而我經過測試,發現這段代碼在接收yahoo郵箱的時候,郵件不能收取完全,是以在收取其他郵箱伺服器的時候,可以采用上面的代碼進行整體收件。
是以可以改成下面的這段代碼
- if(ip=="apple.imap.mail.yahoo.com")
- {
- //開始循環取郵件資料
- true,
- IMAP_t_SeqSet.Parse("1:*"),
- new IMAP_t_Fetch_i[]{
- new IMAP_t_Fetch_i_Envelope(),
- new IMAP_t_Fetch_i_Flags(),
- new IMAP_t_Fetch_i_InternalDate(),
- new IMAP_t_Fetch_i_Rfc822Size(),
- new IMAP_t_Fetch_i_Uid(),
- //new IMAP_t_Fetch_i_Rfc822()
- },
- this.m_pImap_Fetch_MessageItems_UntaggedResponseyahoo
- );
這裡實際上就沒有采用直接收取正文的方式,實際上,這裡隻是收取了頭字段。那麼應該在其他地方繼續寫收件的代碼。
- //調用讀取郵件函數
- private void LoadMessage(long uid)
- {
- this.Cursor = Cursors.WaitCursor;
- try{
- // Start fetching.
- m_pImap.Fetch(
- true,
- IMAP_t_SeqSet.Parse(uid.ToString()),
- new IMAP_t_Fetch_i[]{
- new IMAP_t_Fetch_i_Rfc822()
- },
- this.m_pImap_Fetch_Message_UntaggedResponse
- );
- }
- catch(Exception x){
- MessageBox.Show(this,"Error: " + x.ToString(),"Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Cursor = Cursors.Default;
- }
- //郵件讀取回調函數
- private void m_pImap_Fetch_Message_UntaggedResponse(object sender,EventArgs<IMAP_r_u> e)
- {
- /* NOTE: All IMAP untagged responses may be raised from thread pool thread,
- so all UI operations must use Invoke.
- There may be other untagged responses than FETCH, because IMAP server
- may send any untagged response to any command.
- */
- if(e.Value is IMAP_r_u_Fetch){
- IMAP_r_u_Fetch fetchResp = (IMAP_r_u_Fetch)e.Value;
- this.BeginInvoke(new MethodInvoker(delegate(){
- try{
- fetchResp.Rfc822.Stream.Position = 0;
- Mail_Message mime = Mail_Message.ParseFromStream(fetchResp.Rfc822.Stream);
- fetchResp.Rfc822.Stream.Dispose();
- //m_pTabPageMail_MessagesToolbar.Items["save"].Enabled = true;
- //m_pTabPageMail_MessagesToolbar.Items["delete"].Enabled = true;
- //m_pTabPageMail_MessageAttachments.Tag = mime;
- foreach(MIME_Entity entity in mime.Attachments){
- //ListViewItem item = new ListViewItem();
- if(entity.ContentDisposition != null && entity.ContentDisposition.Param_FileName != null){
- //item.Text = entity.ContentDisposition.Param_FileName;
- }
- else{
- // item.Text = "untitled";
- //item.ImageIndex = 0;
- // item.Tag = entity;
- //m_pTabPageMail_MessageAttachments.Items.Add(item);
- }
- if(mime.BodyText != null){
- // m_pTabPageMail_MessageText.Text = mime.BodyText;
- try
- {
- //寫入eml
- str stringhandle=new str();
- string title=mime.From.ToString()+"#"+stringhandle.strlen(mime.Subject.ToString(),20)+"#"+mime.Date.ToString("yyyy年MM月dd日 HH時mm分")+"#"+(mime.BodyText.Length / (decimal)1000).ToString("f2") + " kb#";
- title=FilterSpecial(title);
- cstring mystring=new cstring();
- title=mystring.ENCODE(title);
- title+=".eml";
- //寫入eml檔案
- string filepro=Application.StartupPath+"\\data\\"+thisuser+"\\"+global_user+"\\"+folder+"\\"+title;
- FileStream fs = new FileStream(filepro, FileMode.Create);//檔案名和路徑
- StreamWriter sw = new StreamWriter(fs);
- //開始寫入
- sw.Write(mime);
- //清空緩沖區
- sw.Flush();
- //關閉流
- sw.Close();
- fs.Close();
- catch(Exception x)
- }
- catch(Exception x){
- MessageBox.Show("Error: " + x.ToString(),"Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
- }));
- }
- MessageBox.Show("Error: " + x.ToString(),"Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);