首页 新闻 赞助 找找看

如何让收取过的邮件下次不再重复收取?

0
悬赏园豆:5 [已关闭问题]

我用jmail做了个接收外部邮箱邮件的功能,接受过后,将邮件插入数据库中,但是每次都会重复接收已经接受过的邮件,恳请高人指点,怎样只接收未被接收的邮件?

Crazy Boy的主页 Crazy Boy | 初学一级 | 园豆:0
提问于:2010-03-22 18:08
< >
分享
其他回答(1)
0

可以尝试在收取邮件后,记录最后的收取时间。每次收取时可以根据这个最后的收取时间,来收取没有接受的邮件。

itaozi | 园豆:1055 (小虾三级) | 2010-03-22 22:59
0

//取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间
                    DataTable data = this.GetDataTable("select max(accepttime) from acceptfax where sendtag='1'");

我使用的完整代码如下:

代码
1 using System.Data.SqlClient;
2  using faxapp.dataconn;
3 using faxapp.common;
4 using System.Net.Sockets;
5 using jmail;
6 using System.IO;
7
8 收邮件完整代码:
9
10 public void ReceiveMails()
11 {
12
13 jmail.Message Msg = new jmail.Message();
14 jmail.POP3 jpop = new jmail.POP3();
15 jmail.Attachments atts;
16 jmail.Attachment att;
17 //Mime m = new Mime();
18
19 //username为用户名,该方法通过用户名获取该用户的pop设置,即用户的POP用户名,密码,POP服务器地址以及端口号这四个参数,这四个参数是连接POP服务器的必用参数.
20 //SqlDataReader dataReader = this.ExtGetSetting(Username);
21 //if (dataReader.Read())
22 //{
23 // if (dataReader["PopServer"].ToString() != "" && dataReader["PopUsername"].ToString() != "")
24 // {
25 //连接POP服务器
26 MailAddress = System.Configuration.ConfigurationManager.AppSettings.Get("MailAddress");
27 MailFrom = System.Configuration.ConfigurationManager.AppSettings.Get("MailFrom");
28 POPAddress = System.Configuration.ConfigurationManager.AppSettings.Get("POPAddress");
29 STMPAddress = System.Configuration.ConfigurationManager.AppSettings.Get("STMPAddress");
30 MailUsername = System.Configuration.ConfigurationManager.AppSettings.Get("MailUsername");
31 MailPass = System.Configuration.ConfigurationManager.AppSettings.Get("MailPass");
32 Mailpassword=System.Configuration.ConfigurationManager.AppSettings.Get("Mailpassword");
33 string mailPopPort = System.Configuration.ConfigurationManager.AppSettings.Get("MailPopPort");
34 MailPopPort = Convert.ToInt32(mailPopPort);
35 jpop.Connect(MailUsername, Mailpassword, POPAddress, MailPopPort);
36 //如果服务器上有邮件
37 if (jpop.Count >= 1)
38 {
39 for (int i = 1; i <= jpop.Count; i++)
40 {
41
42 Msg = jpop.Messages[i];
43 atts = Msg.Attachments;
44
45 //取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间
46 DataTable data = this.GetDataTable("select max(accepttime) from acceptfax where sendtag='1'");
47
48 //对服务器上的邮件的发送时间和数据库最近一封邮件的时间进行比较,如果大那么证明该邮件还未被收取,是一封新邮件,这样避免重复收取邮件入库
49 if (Msg.Date > Convert.ToDateTime(data.Rows[0][4].ToString()))
50 {
51 //将这封新邮件的信息保存到数据库
52 //this.SaveExtMail(Msg, Username, dataReader["Email"].ToString(), jpop.GetMessageUID(i));
53 this.SaveExtMail(Msg.FromName, Msg.Subject, Msg.Body, Msg.Date.ToLongDateString());
54
55 //获取附件上传到服务器并且将信息存入数据库
56 if (atts.Count >= 1)
57 {
58 for (int k = 0; k < atts.Count; k++)
59 {
60
61 att = atts[k];//获得附件
62
63 string attname = att.Name;
64 try
65 {
66
67 //Random TempNameInt = new Random();
68 //string NewMailDirName = TempNameInt.Next(100000000).ToString();
69 //生成随机文件后缀名
70 string strSaveDir = "\\AttachFiles\\";
71 //取得文件名(抱括路径)里最后一个"."的索引
72 int intExt = attname.LastIndexOf(".");
73 //取得文件扩展名
74 string strExt = attname.Substring(intExt);
75 //取得文件名(不包括路径)
76 Random objRand = new Random();
77 System.DateTime date = DateTime.Now;
78 //生成随机文件名
79 string str = attname.Substring(1, attname.LastIndexOf(".") - 1);
80 string saveName = System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
81 string strNewName = str + "(" + saveName + ")" + strExt;//取新名字,防止重复
82
83 Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".") + "\\AttachFiles\\" + strNewName);
84
85 string mailPath = strSaveDir + strNewName;
86
87 att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".") + mailPath);
88 //获取该封邮件在数据库的ID,以便和附件信息相对应,取邮件表中的最大ID即可
89 int mailID = this.GetMailID();
90 ////将附件信息存入数据库
91 this.AttExtSend(mailID, attname, att.Size, mailPath, Msg.From);
92 }
93 catch (Exception ex)
94 {
95 throw new Exception(ex.Message);
96 }
97
98 }
99 }
100 }
101 }
102 }
103
104 //删除服务器上的邮件
105 jpop.DeleteMessages();
106 //断开连接
107 jpop.Disconnect();
108 // }
109
110 //}
111 }
112
113 //取数据库中邮件某个条件下的某条信息
114 public DataTable GetDataTable(string sqlstr)
115 {
116 SqlConnection conn = sqlconn.CreateConn();
117 SqlDataAdapter da = new SqlDataAdapter(sqlstr , conn);
118 DataSet ds = new DataSet();
119 da.Fill(ds);
120 DataTable dt = ds.Tables[0];
121 dt.AcceptChanges();
122 conn.Close();
123 return dt;
124 }
125
126 //将新邮件的信息保存到数据库acceptfax表中
127 public void SaveExtMail(string FromName, string Subject, string Body, string Date)
128 {
129 //-----------------------更新到数据库中-------------------------
130 SqlConnection conn = sqlconn.CreateConn();
131 SqlDataAdapter da = new SqlDataAdapter("select * from acceptfax", conn);
132 DataSet ds = new DataSet();
133 da.Fill(ds);
134 DataTable dt = ds.Tables[0];
135
136 DataRow dr = dt.NewRow();
137 dr["faxno"] = FromName;
138 dr["title"] = Subject;
139 dr["content"] = Body;
140 dr["accepttime"] = Date;
141 dt.Rows.Add(dr);
142 SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
143 da.Update(dt);
144 dt.AcceptChanges();
145 conn.Close();
146 }
147
148 //获取该邮件的在数据库中的ID号,便与和附件相对应
149 public int GetMailID()
150 {
151 //取数据库中邮件信息中的最大发送时间,即最近接收到的一封邮件的时间,得到该邮件sid号
152 DataTable data = this.GetDataTable("select max(accepttime) from acceptfax where sendtag='1'");
153 int result = Convert.ToInt32(data.Rows[0][0].ToString());
154 return result;
155 }
156 //把每个附件的内容更新入数据库
157 public void AttExtSend(int mailID,string attname,int Size,string mailpath,string format)
158 {
159 //-----------------------更新到数据库中-------------------------
160 SqlConnection conn = sqlconn.CreateConn();
161 SqlDataAdapter da = new SqlDataAdapter("select * from attachmentInfo", conn);
162 DataSet ds = new DataSet();
163 da.Fill(ds);
164 DataTable dt = ds.Tables[0];
165
166 DataRow dr = dt.NewRow();
167 dr["mailID"] = mailID;
168 dr["attname"] = attname;
169 dr["attsize"] = Size;
170 dr["mailpath"] = mailpath;
171 dr["format"] = format;
172 dt.Rows.Add(dr);
173 SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
174 da.Update(dt);
175 dt.AcceptChanges();
176 conn.Close();
177 }
178
风影极光 | 园豆:1573 (小虾三级) | 2010-03-24 09:23
不错!!!
支持(0) 反对(0) Crazy Boy | 园豆:0 (初学一级) | 2010-03-24 16:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册