- //开始循环取邮件数据
- 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")
- {
- //开始循环取邮件数据
- m_pImap.Fetch(
- 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.
- */
- try{
- 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);
- }
- }));
- }
- }
- catch(Exception x){
- MessageBox.Show("Error: " + x.ToString(),"Error:",MessageBoxButtons.OK,MessageBoxIcon.Error);
- }
- }
也就是说,收取邮件头和邮件正文分开,这样就能收取包括yahoo在内的所有邮箱。代码是直接从我的工程文件里面拷出来的,作为参考,不能直接使用,但是保证是一定可以用的,更多问题可以联系我。