1. jmail.Message Msg=new jmail.Message(); 
  2.             jmail.POP3 jpop = new jmail.POP3(); 
  3.             jmail.Attachments atts; 
  4.             jmail.Attachment att; 
  5.              
  6.             //username为用户名,该方法通过用户名获取该用户的pop设置,即用户的POP用户名,密码,POP服务器地址以及端口号这四个参数,这四个参数是连接POP服务器的必用参数. 
  7.                 SqlDataReader dataReader = this.ExtGetSetting(Username); 
  8.                 if(dataReader.Read()) 
  9.                 { 
  10.                     if(dataReader["PopServer"].ToString()!=""&&dataReader["PopUsername"].ToString()!=""
  11.                     { 
  12.                         //连接POP服务器 
  13.                         jpop.Connect(dataReader["PopUsername"].ToString(),dataReader["PopPassword"].ToString(),dataReader["PopServer"].ToString(),Int32.Parse(dataReader["PopPort"].ToString())); 
  14.                         //如果服务器上有邮件 
  15.                         if(jpop.Count> =1) 
  16.                         { 
  17.                          for(int i=1;i <=jpop.Count;i++) 
  18.                             { 
  19.                              
  20.                                 Msg = jpop.Messages[i]; 
  21.                                 atts=Msg.Attachments; 
  22.                              //取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间 
  23.                                 Database dbase=new Database(); 
  24.                                 DataTable data=dbase.GetDataTable("select max(MailSendDate) as MailSenderDate from TabMailList where MailTypeFlag='1'"); 
  25.                                  
  26.                              //对服务器上的邮件的发送时间和数据库最近一封邮件的时间进行比较,如果大那么证明该邮件还未被收取,是一封新邮件,这样避免重复收取邮件入库 
  27.                                 if(Msg.Date> Convert.ToDateTime(data.Rows[0][0].ToString())) 
  28.                                 { 
  29.                                     //将这封新邮件的信息保存到数据库 
  30.                                     this.SaveExtMail(Msg,Username,dataReader["Email"].ToString(),jpop.GetMessageUID(i)); 
  31.                              
  32.                                     //获取附件上传到服务器并且将信息存入数据库 
  33.                                     if(atts.Count> =1) 
  34.                                     { 
  35.                                         for(int k=0;k <atts.Count;k++) 
  36.                                         { 
  37.                                             att=atts[k];//获得附件 
  38.                                             string attname=att.Name; 
  39.                                             try 
  40.                                             { 
  41.                                                 Random TempNameInt    = new Random();  
  42.                                                 string NewMailDirName  = TempNameInt.Next(100000000).ToString(); 
  43.                                                 Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".")+"//AttachFiles//"+Username+"//"+NewMailDirName); 
  44.                  
  45.                                                 string mailPath="//AttachFiles//"+Username+"//"+NewMailDirName+"//"+attname; 
  46.              
  47.                                                 att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".")+mailPath);  
  48.                                                  
  49.                                                 //获取该封邮件在数据库的ID,以便和附件信息相对应,取邮件表中的最大ID即可  
  50.                                                 int mailID=this.GetMailID(); 
  51.                                                //将附件信息存入数据库 
  52.                                                 this.AttExtSend(mailID,attname,att.Size,mailPath,Msg.From); 
  53.                                             } 
  54.                                             catch(Exception ex) 
  55.                                             { 
  56.                                                 throw new Exception(ex.Message); 
  57.                                             } 
  58.                                          
  59.                                         } 
  60.                                     }                                         
  61.                                 }                             
  62.                             }                         
  63.                         }     
  64.                         //删除服务器上的邮件 
  65.                         //jpop.DeleteMessages(); 
  66.                         //断开连接     
  67.                         jpop.Disconnect(); 
  68.                     } 
  69.                  
  70.             } 
  71.         } 
  72. 邮件信息入库:

    1. public string SaveExtMail(jmail.Message JMsg, string Username, string Email) 
    2.         { 
    3.             Database data = new Database(); 
    4.             string MailID = null
    5.              
    6.             SqlParameter[] prams = { 
    7.    data.MakeInParam("@MailFolderType",  SqlDbType.Int, 20, 1),//? 
    8.    data.MakeInParam("@MailReceiverStr",  SqlDbType.VarChar, 300, Username),//! 
    9.    data.MakeInParam("@MailSendDate",  SqlDbType.DateTime, 20, DateTime.Parse(JMsg.Date.ToString())),//发送时间 
    10.    data.MakeInParam("@MailSendLevel",  SqlDbType.SmallInt,20, 1),//! 
    11.    data.MakeInParam("@MailSender",  SqlDbType.NVarChar, 100, JMsg.From.ToString()),//发送者地址 
    12.    data.MakeInParam("@MailReceiver",  SqlDbType.NVarChar, 100, Username),//接受者 
    13.    data.MakeInParam("@MailSubject",  SqlDbType.NVarChar, 150,subject),//邮件主题 
    14.    data.MakeInParam("@MailBody",  SqlDbType.Text , 300000,  ( JMsg.BodyText==null?"": body )),//邮件内容 
    15.    data.MakeInParam("@MailCcToAddr",  SqlDbType.NVarChar, 300,""),//? 
    16.    data.MakeInParam("@MailBccToAddr",  SqlDbType.NVarChar, 300, ""),//? 
    17.    data.MakeInParam("@MailReadFlag",  SqlDbType.Bit, 1, 0), 
    18.    data.MakeInParam("@MailTypeFlag",  SqlDbType.Bit, 1,1),//? 
    19.    data.MakeInParam("@MailClassID",  SqlDbType.Int, 1, 0), 
    20.    data.MakeInParam("@MailImportance",  SqlDbType.Int, 1, Convert.ToInt32(JMsg.Priority)),//优先级 
    21.    data.MakeOutParam("@MailID", SqlDbType.Int,20) //? 
    22.    }; 
    23.             try 
    24.             { 
    25.                 data.RunProc("SP_MailSend", prams); 
    26.                 MailID = prams[14].Value.ToString(); 
    27.                 if (MailID == string.Empty) 
    28.                     return null
    29.                 else 
    30.                     return MailID; 
    31.             } 
    32.             catch (Exception ex) 
    33.             { 
    34.                 Error.Log(ex.ToString()); 
    35.               } 
    36.         } 
    37.   

     


本文转载:CSDN博客