My simple POP3 client (set up for Gmail) isn't downloading all of my messages...
Any idea why?
I have 350 messages in my inbox and the program only says 296 of them show up.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.IO;
using System.Data;
namespace POP3Sharp
{
class Program
{
static void Main(string[] args)
{
PopGmail mail = new PopGmail();
Console.Write("Email: ");
string email = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
Console.WriteLine("Connecting.");
if (!mail.Connect("pop.gmail.com", 995))
{
Console.WriteLine("Connect failed, attempted connection to pop.gmail.com on port 995 (client).");
return;
}
Console.WriteLine("Connected.");
if (!mail.Login(email, password))
{
Console.WriteLine("Login failed, valid username/pass? POP3 enabled on gmail settings?");
return;
}
int messageCount = mail.GetMailCount();
Console.WriteLine("Number of email messages reported: " + messageCount.ToString());
//for(int i = 1; i <= messageCount; i++)
//{
// byte[] bt = Encoding.ASCII.GetBytes(mail.GetMessage(i));
// FileStream fs = new FileStream("Emails\\" + i.ToString() + ".txt", System.IO.FileMode.Create);
// fs.Write(bt, 0, bt.Length);
// fs.Close();
//}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
mail.Disconnect();
}
}
}
and more importantly,
POPGmail.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Security;
using System.Net.Sockets;
using System.Web;
using System.Windows.Forms;
namespace POP3Sharp
{
class PopGmail
{
TcpClient m_tcpClient = new TcpClient();
SslStream m_sslStream;
int m_mailCount;
byte[] m_buffer = new byte[8172];
public PopGmail()
{
m_mailCount = -1;
}
/// <summary>
/// Connect to pop3 server "host" using "port" and auth SSL as client.
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string host, int port)
{
m_tcpClient.Connect(host, port);
m_sslStream = new SslStream(m_tcpClient.GetStream());
m_sslStream.AuthenticateAsClient(host);
// Read the stream to make sure we are connected
int bytes = m_sslStream.Read(m_buffer, 0, m_buffer.Length);
return (Encoding.ASCII.GetString(m_buffer, 0, bytes).Contains("+OK"));
}
/// <summary>
/// Closes SSL and TCP connections.
/// </summary>
public void Disconnect()
{
//if(m_tcpClient.Connected)
// m_sslStream.Write(Encoding.ASCII.GetBytes("QUIT\r\n"));
m_sslStream.Close();
m_tcpClient.Close();
}
/// <summary>
/// Logs into the pop3 server (when connected.)
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public bool Login(string username, string password)
{
if (!m_tcpClient.Connected)
return false;
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
throw new ArgumentException("Username or Password was empty.");
int bytesRead = -1;
//Send the users login details
m_sslStream.Write(Encoding.ASCII.GetBytes("USER " + username + "\r\n"));
bytesRead = m_sslStream.Read(m_buffer, 0, m_buffer.Length);
if (!Encoding.ASCII.GetString(m_buffer, 0, bytesRead).Contains("+OK"))
return false;
//Send the password
m_sslStream.Write(Encoding.ASCII.GetBytes("PASS " + password + "\r\n"));
bytesRead = m_sslStream.Read(m_buffer, 0, m_buffer.Length);
if (!Encoding.ASCII.GetString(m_buffer, 0, bytesRead).Contains("+OK"))
return false;
return true;
}
/// <summary>
/// Returns the number of emails in the inbox.
/// </summary>
/// <returns></returns>
public int GetMailCount()
{
m_sslStream.Write(Encoding.ASCII.GetBytes("STAT\r\n"));
int bytesRead = m_sslStream.Read(m_buffer, 0, m_buffer.Length);
string data = Encoding.ASCII.GetString(m_buffer, 0, bytesRead);
//char [] str = Encoding.ASCII.GetChars(m_buffer);
//string data = str.ToString();
if (data.Contains("+OK"))
{
data = data.Remove(0, 4);
string [] temp = data.Split(' ');
int r;
if (Int32.TryParse(temp[0], out r))
{
m_mailCount = r;
return r;
}
}
return -1;
}
/// <summary>
/// Returns the message data as a string.
/// </summary>
/// <param name="messageNumber"></param>
/// <returns></returns>
public string GetMessage(int messageNumber)
{
string ret = "";
m_sslStream.Write(Encoding.ASCII.GetBytes("RETR " + messageNumber.ToString() + "\r\n"));
int bytesRead = -1;
while (bytesRead != 0 && !ret.Contains("\r\n.\r\n"))
{
bytesRead = m_sslStream.Read(m_buffer, 0, m_buffer.Length);
ret += Encoding.ASCII.GetString(m_buffer, 0, bytesRead);
}
ret = ret.Remove(0, ret.IndexOf("\r\n") + 2);
return ret;
}
}
}
All of the messages were sent in 2011.