ASP.NET中常用功能代码总结(1)——发送邮件篇

ASP.NET中常用功能代码总结(1)——发送邮件篇

整理:Terrylee

一.           Asp.net实现邮件系统

 1/// <summary>
 2        /// 功能:实现在Web页面中发送Email
 3        /// </summary>

 4        private void SendMail()
 5        {
 6            MailMessage m = new MailMessage();
 7            
 8            ///发件人地址
 9            m.From = tbFrom.Text;
10
11            ///收件人地址
12            m.To = tbTo.Text;
13
14            ///邮件主题
15            m.Subject = tbSubject.Text;
16
17            ///邮件内容
18            m.Body = tbBody.Text;
19
20            ///优先级
21            switch(ddlp.SelectedIndex)
22            {
23                case 0:
24                    m.Priority = MailPriority.High;
25                    break;
26                case 1:
27                    m.Priority = MailPriority.Low;
28                    break;
29                default:
30                    m.Priority = MailPriority.Normal;
31                    break;
32            }

33
34            ///设置邮件格式
35            if(ddlp.SelectedIndex==0)
36                m.BodyFormat = MailFormat.Text;
37            else
38                m.BodyFormat = MailFormat.Html;
39
40            ///设置服务器
41            if(tbServer.Text!="")
42            {
43                SmtpMail.SmtpServer = tbServer.Text;
44            }

45
46            ///以下处理附件 
47            string strFileName = FileSelect.PostedFile.FileName;
48            if(strFileName!="")
49                m.Attachments.Add(new MailAttachment(strFileName));
50
51            ///发送邮件
52            SmtpMail.Send(m);
53        }

二.           利用Socket来接收邮件

 1/// <summary>
 2        /// 接收邮件
 3        /// </summary>

 4        private void SocketPopMail()
 5        {
 6            POP3 pop = new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
 7            int n = pop.GetNumberOfNewMessages();
 8            if(n==-1)
 9            {
10                Response.Write("<script language='javascript'>alert('服务器连接错误!')</script>");
11                return;
12            }

13            ddlNew.Items.Clear();
14            for(int i=1;i<=n;i++)
15                ddlNew.Items.Add(""+i.ToString()+"封邮件");
16            if(n>0)
17            {
18                MailMessage msg =  pop.GetNewMessages(0);
19                if(msg!=null)
20                    tbBody.Text = msg.Body;
21            }

22        }
POP3类的实现如下:
  1/// <summary>
  2    /// 接收邮件类
  3    /// </summary>

  4    public class POP3
  5    {
  6        string POPServer;
  7        string user;
  8        string pwd;
  9        NetworkStream ns;
 10        StreamReader sr;
 11
 12        public POP3(){}
 13
 14        public POP3(string server, string _user, string _pwd)
 15        {
 16            POPServer = server;
 17            user = _user;
 18            pwd = _pwd;
 19        }

 20        
 21        /// <summary>
 22        /// 连接服务器
 23        /// </summary>
 24        /// <returns></returns>

 25        private bool Connect()
 26        {
 27            TcpClient sender = new TcpClient(POPServer,110);
 28            byte[] outbytes;
 29            string input;
 30
 31            try
 32            {
 33                ns = sender.GetStream();
 34                sr = new StreamReader(ns);
 35
 36                sr.ReadLine();
 37                input = "user " + user + "\r\n";
 38                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
 39                ns.Write(outbytes,0,outbytes.Length) ;
 40                sr.ReadLine();
 41            
 42                input = "pass " + pwd + "\r\n";
 43                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
 44                ns.Write(outbytes,0,outbytes.Length) ;
 45                sr.ReadLine();
 46                return true;  
 47        
 48            }

 49            catch
 50            {
 51                return false;
 52            }

 53        }

 54        
 55        /// <summary>
 56        /// 断开与服务器的连接
 57        /// </summary>

 58        private void Disconnect()
 59        {
 60            string input = "quit" + "\r\n";
 61            Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
 62            ns.Write(outbytes,0,outbytes.Length);
 63            ns.Close();
 64        }

 65
 66        public int GetNumberOfNewMessages()
 67        {
 68            byte[] outbytes;
 69            string input;
 70
 71            try
 72            {
 73                Connect();
 74
 75                input = "stat" + "\r\n";
 76                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
 77                ns.Write(outbytes,0,outbytes.Length);
 78                string resp = sr.ReadLine();
 79                string[] tokens = resp.Split(new Char[] {' '});
 80
 81                Disconnect();
 82
 83                return Convert.ToInt32(tokens[1]);
 84            }

 85            catch
 86            {
 87                return -1;
 88            }

 89        }

 90        public ArrayList GetNewMessages(string subj)
 91        {
 92
 93            int newcount;
 94            ArrayList newmsgs = new ArrayList();
 95
 96            try
 97            {
 98                newcount = GetNumberOfNewMessages();
 99                Connect();
100
101                for(int n=1; n<newcount+1; n++)
102                {
103                    ArrayList msglines = GetRawMessage(n);
104                    string msgsubj = GetMessageSubject(msglines);
105                    if(msgsubj.CompareTo(subj) == 0)
106                    {
107                        System.Web.Mail.MailMessage msg = new MailMessage();
108                        msg.Subject = msgsubj;
109                        msg.From = GetMessageFrom(msglines);
110                        msg.Body = GetMessageBody(msglines);
111                        newmsgs.Add(msg);
112                        DeleteMessage(n);
113                    }

114                }

115
116                Disconnect();
117                return newmsgs;
118            }

119            catch(Exception e)
120            {
121                return newmsgs;
122            }

123        }

124
125        /// <summary>
126        /// 获取新邮件
127        /// </summary>
128        /// <param name="nIndex"></param>
129        /// <returns></returns>

130        public MailMessage GetNewMessages(int nIndex)
131        {
132            int newcount;
133            System.Web.Mail.MailMessage msg = new MailMessage();
134
135            try
136            {
137                newcount = GetNumberOfNewMessages();
138                Connect();
139                int n = nIndex+1;
140
141                if(n<newcount+1)
142                {
143                    ArrayList msglines = GetRawMessage(n);
144                    string msgsubj = GetMessageSubject(msglines);
145                
146                    
147                    msg.Subject = msgsubj;
148                    msg.From = GetMessageFrom(msglines);
149                    msg.Body = GetMessageBody(msglines);
150                }

151
152                Disconnect();
153                return msg;
154            }

155            catch
156            {
157                return null;
158            }

159        }

160        private ArrayList GetRawMessage (int messagenumber)
161        {
162            Byte[] outbytes;
163            string input;
164            string line = "";
165
166            input = "retr " + messagenumber.ToString() + "\r\n";
167            outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
168            ns.Write(outbytes,0,outbytes.Length);
169
170            ArrayList msglines = new ArrayList();
171            do
172            {
173                line = sr.ReadLine();
174                msglines.Add(line);
175            }
 while (line != ".");
176            msglines.RemoveAt(msglines.Count-1);
177
178            return msglines;
179        }

180
181