C#的简单的邮件发送和接收

时间:2021-10-06 03:38:27
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets; using System.IO; using System.Net; namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {
          // 发送邮件            //MailAddress from = new MailAddress("arfornet@gmail.com");            //MailAddress to = new MailAddress("swang@tekdigitel.com");            //MailMessage message = new MailMessage(from, to);            //message.Subject = "test";            //message.Body = "IP地址:";            //message.IsBodyHtml = true;            //message.SubjectEncoding = System.Text.Encoding.Default;            //message.BodyEncoding = System.Text.Encoding.Default;            //SmtpClient client = new SmtpClient("smtp.gmail.com");            //System.Net.NetworkCredential smtpuserinfo = new System.Net.NetworkCredential();            //smtpuserinfo.UserName = "arfornet@gmail.com";            //smtpuserinfo.Password = "a8r3f8o7r6n1e5t8";            //client.Credentials = smtpuserinfo;            //client.EnableSsl = true;//这里是服务器要求验证            //client.Send(message);            //MessageBox.Show("victory");                         
            POP pop3 = new POP("E-Mail.Server", "UserName", "PassWord");            Console.WriteLine("New Messages = {0}", pop3.GetNumberOfNewMessages());            Console.ReadLine(); 
        }    }    class POP    {        string POPServer;        string user;        string pwd;        public POP() { }        public POP(string _popserver, string _user, string _pwd)        {            POPServer = _popserver;            user = _user;            pwd = _pwd;        }        private NetworkStream Connect()        {            TcpClient sender = new TcpClient(POPServer, 110);            Byte[] outbytes;            string input;            NetworkStream ns = null;            try            {                ns = sender.GetStream();                StreamReader sr = new StreamReader(ns);                Console.WriteLine("1:" + sr.ReadLine());                input = "user " + user + "/r/n";                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());                ns.Write(outbytes, 0, outbytes.Length);                Console.WriteLine("2:" + sr.ReadLine());                input = "pass " + pwd + "/r/n";                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());                ns.Write(outbytes, 0, outbytes.Length);                Console.WriteLine("3:" + sr.ReadLine());                return ns;            }            catch (InvalidOperationException ioe)            {                Console.WriteLine("Could not connect to mail server");                return ns;            }        }        public int GetNumberOfNewMessages()        {            Byte[] outbytes;            string input;            try            {                NetworkStream ns = Connect();                StreamReader sr = new StreamReader(ns);                input = "stat" + "/r/n";                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());                ns.Write(outbytes, 0, outbytes.Length);                string resp = sr.ReadLine();                Console.WriteLine("4:" + resp);                string[] tokens = resp.Split(new Char[] { ' ' });                input = "quit" + "/r/n";                outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());                ns.Write(outbytes, 0, outbytes.Length);                Console.WriteLine("5:" + sr.ReadLine());                sr.Close();                ns.Close();                //return tokens[1].ToInt32();                return Convert.ToInt32(tokens[1]);            }            catch (InvalidOperationException ioe)            {                Console.WriteLine("Could not connect to mail server");                return 0;            }        }    }}