- jmail.Message Msg=new jmail.Message();
- jmail.POP3 jpop = new jmail.POP3();
- jmail.Attachments atts;
- jmail.Attachment att;
- //username为用户名,该方法通过用户名获取该用户的pop设置,即用户的POP用户名,密码,POP服务器地址以及端口号这四个参数,这四个参数是连接POP服务器的必用参数.
- SqlDataReader dataReader = this.ExtGetSetting(Username);
- if(dataReader.Read())
- {
- if(dataReader["PopServer"].ToString()!=""&&dataReader["PopUsername"].ToString()!="")
- {
- //连接POP服务器
- jpop.Connect(dataReader["PopUsername"].ToString(),dataReader["PopPassword"].ToString(),dataReader["PopServer"].ToString(),Int32.Parse(dataReader["PopPort"].ToString()));
- //如果服务器上有邮件
- if(jpop.Count> =1)
- {
- for(int i=1;i <=jpop.Count;i++)
- {
- Msg = jpop.Messages[i];
- atts=Msg.Attachments;
- //取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间
- Database dbase=new Database();
- DataTable data=dbase.GetDataTable("select max(MailSendDate) as MailSenderDate from TabMailList where MailTypeFlag='1'");
- //对服务器上的邮件的发送时间和数据库最近一封邮件的时间进行比较,如果大那么证明该邮件还未被收取,是一封新邮件,这样避免重复收取邮件入库
- if(Msg.Date> Convert.ToDateTime(data.Rows[0][0].ToString()))
- {
- //将这封新邮件的信息保存到数据库
- this.SaveExtMail(Msg,Username,dataReader["Email"].ToString(),jpop.GetMessageUID(i));
- //获取附件上传到服务器并且将信息存入数据库
- if(atts.Count> =1)
- {
- for(int k=0;k <atts.Count;k++)
- {
- att=atts[k];//获得附件
- string attname=att.Name;
- try
- {
- Random TempNameInt = new Random();
- string NewMailDirName = TempNameInt.Next(100000000).ToString();
- Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".")+"//AttachFiles//"+Username+"//"+NewMailDirName);
- string mailPath="//AttachFiles//"+Username+"//"+NewMailDirName+"//"+attname;
- att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".")+mailPath);
- //获取该封邮件在数据库的ID,以便和附件信息相对应,取邮件表中的最大ID即可
- int mailID=this.GetMailID();
- //将附件信息存入数据库
- this.AttExtSend(mailID,attname,att.Size,mailPath,Msg.From);
- }
- catch(Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }
- }
- }
- }
- //删除服务器上的邮件
- //jpop.DeleteMessages();
- //断开连接
- jpop.Disconnect();
- }
- }
- }
-
邮件信息入库:
- public string SaveExtMail(jmail.Message JMsg, string Username, string Email)
- {
- Database data = new Database();
- string MailID = null;
- SqlParameter[] prams = {
- data.MakeInParam("@MailFolderType", SqlDbType.Int, 20, 1),//?
- data.MakeInParam("@MailReceiverStr", SqlDbType.VarChar, 300, Username),//!
- data.MakeInParam("@MailSendDate", SqlDbType.DateTime, 20, DateTime.Parse(JMsg.Date.ToString())),//发送时间
- data.MakeInParam("@MailSendLevel", SqlDbType.SmallInt,20, 1),//!
- data.MakeInParam("@MailSender", SqlDbType.NVarChar, 100, JMsg.From.ToString()),//发送者地址
- data.MakeInParam("@MailReceiver", SqlDbType.NVarChar, 100, Username),//接受者
- data.MakeInParam("@MailSubject", SqlDbType.NVarChar, 150,subject),//邮件主题
- data.MakeInParam("@MailBody", SqlDbType.Text , 300000, ( JMsg.BodyText==null?"": body )),//邮件内容
- data.MakeInParam("@MailCcToAddr", SqlDbType.NVarChar, 300,""),//?
- data.MakeInParam("@MailBccToAddr", SqlDbType.NVarChar, 300, ""),//?
- data.MakeInParam("@MailReadFlag", SqlDbType.Bit, 1, 0),
- data.MakeInParam("@MailTypeFlag", SqlDbType.Bit, 1,1),//?
- data.MakeInParam("@MailClassID", SqlDbType.Int, 1, 0),
- data.MakeInParam("@MailImportance", SqlDbType.Int, 1, Convert.ToInt32(JMsg.Priority)),//优先级
- data.MakeOutParam("@MailID", SqlDbType.Int,20) //?
- };
- try
- {
- data.RunProc("SP_MailSend", prams);
- MailID = prams[14].Value.ToString();
- if (MailID == string.Empty)
- return null;
- else
- return MailID;
- }
- catch (Exception ex)
- {
- Error.Log(ex.ToString());
- }
- }
用jmail收邮件
本文转载:CSDN博客