最近都在忙工作上的事情,博客也好久都没有更新了,也不知道要写什么,最近感觉比较混乱,学习的时候不能静下心来。哎,希望这样混日子的状态能早点结束。
上周在工作中遇到了一个小需求,需要将公司内部的邮件全部从邮件服务器中抓取下来,在存储到数据库中去。我就写了以下的程序,将所有的邮件账号以及密码都存储到数据库中,通过程序来读取,最后将所有邮箱中的邮件存储到数据库中去。数据库的脚本如下:
1
create
table
MailAccount
2 (
3 UserName varchar ( 100 ) not null primary key ,
4 Pass varchar ( 100 ) not null ,
5 PopServer varchar ( 100 ) not null ,
6 Port int not null
7 )
8
9 create table MailRequest
10 (
11 MailFrom varchar ( 100 ),
12 MailTo varchar ( 100 ) ,
13 Subject varchar ( 200 ) ,
14 Contents varchar ( max ) ,
15 ServerIP varchar ( 100 ) ,
16 Country varchar ( 100 )
17 )
2 (
3 UserName varchar ( 100 ) not null primary key ,
4 Pass varchar ( 100 ) not null ,
5 PopServer varchar ( 100 ) not null ,
6 Port int not null
7 )
8
9 create table MailRequest
10 (
11 MailFrom varchar ( 100 ),
12 MailTo varchar ( 100 ) ,
13 Subject varchar ( 200 ) ,
14 Contents varchar ( max ) ,
15 ServerIP varchar ( 100 ) ,
16 Country varchar ( 100 )
17 )
新建了一个MailMessage类,
1
public
class
MailMessage
2 {
3 public MailMessage()
4 {
5 //
6 // TODO: Add constructor logic here
7 //
8 }
9
10 private string _mailFrom;
11 public string MailFrom
12 {
13 get
14 {
15 return _mailFrom;
16 }
17 set
18 {
19 _mailFrom = value;
20 }
21 }
22
23 private string _mailTo;
24 public string MailTo
25 {
26 get
27 {
28 return _mailTo;
29 }
30 set
31 {
32 _mailTo = value;
33 }
34 }
35
36 private string _subject;
37 public string Subject
38 {
39 get
40 {
41 return _subject;
42 }
43 set
44 {
45 _subject = value;
46 }
47 }
48
49 private string _content;
50 public string Content
51 {
52 get
53 {
54 return _content;
55 }
56 set
57 {
58 _content = value;
59 }
60 }
61
62 private string _addressIP;
63 public string AddressIP
64 {
65 get
66 {
67 return _addressIP;
68 }
69 set
70 {
71 _addressIP = value;
72 }
73 }
74}
2 {
3 public MailMessage()
4 {
5 //
6 // TODO: Add constructor logic here
7 //
8 }
9
10 private string _mailFrom;
11 public string MailFrom
12 {
13 get
14 {
15 return _mailFrom;
16 }
17 set
18 {
19 _mailFrom = value;
20 }
21 }
22
23 private string _mailTo;
24 public string MailTo
25 {
26 get
27 {
28 return _mailTo;
29 }
30 set
31 {
32 _mailTo = value;
33 }
34 }
35
36 private string _subject;
37 public string Subject
38 {
39 get
40 {
41 return _subject;
42 }
43 set
44 {
45 _subject = value;
46 }
47 }
48
49 private string _content;
50 public string Content
51 {
52 get
53 {
54 return _content;
55 }
56 set
57 {
58 _content = value;
59 }
60 }
61
62 private string _addressIP;
63 public string AddressIP
64 {
65 get
66 {
67 return _addressIP;
68 }
69 set
70 {
71 _addressIP = value;
72 }
73 }
74}
以下代码是抓取邮件的代码,抓取邮件采用jmail组件提供的接收邮件的功能,使用的是4.4免费版。朋友们可以从网上去下载。在抓取每一封邮件的IP地址的时候需要注意一下,有的邮件服务器在发邮件的时候在邮件头中是不写IP地址的,所以有时候会抓不到IP
1
public
partial
class
ReceiveMail : System.Web.UI.Page
2 {
3 private readonly string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
4 protected void Page_Load(object sender, EventArgs e)
5 {
6
7 }
8
9 //收取邮箱中邮件
10 private void ReceiveMails()
11 {
12 Message Message;
13 DataTable DtAccount = GetAccount();
14 MailMessage MyMessage = new MailMessage();
15 int StartIndex, EndIndex;
16 string Character = "[" + "]";
17 char[] AnyOf = Character.ToCharArray();
18 foreach (DataRow Rows in DtAccount.Rows)
19 {
20 POP3Class PopMail = new POP3Class();
21 //登录邮箱
22 PopMail.Connect((string)Rows["UserName"], (string)Rows["Pass"], (string)Rows["PopServer"], (int)Rows["Port"]);
23 if (PopMail.Count > 0)
24 {
25
26 //循环插入邮件内容
27 for (int i = 1; i <= PopMail.Count; i++)
28 {
29 Message = PopMail.Messages[i];
30 Message.Charset = "utf-8";
31 MyMessage.MailFrom = (Message.From.ToString() == string.Empty ? "Null" : Message.From.ToString());
32 MyMessage.MailTo = (string)Rows["UserName"] + "@" + Rows["PopServer"].ToString().Substring(Rows["PopServer"].ToString().IndexOf(".") + 1);
33 //MyMessage.MailTo = (Message.From.ToString() == string.Empty ? "test" : Message.From.ToString());
34 MyMessage.Subject = (Message.Subject != null ? Message.Subject : "Null");
35 MyMessage.Content = (Message.Body != null ? Message.Body.ToString() : "Null");
36
37 StartIndex = Message.Headers.Text.ToString().IndexOf(AnyOf[0]);
38 EndIndex = Message.Headers.Text.ToString().IndexOf(AnyOf[1]);
39 if (StartIndex > 0 && EndIndex > 0)
40 {
41 MyMessage.AddressIP = Message.Headers.Text.Substring(StartIndex + 1, (EndIndex - StartIndex - 1));
42 }
43 //插入邮件内容
44 InsertMail(MyMessage.MailFrom, MyMessage.MailTo, MyMessage.Subject, MyMessage.Content, MyMessage.AddressIP);
45 }
46 }
47 //PopMail.DeleteMessages();
48 PopMail.Disconnect();
49 PopMail = null;
50 }
51
52 }
53
54 //获取邮件账号
55 private DataTable GetAccount()
56 {
57 string QuerySql = "SELECT * FROM MailAccount";
58 DataTable dt = new DataTable("Account");
59 using (SqlConnection Con = new SqlConnection(ConStr))
60 {
61 using (SqlCommand Cmd = new SqlCommand(QuerySql, Con))
62 {
63 Con.Open();
64 using (SqlDataReader Dr = Cmd.ExecuteReader())
65 {
66 dt.Load(Dr);
67 }
68 }
69 }
70 return dt;
71 }
72
73 //插入邮件
74 private void InsertMail(string mailFrom, string mailTo,string subject,string content,string ip)
75 {
76 string InsertSql = "INSERT INTO MailRequest VALUES(@MailFrom,@MailTo,@Subject,@Content,@IP)";
77 using (SqlConnection Con = new SqlConnection(ConStr))
78 {
79 using (SqlCommand Cmd = new SqlCommand(InsertSql, Con))
80 {
81 Cmd.Parameters.AddWithValue("@MailFrom", mailFrom);
82 Cmd.Parameters.AddWithValue("@MailTo", mailTo);
83 Cmd.Parameters.AddWithValue("@Subject", subject);
84 Cmd.Parameters.AddWithValue("@Content", content);
85 Cmd.Parameters.AddWithValue("@IP", ip);
86 Con.Open();
87 Cmd.ExecuteNonQuery();
88 }
89 }
90 }
91}
2 {
3 private readonly string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
4 protected void Page_Load(object sender, EventArgs e)
5 {
6
7 }
8
9 //收取邮箱中邮件
10 private void ReceiveMails()
11 {
12 Message Message;
13 DataTable DtAccount = GetAccount();
14 MailMessage MyMessage = new MailMessage();
15 int StartIndex, EndIndex;
16 string Character = "[" + "]";
17 char[] AnyOf = Character.ToCharArray();
18 foreach (DataRow Rows in DtAccount.Rows)
19 {
20 POP3Class PopMail = new POP3Class();
21 //登录邮箱
22 PopMail.Connect((string)Rows["UserName"], (string)Rows["Pass"], (string)Rows["PopServer"], (int)Rows["Port"]);
23 if (PopMail.Count > 0)
24 {
25
26 //循环插入邮件内容
27 for (int i = 1; i <= PopMail.Count; i++)
28 {
29 Message = PopMail.Messages[i];
30 Message.Charset = "utf-8";
31 MyMessage.MailFrom = (Message.From.ToString() == string.Empty ? "Null" : Message.From.ToString());
32 MyMessage.MailTo = (string)Rows["UserName"] + "@" + Rows["PopServer"].ToString().Substring(Rows["PopServer"].ToString().IndexOf(".") + 1);
33 //MyMessage.MailTo = (Message.From.ToString() == string.Empty ? "test" : Message.From.ToString());
34 MyMessage.Subject = (Message.Subject != null ? Message.Subject : "Null");
35 MyMessage.Content = (Message.Body != null ? Message.Body.ToString() : "Null");
36
37 StartIndex = Message.Headers.Text.ToString().IndexOf(AnyOf[0]);
38 EndIndex = Message.Headers.Text.ToString().IndexOf(AnyOf[1]);
39 if (StartIndex > 0 && EndIndex > 0)
40 {
41 MyMessage.AddressIP = Message.Headers.Text.Substring(StartIndex + 1, (EndIndex - StartIndex - 1));
42 }
43 //插入邮件内容
44 InsertMail(MyMessage.MailFrom, MyMessage.MailTo, MyMessage.Subject, MyMessage.Content, MyMessage.AddressIP);
45 }
46 }
47 //PopMail.DeleteMessages();
48 PopMail.Disconnect();
49 PopMail = null;
50 }
51
52 }
53
54 //获取邮件账号
55 private DataTable GetAccount()
56 {
57 string QuerySql = "SELECT * FROM MailAccount";
58 DataTable dt = new DataTable("Account");
59 using (SqlConnection Con = new SqlConnection(ConStr))
60 {
61 using (SqlCommand Cmd = new SqlCommand(QuerySql, Con))
62 {
63 Con.Open();
64 using (SqlDataReader Dr = Cmd.ExecuteReader())
65 {
66 dt.Load(Dr);
67 }
68 }
69 }
70 return dt;
71 }
72
73 //插入邮件
74 private void InsertMail(string mailFrom, string mailTo,string subject,string content,string ip)
75 {
76 string InsertSql = "INSERT INTO MailRequest VALUES(@MailFrom,@MailTo,@Subject,@Content,@IP)";
77 using (SqlConnection Con = new SqlConnection(ConStr))
78 {
79 using (SqlCommand Cmd = new SqlCommand(InsertSql, Con))
80 {
81 Cmd.Parameters.AddWithValue("@MailFrom", mailFrom);
82 Cmd.Parameters.AddWithValue("@MailTo", mailTo);
83 Cmd.Parameters.AddWithValue("@Subject", subject);
84 Cmd.Parameters.AddWithValue("@Content", content);
85 Cmd.Parameters.AddWithValue("@IP", ip);
86 Con.Open();
87 Cmd.ExecuteNonQuery();
88 }
89 }
90 }
91}