/ * W3 jmail v4.4 Professional gets yourself Google! or see http://community.9cbs.net/expert/topicview.asp?id=3739405
W3 Jmail v4.4 Professional is a COM component, I use C # to package its POP3 COM class into a .NET component for collecting mail: implement the // Synchronous event public event messageeseventhandler messageReceReceiVed; // An email has been received To the local public event messageseageseventhandler messagereceive; // An email is arriving // can be used to charge each attachment for email processing public event attachmentseventhandler attachmentReceive; // An attachment of an email is arrived
// A abnormal event occurs public event message; public event attachmentseventhandler attachmentException;
Therefore, in the main adjustment program of the component, it can be different according to the results of the analysis email or attachment!
Will all code: 1. Copy to the new "Console Application" item of Microsoft Visual Studio .NET 2003 2. Then add the reference JMail 4.x library! Jmail's registration method, run the command line : Regsvr32 E: /JMAIL/JMAIL.DLL3.F5 run
Or all code will be as follows: 1. Copy to any * .cs file saved in any * .cs file! (Such as: e: /temp/njmail.cs) 2. Generate a .NET program using the TLBIMP.EXE (Type Library Import) utility Set the command line: (in the MS VS.NET installation directory: e: /msvs.net/sdk/v1.1/bin/tlbimp.exe) TLBIMP.EXE E: /JMAIL/JMAIL.DLL / OUT: E : /temp/jmail.net.dll / name: Jmail generated jmail.net.dll with * .cs files in the same directory! 3. Execute the CSC command line compile * .cs compiled into EXE: (here is demonstration The test effect is compiled into an exe) csc.exe njmail.cs /r :jmail.net.dll compiles to a DLL, you can add a reference from other .NET programs: CSC.exe / T: library njmail.cs / R: Jmail. Net.dll
(Of course, you can also compile the code under Namespace Microshaoft.njmail to DLL) * /
Namespace microshaoft.njmail {// using jmail; // useing system;
Public class pop3 {public delegate void MessageseVentHandler (MessageSstate OmessageSstate); Public Delegate Void AttachmentSeventHndler (AttachmentsState oattachmentSstate);
// Asynchronous event Public Event MessageseesementHandler MessagesReceviceAnc; Public Event AttachmentSeventhandler AttachmentsRecEventageNC;
// synchronization event public event MessagesEventHandler MessageReceived; public event MessagesEventHandler MessageReceive; public event AttachmentsEventHandler AttachmentReceive; abnormal event public event MessagesEventHandler MessageException // occur; public event AttachmentsEventHandler AttachmentException;
Private string _username; private string _password; private string _server; private int _port = 110;
Private static Object _lockObject = new Object ();
Public POP3 (String Username, String Password, String Server, int port) {this._username = username; this._parsword = password; this._server = server; THIS._PORT = port;
Public POP3 (String Username, String Password, String Server) {this._username = username; this._password = password; this._server = server;}
Public string username {get {return_username;} set {_username = value;}}
Public string password {get {return _password;} set {_password = value;}}
Public String Server {get {returb_server;} set {_server = value;}}
Public int port {get {return _port;} set {_port = value;}} // [mtathread] public void execute () {this.execute (false);
public void Execute (bool IsAsync) {jmail.POP3Class pop3 = new jmail.POP3Class (); try {pop3.Timeout = 0; pop3.Connect (this._UserName, this._Password, this._Server, this._Port); jmail .Mersclass JMS = (jmail.MESSAGESCLASS) POP3.MESSAGES; INT i = jms.count; MessagesState oms = null; for (int i = 0; i
} Catch (System.Exception e) {if (this.AttachmentException = null!) {Oass.Exception = e; this.AttachmentException (oass); if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.CancelAll) {break; } else if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry) {j--;} else if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore) {// continue;} else if (oass. ExceptionAction == Microshaoft.NJMail.ExceptionActions.Throw) {throw e;}} else {throw e;}}} if (this.MessageReceived = null) {this.MessageReceived (omss);!}}}}} catch (System .Exception ! E) {if (this.MessageException = null) {omss.Exception = e; this.MessageException (omss); if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.CancelAll) {break;} else if (omss. ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry) {i--;} else if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore) {// continue;} else if (omss.ExceptionAction == Microshaoft.NJMail {Throw e;}} else {throw e;
}}}} Catch (system.exception e) {throw e;} finally {pop3.disconnect (); pop3 = null;} //system.console.writeline ("main end ");} // [mtathread] Private void MessageExecuteAsync (MessagesState oMessagesState) {int J = oMessagesState.jMessage.Attachments.Count; for (int j = 0; j // [mtathread] private void attachmentexecuteasync (attachmentsstate oattementsstate) {//} // [MTAThread] private void OnMessageCallBack (System.IAsyncResult iar) {MessagesState omss = (MessagesState) iar.AsyncState; if (! This.MessagesReceiveAsync = null) {if (omss.jMessage.Attachments.Count == 0) {this .MESSAGESRECEIVEASYNC (OMSS);}}} // [MTAThread] private void OnAttachemnetCallBack (System.IAsyncResult iar) {AttachmentsState oass = (AttachmentsState) iar.AsyncState; if (this.AttachmentsReceiveAsync = null!) {This.AttachmentsReceiveAsync (oass);} if (! Oass.CancelCurrent) {try {oass.SaveToFile (oass.FileName);} catch (System.Exception e) {oass.Exception = e; if (AttachmentException = null!) {AttachmentException (oass); if (oass.ExceptionAction == Microshaoft.NJMail .ExceptionActions.cancelall) { } Else if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry) {this.OnAttachemnetCallBack ((System.IAsyncResult) oass);} else if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore) {} else if (oass.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Throw) {throw e;}}}} if (! this.MessagesReceiveAsync = null) {if (oass.AttachmentsCount == 0) {this.MessagesReceiveAsync (oass.MessagesState }}}} public class MessagesState // Messages state {private static object _LockObject = new object (); private int _MessageID; private jmail.MessageClass _jMessage; private jmail.MessagesClass _jMessages; private jmail.POP3Class _jPOP3; private string _FilePath; private bool _CancelCurrent; Private system.exception_exception; private exceptionAnch_exception; Public ExceptionActions ExceptionAction {get {return_exceptionaction;} set {this._excectionAction = value;}} Public system.exception exception {get {return _exception;} set {this._exception = value;}} Public string filepath {get {return this._filepath;}} INTERNAL VOID SETFILEPATH (String FilePath) {this._filepath = filepath;} Public Bool Cancelcurrent {Get {Return THIS_CANCELCURRENT;} set {this._cancelcurrent = value;}} Public int messagescount // Number of messages that have not been processed {GET {//lock (MessageSstate._lockObject) {Return this._jimentages.count - this._messageId - 1;}}} Public JMail.MessagesClass Jimentages {Get {Return THIS._JMESSAGES;} Public JMail.MessageClass Jimentage {Get {Return this._jimentage;}} public int match_essageId;}} internal MessagesState (int MessageID, jmail.MessageClass jMessage, jmail.MessagesClass jMessages, jmail.POP3Class jPOP3) {this._MessageID = MessageID; this._jMessage = jMessage; this._jMessages = jMessages; this._jPOP3 = jPOP3;} Public void deleteSingleMessage () {Lock (MessageSstate._lockObject) {this.deleteSingleMessage (this._messageid);}} Public void deleteSingleMessage (INT messageID) {Lock (MessageSstate._lockObject) {this._jpop3.deleteSingleMessage (MessageID);}} Public void deleteMedes () {lock (message._lockobject) {this._jpop3.deleteMessages ();}}}}}}}}}}}}}}}}} Public Enum ExceptionActions {Cancelall, Ignore, Retry, Throw} public class AttachmentsState // Attachments state {private MessagesState _MessagesState; private int _AttachmentID; private string _FileName; private static object _LockObject = new object (); private jmail.AttachmentClass _jAttachment; private bool _CancelCurrent; private System.Exception _Exception; private ExceptionActions _ExceptionAction; Public ExceptionActions ExceptionAction {get {return_exceptionaction;} set {this._excectionAction = value;}} Public system.exception exception {get {return _exception;} set {this._exception = value;}} Public Bool Cancelcurrent {Get {Return THIS_CANCELCURRENT;} set {this._cancelcurrent = value;}} Public Jmail.attachmentClass Jattachment {Get {Return this._jattachment;}} public int AttachmentsCount // number of messages has not been processed attachment {get {//lock(AttachmentsState._LockObject) {return this._MessagesState.jMessage.Attachments.Count - this._AttachmentID - 1;}}} public string FileName {get {return THIS._FILENAME;} set {this._filename = value;}} Public MessagesState MessageState {get {return this._MessageSstate;}} Public int attachmentid {get {return this._attachmentId;}} Public void savetofile (String filename) {// if (! this.cancelcurrent) {this._jattachment.savetofile (filename);}} internal AttachmentsState (int AttachmentID, MessagesState oMessagesState) {this._MessagesState = oMessagesState; this._AttachmentID = AttachmentID; this._jAttachment = (jmail.AttachmentClass) oMessagesState.jMessage.Attachments [AttachmentID]; this._FileName = System.String.Format ( "[{0}]. {1}. [{2}]. {3}", OMESSAGESSTATE.MESSAGEID, OMESSAGESSTATE.JMESSAGE.SUBJECT, ATTACHMENTID, this._jattachment.name;}}} / / =========================================================================================================================================================================================== ======================================================= // Console Test Program: Namespace ConsoleApplicationTest {Using Microshaoft.njmail; Using Pop = Microshaoft.njmail.Pop3; Class Apptest {Pop Pop1; Bool Isbusy = FALSE Static void main () { // call the Windows test program System.Console.WriteLine ( "WindowsApplicationTest Begin:"); WindowsApplicationTest.Form1.Main0 (); System.Console.WriteLine ( "WindowsApplicationTest End."); System.Console.WriteLine ( "ConsoleApplicationTest Begin: "); Apptest a = new apptest (); a.pop1 = new pop (" username "," password "," mail.xxx.com "); // Subscribe to asynchronous event a.Pop1.MessageSreceviceAnc = New Microshaoft. NJMail.POP3.MessagesEventHandler (a.Pop1_MessagesReceiveAsync); a.Pop1.AttachmentsReceiveAsync = new Microshaoft.NJMail.POP3.AttachmentsEventHandler (a.Pop1_AttachmentsReceiveAsync); // subscribe to a synchronization event a.Pop1.MessageReceived = new Microshaoft.NJMail.POP3 .MessagesEventHandler (a.Pop1_MessageReceived); a.Pop1.AttachmentReceive = new Microshaoft.NJMail.POP3.AttachmentsEventHandler (a.Pop1_AttachmentReceive); a.Pop1.MessageReceive = new Microshaoft.NJMail.POP3.MessagesEventHandler (a.Pop1_MessageReceive); // Subscribe to Exception event a.pop1.attachmentException = new microshaoft.njmail.pop3.attachmentseventhandler (A.POP1_ Attachmentexception; a.run (); system.timers.timer t = new system.timers.timer (); t.interval = 1000 * 30; // Take a message T.Nabled = true every 30 seconds; t.ELAPSED = new system.timers.ELAPSEDEVENTHANDLER (A.T_ELAPSED); while (system.console.readline (). TOLOWER ()! = "exit") {system.console.writeline ("Press /" exit / "to EXIT THIS Programe! ");}} Private void t_elapsed (Object sender, system.timers.lapsendeventargs e) {this.run ();} Private void run () {if (! isbusy) {system.console.writeline ("busy"); this.isbusy = true; //this.pop1.execute (true); // Executive this.Pop1.execute ( ); // Synchronize the trigue; system.console.writeline ("iDle"); system.console.writeline ("Press /" exit / "to exit this programe!");}} Private void pop1_messageSreceiveasync MessagesState oMessagesState) {System.Console.WriteLine ( "Message:.! [{0}] {1} of {2} Messages have been recieved {3}", oMessagesState.MessageID, oMessagesState.jMessage.Subject, oMessagesState.jMessages. Count - 1, OMESSAGESSTATE.MESSAGESCOUNT); if (OmessageSstate.Messagescount == 0) {System.Console.writeline ("All Messages Have Been Recieved!");}} private void Pop1_AttachmentsReceiveAsync (AttachmentsState oAttachmentsState) {oAttachmentsState.SaveToFile (@ "E: / jmailAttachments / [". System.Guid.NewGuid () ToString () "]." oAttachmentsState.FileName); oAttachmentsState.FileName = @ " E: / jmailattachments / [" system.guid.newguid (). Tostring () "]. " Oattachmentsstate.FileName; if (oAttachmentsState.AttachmentsCount == 0) {System.Console.WriteLine (, oAttachmentsState.MessagesState.MessageID, oAttachmentsState.MessagesState.jMessage.Subject "[{0}] {1} have been recieved.!"); // asynchronous Perform deletion with errors //oattachmentSstate.MessageSstate.DeleteSingleMessage (oAttachmentsState.MessageSstate.MESSAGEID);} //oattachmentsState.canceLcurRrent = true;} private void Pop1_MessageReceived (MessagesState oMessagesState) {System.Console.WriteLine ( "Message:.! [{0}] {1} of {2} Messages have been recieved {3}", oMessagesState.MessageID, oMessagesState.jMessage.Subject, oMessagesState.jMessages.Count - 1, oMessagesState.MessagesCount); // can each receive a complete message is deleted //oMessagesState.DeleteSingleMessage(oMessagesState.MessageID);} private void Pop1_AttachmentReceive (AttachmentsState oAttachmentsState) {//oAttachmentsState.CancelCurrent = true; oAttachmentsState.SaveToFile (@ "E: / jmailAttachments / [" System.Guid.NewGuid () ToString () . "]." oAttachmentsState.FileName); oAttachmentsState.FileName = @ "E: / jmailAttachments / [ " System.guid.newguid (). Tostring () "]. " Oattachmentsstate.filename; //oattachmentsstate.cancelcurrent = true; PRIVATE VOID POP1_MESSAGERECEIVE (MessageSstate OmessageSstate) {//00ssagesstate.cancelcurrent = false;} private void Pop1_AttachmentException (Microshaoft.NJMail.AttachmentsState oAttachmentsState) {System.Console.WriteLine ( "Execute Exception: {0}", oAttachmentsState.Exception.Message); oAttachmentsState.FileName = "e: // temp // copy of" oAttachmentsState.jAttachment.Name; oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Retry; // a document name after reprocessing oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Ignore; // process a next attachment //oAttachmentsState.ExceptionAction = Microshaoft.njmail.exceptionactions.THROW;}}} / / =========================================================================================================================================================================================== ======================================================= // Windows Test Program: Namespace WindowsApplicationTest {Using System; Using System.Windows.Forms; /// // // Todo: Add any constructor code after INITIALIZEComponent call //} /// #REGION Windows Form Designer The code /// /// } Private void Timer1_Tick (Object Sender, System.EventArgs E) {//x.execute (); new system.threading.thread (new system.threading.threadstart (this.threadproc)). Start ();} private void x_MessageReceived (Microshaoft.NJMail.MessagesState oMessagesState) {if (oMessagesState.MessagesCount == 0) {//System.Windows.Forms.MessageBox.Show("game over ");} else {System.Console.WriteLine (oMessagesState .MessageID.ToString ());}} private void x_AttachmentReceive (Microshaoft.NJMail.AttachmentsState oAttachmentsState) {oAttachmentsState.FileName = "e: // temp //" oAttachmentsState.FileName; oAttachmentsState.SaveToFile (oAttachmentsState.FileName); if (oattachmentsstate.exception! = null) {throw oattachmentsstate.exception;} oAttachmentsState.CancelCurrent = true; // Save processing is not currently attachments if (oAttachmentsState.AttachmentsCount == 0) {//System.Windows.Forms.MessageBox.Show (oAttachmentsState.MessagesState.jMessage.Attachments.Count.ToString ()); } Else {system.console.writeline (oattachmentsstate.attachmentid.tostring ());}} private void x_AttachmentException (Microshaoft.NJMail.AttachmentsState oAttachmentsState) {System.Console.WriteLine ( "Execute Exception: {0}", oAttachmentsState.Exception.Message); oAttachmentsState.FileName = "e: // temp // copy of" oAttachmentsState.jAttachment.Name; //oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Retry; // a document name after reprocessing oAttachmentsState.ExceptionAction = Microshaoft.NJMail.ExceptionActions.Ignore; // process a next attachment // oAttachmentsState .ExceptionAction = microshaoft.njmail.exceptionactions.throw;}}}