private List<emailMessage> GetEmailsFromSMTPServer(string server, string username, string password, int port, bool ssl)
{
Messaging.EMail.POP3.Pop3MailClient pop = new Pop3MailClient(server, port, ssl, username, password);
List<emailMessage> messages = new List<emailMessage>();
try
{
Regex date = new Regex(@"(?:Date:(?<date>.+))");
Regex from = new Regex(@"(?:From:(?<from>.+))");
Regex to = new Regex(@"(?:To:(?<to>.+))");
Regex subject = new Regex(@"(?:Subject:(?<subject>.+))");
Regex dateRegex = new Regex(@"\w+,(?<date>[^\+\-]+)[\+\-].+");
Regex matchSubject = new Regex(@"\#+(?<name>[^\#]+)\#+(?<id>[^\#]+)\#\#+");
string message = "";
pop.Connect();
pop.IsAutoReconnect = true;
pop.ReadTimeout =180000;
int NumberOfMails, MailboxSize;
pop.GetMailboxStats(out NumberOfMails, out MailboxSize);
if (NumberOfMails > 0)
{
for (int i = 1; i <= NumberOfMails; i++)
{
emailMessage m = new emailMessage();
pop.GetRawEmail(i, out message);
m.mailindex = i;
m.subject = subject.Match(message).Groups[1].Value;
try
{
m.date = Convert.ToDateTime(dateRegex.Match(date.Match(message).Groups[1].Value).Groups[1].Value);
}
catch
{
m.date = DateTime.Now;
}
m.body = GetEmailBody(message);