使用c#中的控制台应用程序读取Gmail电子邮件附件

时间:2013-09-28 04:27:13

标签: c#

在c#中使用控制台应用程序读取Gmail电子邮件附件并移动到我的计算机中的任何文件夹..我尝试过POp3Client,TCpclient& Smtp ..他们都在网络应用程序中工作。但我只需要在c#

中使用控制台应用程序

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;

namespace FetchEmailFromGmail
{
    class Program
    {
        static void Main(string[] args)
        {
            // create an instance of TcpClient 
            TcpClient tcpclient = new TcpClient();
            tcpclient.Connect("pop.gmail.com", 995);
            System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
            sslstream.AuthenticateAsClient("pop.gmail.com");

            StreamWriter sw = new StreamWriter(sslstream);
            System.IO.StreamReader reader = new StreamReader(sslstream);
            sw.WriteLine("USER someaccount@gmail.com"); sw.Flush();

            sw.WriteLine("PASS somepass"); sw.Flush();

            //sw.WriteLine("RETR 1");
            //sw.WriteLine("STAT ");
            sw.WriteLine("LIST ");
            sw.Flush();
            sw.WriteLine("Quit ");
            sw.Flush();
            string str = string.Empty;
            string strTemp = string.Empty;
            while ((strTemp = reader.ReadLine()) != null)
            {
                if (".".Equals(strTemp))
                {
                    break;
                }
                if (strTemp.IndexOf("-ERR") != -1)
                {
                    break;
                }
                str += strTemp;
            }
            Console.Write(str);
            reader.Close();
            sw.Close();
            tcpclient.Close(); // close the connection

            Console.ReadLine();
        }
    }
}

0 个答案:

没有答案