using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Text;
using anmar.SharpMimeTools;
using OpenPOP.POP3;
namespace SiteOffice.ExternalMail{
/// <summary>
/// Author: Joe Audette
/// Created: 2006-08-02
/// Last Modified: 2007-07-10
///
/// The use and distribution terms for this software are covered by the
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.
/// </summary>
public class EmailAccountHelper
{
#region Private Properties
private POPClient mailClient = null;
private String userName = String.Empty;
private String password = String.Empty;
private String popServerName = String.Empty;
private int popServerPort = 110;
private bool useSSL = true;
#endregion
#region Constructors
public EmailAccountHelper(
String name,
String userPassword,
String server,
int port)
{
userName = name;
password = userPassword;
popServerName = server;
popServerPort = port;
mailClient = new POPClient();
mailClient.UseSSL = useSSL;
}
public EmailAccountHelper()
{
// temporary testing
userName = ConfigurationManager.AppSettings["testEmailUserName"];
password = ConfigurationManager.AppSettings["testEmailPassword"];
popServerName = ConfigurationManager.AppSettings["testEmailPopServerName"];
}
#endregion
#region Public Methods
public int GetMessageCount()
{
if (mailClient != null)
{
mailClient.Disconnect();
mailClient.UseSSL = this.useSSL;
mailClient.Connect(this.popServerName, this.popServerPort);
mailClient.Authenticate(this.userName, this.password);
int count = mailClient.GetMessageCount();
mailClient.Disconnect();
return count;
}
return 0;
}
public int GetMessageCount(int pageSize, out int totalPages)
{
totalPages = 1;
if (mailClient != null)
{
mailClient.Disconnect();
mailClient.UseSSL = this.useSSL;
mailClient.Connect(this.popServerName, this.popServerPort);
mailClient.Authenticate(this.userName, this.password);
int totalMessages = mailClient.GetMessageCount();
mailClient.Disconnect();
if (pageSize > 0) totalPages = totalMessages / pageSize;
if (totalMessages <= pageSize)
{
totalPages = 1;
}
else
{
int remainder;
Math.DivRem(totalMessages, pageSize, out remainder);
if (remainder > 0)
{
totalPages += 1;
}
}
return totalMessages;
}
return 0;
}
public List<SharpMessage> GetPage(
int pageNumber,
int pageSize,
out int totalPages)
{
totalPages = 1;
List<SharpMessage> inboxMessages
= new List<SharpMessage>();
mailClient.Disconnect();
mailClient.UseSSL = this.useSSL;
mailClient.Connect(this.popServerName, this.popServerPort);
mailClient.Authenticate(
this.userName,
this.password,
AuthenticationMethod.TRYBOTH);
int totalMessages = mailClient.GetMessageCount();
if (pageSize > 0) totalPages = totalMessages / pageSize;
if (totalMessages <= pageSize)
{
totalPages = 1;
}
else
{
int remainder;
Math.DivRem(totalMessages, pageSize, out remainder);
if (remainder > 0)
{
totalPages += 1;
}
}
int lastMessageToGet;
if (pageNumber == 1)
{
lastMessageToGet = totalMessages;
}
else
{
lastMessageToGet = totalMessages - (pageSize * pageNumber);
}
int firstMessageToGet = lastMessageToGet - pageSize;
for (int i = lastMessageToGet; i >= firstMessageToGet; i -= 1)
{
SharpMessage message
= mailClient.GetMessage(i, false);
if (message != null)
{
inboxMessages.Add(message);
}
//message.
}
mailClient.Disconnect();
return inboxMessages;
}
#endregion
//public DataTable GetInBox()
//{
// DataTable dtInBox = new DataTable();
// dtInBox.Columns.Add("MessageID", typeof(String));
// dtInBox.Columns.Add("From", typeof(String));
// dtInBox.Columns.Add("Subject", typeof(String));
// dtInBox.Columns.Add("Date", typeof(String));
// //Utility.Log=true;
// mailClient.Disconnect();
// mailClient.UseSSL = this.useSSL;
// mailClient.Connect(this.popServerName, this.popServerPort);
// mailClient.Authenticate(
// this.userName,
// this.password,
// AuthenticationMethod.TRYBOTH);
// int Count = mailClient.GetMessageCount();
// if (Count > 20) Count = 20;
// for (int i = Count; i >= 1; i -= 1)
// {
// SharpMessage message
// = mailClient.GetMessage(i, false);
// if (message != null)
// {
// DataRow row = dtInBox.NewRow();
// row["MessageID"] = message.MessageID;
// row["From"] = message.From;
// row["Subject"] = message.Subject;
// row["Date"] = message.Date;
// dtInBox.Rows.Add(row);
// }
// }
// mailClient.Disconnect();
// return dtInBox;
//}
}
}
|