using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using anmar.SharpMimeTools;
using log4net;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using SiteOffice.Business.ExternalMail;
namespace SiteOffice.ExternalMail.Services{
/// <summary>
/// Author: Joe Audette
/// Created: 2007-06-25
/// Last Modified: 2007-06-25
///
/// 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>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PopMailRetriever : IHttpHandler
{
private static readonly ILog log
= LogManager.GetLogger(typeof(PopMailRetriever));
private Guid accountGuid = Guid.Empty;
// 25697cea-0756-4d35-8eda-f9f8e889d08c = yahoo
private SiteUser siteUser;
public void ProcessRequest(HttpContext context)
{
LoadSettings(context);
SendResponse(context);
}
private void SendResponse(HttpContext context)
{
if (
(siteUser != null)
&& (accountGuid != Guid.Empty)
)
{
UserEmailAccount userEmailAccount
= new UserEmailAccount(accountGuid);
if (userEmailAccount.UserGuid == siteUser.UserGuid)
{
SendMail(context, userEmailAccount);
}
else
{
SendDefaultXml(context);
}
}
else
{
SendDefaultXml(context);
}
}
private void SendMail(
HttpContext context,
UserEmailAccount userEmailAccount)
{
EmailAccountHelper emailAccount
= new EmailAccountHelper(
userEmailAccount.UserName,
userEmailAccount.Password,
userEmailAccount.Pop3Server,
userEmailAccount.Pop3Port);
int pageNumber = 1;
int pageSize = 20;
int totalPages = 1;
List<SharpMessage> inboxMessages = new List<SharpMessage>();
try
{
inboxMessages
= emailAccount.GetPage(
pageNumber,
pageSize,
out totalPages);
}
catch { }
context.Response.ContentType = "application/xml";
Encoding encoding = new UTF8Encoding();
context.Response.ContentEncoding = encoding;
XmlTextWriter xmlTextWriter = new XmlTextWriter(context.Response.OutputStream, encoding);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("messages");
xmlTextWriter.WriteStartAttribute("accountid");
xmlTextWriter.WriteValue(accountGuid.ToString());
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("isauthenticated");
xmlTextWriter.WriteValue(context.Request.IsAuthenticated.ToString());
xmlTextWriter.WriteEndAttribute();
foreach (SharpMessage message in inboxMessages)
{
xmlTextWriter.WriteStartElement("m");
xmlTextWriter.WriteStartAttribute("id");
xmlTextWriter.WriteValue(message.MessageID);
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("subject");
xmlTextWriter.WriteValue(message.Subject);
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("from");
xmlTextWriter.WriteValue(message.FromAddress);
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteCData(message.Body);
xmlTextWriter.WriteEndElement(); //m
}
xmlTextWriter.WriteEndElement(); //messages
//end of document
xmlTextWriter.WriteEndDocument();
xmlTextWriter.Close();
}
private void SendDefaultXml(HttpContext context)
{
context.Response.ContentType = "application/xml";
Encoding encoding = new UTF8Encoding();
context.Response.ContentEncoding = encoding;
XmlTextWriter xmlTextWriter = new XmlTextWriter(context.Response.OutputStream, encoding);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("messages");
xmlTextWriter.WriteStartAttribute("accountid");
xmlTextWriter.WriteValue(accountGuid.ToString());
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("isauthenticated");
xmlTextWriter.WriteValue(context.Request.IsAuthenticated.ToString());
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartElement("m");
xmlTextWriter.WriteStartAttribute("id");
xmlTextWriter.WriteValue("messageidgoeshere");
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("subject");
xmlTextWriter.WriteValue("subject");
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteStartAttribute("from");
xmlTextWriter.WriteValue("from");
xmlTextWriter.WriteEndAttribute();
xmlTextWriter.WriteCData(" <xml is not supposed to have stuff like this");
xmlTextWriter.WriteEndElement(); //m
xmlTextWriter.WriteEndElement(); //messages
//end of document
xmlTextWriter.WriteEndDocument();
xmlTextWriter.Close();
}
private void LoadSettings(HttpContext context)
{
if (context.Request.Params.Get("accountid") != null)
{
string strAcct = context.Request.Params.Get("accountid");
if (strAcct.Length == 36)
{
try
{
accountGuid = new Guid(context.Request.Params.Get("accountid"));
}
catch (FormatException)
{ }
}
siteUser = SiteUtils.GetCurrentSiteUser();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
|