自己毕业论文中设计的邮件发送和接收程序(2)
代码太多,IE有点受不了,重新在发一个,上面的是收邮件类。下面的这个是发邮件的:
Code
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Data;
5using System.Net;
6using System.Net.Sockets;
7using System.IO;
8using System.Text;
9using System.Windows.Forms;
10namespace ConsoleApplication1
11{
12 public class SendMail
13 {
14 // mei jia ru zhuang tai ce shi
15 public void Send_Mail(string serverText,string sendermail,string senderpass,string mailsen,string mailrec,string subject,string body)
16 {
17 Cursor cr = Cursor.Current;
18 Cursor.Current = Cursors.WaitCursor;
19 TcpClient SmtpServ = new TcpClient(serverText, 25);
20 string Data;
21 byte[] szData;
22 string CRLF = "\r\n";
23 try
24 {
25 // initialization
26 NetworkStream NetStrm = SmtpServ.GetStream();
27 StreamReader RdStrm = new StreamReader(SmtpServ.GetStream());
28 // LogList.Items.Add(RdStrm.ReadLine());
29 Console.WriteLine(RdStrm.ReadLine());
30 // say hello to server and send response into log report
31 Data = "HELO server " + CRLF;
32 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
33 NetStrm.Write(szData, 0, szData.Length);
34 // LogList.Items.Add(RdStrm.ReadLine());
35 Console.WriteLine(RdStrm.ReadLine());
36 Data = "AUTH LOGIN" + CRLF;
37 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
38 NetStrm.Write(szData, 0, szData.Length);
39 // LogList.Items.Add(RdStrm.ReadLine());
40 Console.WriteLine(RdStrm.ReadLine());
41 byte[] temp = System.Text.Encoding.ASCII.GetBytes(sendermail);
42 Data = Convert.ToBase64String(temp) + CRLF;
43 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
44 NetStrm.Write(szData, 0, szData.Length);
45 // LogList.Items.Add(RdStrm.ReadLine());
46 Console.WriteLine(RdStrm.ReadLine());
47 temp = System.Text.Encoding.ASCII.GetBytes(senderpass);
48 Data = Convert.ToBase64String(temp) + CRLF;
49 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
50 NetStrm.Write(szData, 0, szData.Length);
51 // LogList.Items.Add(RdStrm.ReadLine());
52 Console.WriteLine(RdStrm.ReadLine());
53
54 // send sender data
55 Data = "MAIL FROM: " + "<" + mailsen + ">" + CRLF;
56 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
57 NetStrm.Write(szData, 0, szData.Length);
58 // LogList.Items.Add(RdStrm.ReadLine());
59 Console.WriteLine(RdStrm.ReadLine());
60 // send receiver data
61 Data = "RCPT TO: " + "<" + mailrec + ">" + CRLF;
62 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
63 NetStrm.Write(szData, 0, szData.Length);
64 // LogList.Items.Add(RdStrm.ReadLine());
65 Console.WriteLine(RdStrm.ReadLine());
66 // send DATA
67 Data = "DATA" + CRLF;
68 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
69 NetStrm.Write(szData, 0, szData.Length);
70 // LogList.Items.Add(RdStrm.ReadLine());
71 Console.WriteLine(RdStrm.ReadLine());
72 // send content data
73 Data = "SUBJECT:" + subject + CRLF + CRLF + "BODY:" + body + CRLF + "." + CRLF;
74 // Data = sSubject.Text + CRLF + sMessage.Text + CRLF + "." + CRLF;
75 szData = System.Text.Encoding.GetEncoding("GBK").GetBytes(Data.ToCharArray());
76 NetStrm.Write(szData, 0, szData.Length);
77 // LogList.Items.Add(RdStrm.ReadLine());
78 Console.WriteLine(RdStrm.ReadLine());
79 // quit from server SMTP
80 Data = "QUIT" + CRLF;
81 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
82 NetStrm.Write(szData, 0, szData.Length);
83 // LogList.Items.Add(RdStrm.ReadLine());
84 Console.WriteLine(RdStrm.ReadLine());
85 // close connection
86 NetStrm.Close();
87 RdStrm.Close();
88 // LogList.Items.Add("Close connection");
89 Console.WriteLine("Close connection");
90 // LogList.Items.Add("Send mail successly..");
91 // back to normal cursor
92 Cursor.Current = cr;
93 }
94 catch (InvalidOperationException err)
95 {
96 // LogList.Items.Add("Error: " + err.ToString());
97 Console.WriteLine("Error: " + err.ToString());
98 }
99 }
100 }
101}
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Data;
5using System.Net;
6using System.Net.Sockets;
7using System.IO;
8using System.Text;
9using System.Windows.Forms;
10namespace ConsoleApplication1
11{
12 public class SendMail
13 {
14 // mei jia ru zhuang tai ce shi
15 public void Send_Mail(string serverText,string sendermail,string senderpass,string mailsen,string mailrec,string subject,string body)
16 {
17 Cursor cr = Cursor.Current;
18 Cursor.Current = Cursors.WaitCursor;
19 TcpClient SmtpServ = new TcpClient(serverText, 25);
20 string Data;
21 byte[] szData;
22 string CRLF = "\r\n";
23 try
24 {
25 // initialization
26 NetworkStream NetStrm = SmtpServ.GetStream();
27 StreamReader RdStrm = new StreamReader(SmtpServ.GetStream());
28 // LogList.Items.Add(RdStrm.ReadLine());
29 Console.WriteLine(RdStrm.ReadLine());
30 // say hello to server and send response into log report
31 Data = "HELO server " + CRLF;
32 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
33 NetStrm.Write(szData, 0, szData.Length);
34 // LogList.Items.Add(RdStrm.ReadLine());
35 Console.WriteLine(RdStrm.ReadLine());
36 Data = "AUTH LOGIN" + CRLF;
37 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
38 NetStrm.Write(szData, 0, szData.Length);
39 // LogList.Items.Add(RdStrm.ReadLine());
40 Console.WriteLine(RdStrm.ReadLine());
41 byte[] temp = System.Text.Encoding.ASCII.GetBytes(sendermail);
42 Data = Convert.ToBase64String(temp) + CRLF;
43 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
44 NetStrm.Write(szData, 0, szData.Length);
45 // LogList.Items.Add(RdStrm.ReadLine());
46 Console.WriteLine(RdStrm.ReadLine());
47 temp = System.Text.Encoding.ASCII.GetBytes(senderpass);
48 Data = Convert.ToBase64String(temp) + CRLF;
49 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
50 NetStrm.Write(szData, 0, szData.Length);
51 // LogList.Items.Add(RdStrm.ReadLine());
52 Console.WriteLine(RdStrm.ReadLine());
53
54 // send sender data
55 Data = "MAIL FROM: " + "<" + mailsen + ">" + CRLF;
56 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
57 NetStrm.Write(szData, 0, szData.Length);
58 // LogList.Items.Add(RdStrm.ReadLine());
59 Console.WriteLine(RdStrm.ReadLine());
60 // send receiver data
61 Data = "RCPT TO: " + "<" + mailrec + ">" + CRLF;
62 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
63 NetStrm.Write(szData, 0, szData.Length);
64 // LogList.Items.Add(RdStrm.ReadLine());
65 Console.WriteLine(RdStrm.ReadLine());
66 // send DATA
67 Data = "DATA" + CRLF;
68 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
69 NetStrm.Write(szData, 0, szData.Length);
70 // LogList.Items.Add(RdStrm.ReadLine());
71 Console.WriteLine(RdStrm.ReadLine());
72 // send content data
73 Data = "SUBJECT:" + subject + CRLF + CRLF + "BODY:" + body + CRLF + "." + CRLF;
74 // Data = sSubject.Text + CRLF + sMessage.Text + CRLF + "." + CRLF;
75 szData = System.Text.Encoding.GetEncoding("GBK").GetBytes(Data.ToCharArray());
76 NetStrm.Write(szData, 0, szData.Length);
77 // LogList.Items.Add(RdStrm.ReadLine());
78 Console.WriteLine(RdStrm.ReadLine());
79 // quit from server SMTP
80 Data = "QUIT" + CRLF;
81 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
82 NetStrm.Write(szData, 0, szData.Length);
83 // LogList.Items.Add(RdStrm.ReadLine());
84 Console.WriteLine(RdStrm.ReadLine());
85 // close connection
86 NetStrm.Close();
87 RdStrm.Close();
88 // LogList.Items.Add("Close connection");
89 Console.WriteLine("Close connection");
90 // LogList.Items.Add("Send mail successly..");
91 // back to normal cursor
92 Cursor.Current = cr;
93 }
94 catch (InvalidOperationException err)
95 {
96 // LogList.Items.Add("Error: " + err.ToString());
97 Console.WriteLine("Error: " + err.ToString());
98 }
99 }
100 }
101}
程序没有实现对邮件附件的下载和发送,因为我程序不需要这个功能,我只需要提取subject和body就可以。
因为我只需要知道邮件正文的信息,所以这里一个最重要的问题就是新邮件的提取,当然用uidl命令就可以,我需要实现
用文件系统(别的也可以)存储好已经下载的邮件列表,然后登陆邮件服务器,查看是否有不同于我保存的列表的邮件,如果有就是新邮件,当然了这里逻辑上感觉还是简单,但也足够用,大家如果有好思路可以告诉我啊!!!!!!!
程序运行时,需要先运行ReceivMail类中的Create_New方法,创建以保存的邮件列表,然后就可以分别调用SendMail类发邮件,接着就可以调用ReceivMail类提取新邮件了。大家有疑问可以问我,估计很简单,应该没问题。