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

本文介绍了一个简单的C#程序,该程序利用POP3协议来连接邮件服务器并获取新邮件的数量。通过实例展示了如何建立网络连接、发送认证信息及读取服务器响应。
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;
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值