1

仅在 c# 中使用控制台应用程序读取 Gmail 电子邮件附件并移动到我计算机中的任何文件夹..我尝试了 POp3Client、TCpclient 和 Smtp..它们都在 Web 应用程序中工作..但我只需要 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();
        }
    }
}
4

0 回答 0