?? simpleemailclient.cs
字號:
// -----------------------------------------------------------------------
//
// Copyright (C) 2003-2005 Angel Marin
//
// This file is part of SharpWebMail.
//
// SharpWebMail is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpWebMail is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SharpWebMail; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// -----------------------------------------------------------------------
using System;
namespace anmar.SharpWebMail
{
internal abstract class SimpleEmailClient : anmar.SharpWebMail.IEmailClient {
protected static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private anmar.SharpWebMail.CTNSimpleTCPClient client;
/// <summary>
///
/// </summary>
protected System.Boolean connected;
/// <summary>
///
/// </summary>
protected System.Int32 portnumber;
/// <summary>
///
/// </summary>
protected System.String hostname;
/// <summary>
///
/// </summary>
protected System.String lastResponse;
/// <summary>
///
/// </summary>
protected System.String password;
/// <summary>
///
/// </summary>
protected System.String username;
/// <summary>
///
/// </summary>
protected System.String commandEnd;
/// <summary>
///
/// </summary>
protected bool responseEndOnEnd;
/// <summary>
///
/// </summary>
protected System.String responseEnd;
/// <summary>
///
/// </summary>
protected System.String responseEndSL;
/// <summary>
///
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="user"></param>
/// <param name="pass"></param>
public SimpleEmailClient( System.String host, System.Int32 port, System.String user, System.String pass ) {
client = new anmar.SharpWebMail.CTNSimpleTCPClient();
this.hostname = host;
this.password = pass;
this.portnumber = port;
this.username = user;
}
/// <summary>
///
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="user"></param>
/// <param name="pass"></param>
/// <param name="timeout"></param>
public SimpleEmailClient( System.String host, System.Int32 port, System.String user, System.String pass, long timeout ) {
client = new anmar.SharpWebMail.CTNSimpleTCPClient(timeout);
this.hostname = host;
this.password = pass;
this.portnumber = port;
this.username = user;
}
/// <summary>
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="args"></param>
/// <returns></returns>
protected abstract System.String buildcommand ( anmar.SharpWebMail.EmailClientCommand cmd, params System.Object[] args );
/// <summary>
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="args"></param>
/// <returns></returns>
protected abstract bool commandResponseTypeIsSL ( anmar.SharpWebMail.EmailClientCommand cmd, params System.Object[] args );
/// <summary>
///
/// </summary>
/// <returns></returns>
protected bool connect () {
bool error = false;
System.String response = null;
if (!this.connected) {
// Connect to email server
error = !client.connect( this.hostname, this.portnumber );
if (!error) {
this.connected = true;
error = !client.readResponse( ref response, responseEndSL, true );
error = (error)?true:!this.evaluateresponse( response );
this.lastResponse = response;
}
}
return !error;
}
/// <summary>
///
/// </summary>
/// <param name="mindex"></param>
/// <returns></returns>
protected virtual bool delete ( int mindex ) {
bool error = false;
// Send delete command for mindex message
System.String cmd = this.buildcommand(anmar.SharpWebMail.EmailClientCommand.Delete, mindex);
error = ( cmd.Equals(System.String.Empty) )?true:!this.sendCommand( anmar.SharpWebMail.EmailClientCommand.Delete, cmd );
return !error;
}
/// <summary>
///
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
protected virtual bool deletemessages ( System.Data.DataView result ) {
bool error = false;
foreach ( System.Data.DataRowView msg in result )
error = (error)?error:!this.delete ( (int)msg[1] );
return !error;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected bool disconnect () {
bool error = false;
if (this.connected) {
// Disconnect from email server
error = !client.closeConnection();
this.connected = false;
}
return !error;
}
/// <summary>
///
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
protected abstract bool evaluateresponse ( System.String response );
/// <summary>
///
/// </summary>
/// <param name="inbox"></param>
/// <param name="npage"></param>
/// <param name="npagesize"></param>
/// <param name="askserver"></param>
/// <returns></returns>
public virtual bool GetFolderIndex ( anmar.SharpWebMail.CTNInbox inbox, int npage, int npagesize, bool askserver ) {
bool error = false;
int total = 0;
int totalbytes = 0;
System.Collections.Hashtable list = new System.Collections.Hashtable();
if ( !askserver ) {
error = !inbox.buildMessageList ( list, npage, npagesize );
askserver = (!error&&list.Count>0)?!askserver:askserver;
}
if ( askserver ) {
System.Collections.Hashtable messages = new System.Collections.Hashtable();
error = !this.connect();
error = (error)?error:!this.login ( this.username, this.password );
error = (error)?error:!this.status ( ref total, ref totalbytes );
error = (error)?error:!this.getListToIndex ( list, total, inbox, npage, npagesize );
if ( !error && total>0 && list.Count>0 ) {
System.IO.MemoryStream header = null;
foreach ( System.Collections.DictionaryEntry msg in list ) {
error = (error)?error:!this.getMessageHeader ( out header, (int) msg.Key );
if ( !error )
messages.Add(msg.Value, header);
}
}
this.quit();
foreach ( System.Collections.DictionaryEntry item in messages ) {
System.IO.MemoryStream stream = this.getStreamDataPortion(item.Value as System.IO.MemoryStream);
anmar.SharpMimeTools.SharpMimeHeader header = new anmar.SharpMimeTools.SharpMimeHeader( stream, stream.Position );
header.Close();
inbox.newMessage ( item.Key.ToString(), header );
}
}
return !error;
}
private bool getListToIndex ( System.Collections.Hashtable msgs, int total, anmar.SharpWebMail.CTNInbox inbox, int npage, int npagesize ) {
bool error = false;
System.Int32[] list = new System.Int32[total];
System.String[] uidlist = new System.String[total];
if ( total>0 ) {
// Get uid list
error = (error)?error:!this.uidl ( uidlist, 0);
//Get messages list
error = (error)?error:!this.list ( list );
}
// Prepare message table with new messages
error = (error)?error:!inbox.buildMessageTable ( list, uidlist );
list = null;
uidlist = null;
//Determine what messages we have to index
if ( msgs!=null )
error = (error)?error:!inbox.buildMessageList ( msgs, npage, npagesize );
return !error;
}
/// <summary>
///
/// </summary>
/// <param name="Message"></param>
/// <param name="mindex"></param>
/// <param name="uidl"></param>
/// <returns></returns>
public bool GetMessage ( System.IO.MemoryStream message, int mindex, System.String uid ) {
bool error = false;
error = !this.connect();
error = (error)?true:!this.login ( this.username, this.password );
if ( !error && uid!=null ) {
System.String[] uidllist = new System.String[mindex];
error = !this.uidl( uidllist, mindex );
// Make sure mindex message is there and its UID is uid
if ( error || uidllist[mindex-1]==null || !uidllist[mindex-1].Equals(uid) ) {
error = true;
}
uidllist=null;
}
error = (error)?true:!this.retrieve ( mindex, message );
if ( !error )
message = this.getStreamDataPortion(message);
this.quit();
return !error;
}
/// <summary>
///
/// </summary>
/// <param name="Header"></param>
/// <param name="mindex"></param>
/// <returns></returns>
protected bool getMessageHeader ( out System.IO.MemoryStream stream, int mindex ) {
bool error = false;
stream = new System.IO.MemoryStream ();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -