Package JMail 4.4 POP3 is "event" in .NET components (.dll assembly), implementing "email (accessories) arrive&qu

xiaoxiao2021-03-06  15

/ * W3 Jmail v4.4 Professional gets Google! or see http://community.9cbs.net/expert/topicview.asp?id=3739405 W3 Jmail v4.4 Professional is a COM component, I use C # Package its POP3 COM class into a .NET component for collecting emails: implement / / synchronization event public event messageseageseventhandler messagereceive; / receive mail attachments can be used for each treatment public event AttachmentsEventHandler AttachmentReceive; // attachment of an email is arriving abnormal event public event MessagesEventHandler MessageException // occur; public event AttachmentsEventHandler AttachmentException; therefore in the calling program calls the assembly Different processing according to the results of the subject or attachment of the analysis email! All code will be as follows: 1. Copy to the new "Console Application" item of Microsoft Visual Studio .NET 2003. 4.X Library! Jmail's registration method, run the command line: Regsvr32 E: /JMAIL/JMAIL.DLL3.F5 run or you will be all code: 1. Copy to any * .cs file saved in any * .cs file! (Such as: E: E: E: /TEMP/njmail.cs 2. Use the TLBIMP.EXE utility to generate a .NET assembly execute 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 and * .cs The file is placed in the same directory! 3. Execute the CSC life To compile * .cs Compile to EXE: (here is to demonstrate the test effect, compiled into an exe) csc.exe njmail.cs /r :jmail.net.dll compile into 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; // Using System; public class POP3 {public delegate void MessagesEventHandler (MessagesState oMessagesState); public delegate void AttachmentsEventHandler (AttachmentsState oAttachmentsState); // asynchronous event public event MessagesEventHandler MessagesReceiveAsync; public event AttachmentsEventHandler AttachmentsReceiveAsync; // synchronization event public event MessagesEventHandler MessageReceived; public event MessagesEventHandler MessageReceive; Public Event AttachmentSeventHandler AttachmentReceive;

Abnormal event occurs public event MessagesEventHandler MessageException //; 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._Password = Password; this._Server = Server; this._Port = Port;} public POP3 (string UserName, string Password, string Server) {this._UserName = UserName; this._Password = Password; this._Server = Server;} // [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.MessagesClass JMS = (jmail.MessagesClass) pop3.MESSAGES; INT i = jms.count; MessageSstate OMSS = NULL; for (int i = 0; I

0; J

} Else if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Retry) {i--;} else if (omss.ExceptionAction == Microshaoft.NJMail.ExceptionActions.Ignore) {// continue;} else if (omss. ExceptionAction == microshaoft.njmail.exceptionActions.throw) {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

} If (! ".Cancelcurrent) {Try {OASS.SAVETOFILE (OASS.FILENAME);} catch (system.exception e) {OASS.EXCEPTION = E; if (attachmentExce! = 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.Messag eClass _jMessage; private jmail.MessagesClass _jMessages; private jmail.POP3Class _jPOP3; private string _FilePath; private bool _CancelCurrent; private System.Exception _Exception; private ExceptionActions _ExceptionAction; public ExceptionActions ExceptionAction {get {return _ExceptionAction;} set {this._ExceptionAction = 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 number of messages has not been processed MessagesCount // {get {//lock(MessagesState._LockObject) {return this._jMessages. Count - this._MessageID - 1;}}} public jmail.MessagesClass jMessages {get {return this._jMessages;}} public jmail.MessageClass jMessage {get {return this._jMessage;}} public int MessageID {get {return this. _MessageID;}} 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._j POP3.DeleteSingleMessage (MessageID);}} public void DeleteMessages () {lock (MessagesState._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._exceptionAction = 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;}} the number of messages not yet public int AttachmentsCount // attachment process {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 MessagesState {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, MessageSstat) e oMessagesState) {this._MessagesState = oMessagesState; this._AttachmentID = AttachmentID; this._jAttachment = (jmail.AttachmentClass) oMessagesState.jMessage.Attachments [AttachmentID]; this._FileName = System.String.Format ( "[{0}] {1}]. {3} ", OMESSAGESSTATE.MESSAGEID, OMESSAGESSTATE.JMESSAGE.SUBJECT, AttachmentId, this._jattachment.name;}}} // ========== ============================================================================================================================================================================================================= ==========

===========================// Console test program: namespace consoleapplicationtest {using microshaoft.njmail; use 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.Conle.writeline ("ConsoleApplicationTest Begin:"); Apptest A = New Apptest (); A.POP1 = New Pop ("UserName", "Password", "mail.xxx.com" ); // subscribe to asynchronous events a.Pop1.MessagesReceiveAsync = new Microshaoft.NJMail.POP3.MessagesEventHandler (a.Pop1_MessagesReceiveAsync); a.Pop1.AttachmentsReceiveAsync = new Microshaoft.NJMail.POP3.AttachmentsEventHandler (a.Pop1_AttachmentsReceiveAsync); / / subscribe synchronous 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 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; T.ELAPSED = New System. Timers.ELAPSEDEVENTHANDLER (A.T_ELAPSED); while (system.console.readline (). TOLOWER ()! = "Exit"

) {System.console.writeline ("Press /" exit / "to exit this program!");}} Private void t_elapsed (object sender, system.timers.e) {this.run ();} private void () {If (! Isbusy) {system.console.writeline ("busy"); this.isbusy = true; //this.pop1.execute (true); // Execute this.Pop1.execute (); / / synchronous execution this.IsBusy = false; 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.SaveTo File (@ "E: / jmailattachments / [" system.guid.newguid (). ToString () "]." OattachmentState.FileName); oattachmentsstate.FileName = @ "e: / jmailattachments / [" system. Guid.newguid (). TOSTRING () "]." OattachmentsState.FileName; if (oattachmentsstate.attachmentscount == 0) {system.console.writeline ("[{0}]. {1} Have Been Recieved!" , oAttachmentsState.MessagesState.MessageID, oAttachmentsState.MessagesState.jMessage.Subject); // delete asynchronous error //oAttachmentsState.MessagesState.DeleteSingleMessage(oAttachmentsState.MessagesState.MessageID);

} //OAttachmentsState.CancelCurrent = 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) { //MASSSSSTATE.CANCELCURRENT = false;} private void pop1_attachmentException (micro shaoft.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;}}} // =====================

============================================================================================================================================================================================================= ====================================================================================================================== /// Form1 summary description. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; public Form1 ( ) {// // Windows Form Designer Support for // InitializationComponent (); // /// Todo: Add any constructor code after INITIALIZECOMPONENT call //} ///

/// Clean up all Resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer /// /// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.components = new System.ComponentModel.Container (); this.button1 = new System.Windows.Forms.Button (); this.timer1 = new System.Windows. Forms.timer (this.components); this.suspendlayout (); // // button1 // this.button1.location = new system.drawing.point (24, 24); this.button1.name = "button1"; This.Button1.Size = new system.drawing.size (80, 40); this.button1.tabindex = 0; this.button1.text = "button1"; this.button1.click = new System.EventHandler (this. Button1_click; // // Timer1 // this.timer1.tick = new system.EventHandler (this.timer1_tick); // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); This.ClientSize = new system.drawing.size (292, 273); this.controls.add (this.button1); this.name = "form1"; this.text = "form1"; this.ResumeLayout (false); } #ENDREGION ///

/// Application of primary invested points.

/// [stathread] public static void main0 () // main () // To replace main () {Application.Run (New Form1 ());} private microshaoft .Njmail.pop3 x; private void button1_click (Object sender, system.eventargs e) {Button1.enabled = false; x = new microshaoft.njmail.pop3 ("Username", "Password", "mail.xxx.com") ; x.MessageReceive = new Microshaoft.NJMail.POP3.MessagesEventHandler (x_MessageReceive); x.MessageReceived = new Microshaoft.NJMail.POP3.MessagesEventHandler (x_MessageReceived); x.AttachmentReceive = new Microshaoft.NJMail.POP3.AttachmentsEventHandler (x_AttachmentReceive ); x.AttachmentException = new Microshaoft.NJMail.POP3.AttachmentsEventHandler (x_AttachmentException); timer1.Interval = 1000 * 120; // receive mail once every 120 seconds timer1.Enabled = false; x.Execute (); button1.Enabled = true;} private void x_messagereceive (microshaoft.njmail.MessageSstate OmessageSstate) {//system.windows.forms.MessageBox.show (OmessageSstate.MessageId.Tostring ()); System.console.writeline ("{0}," {1} "", OMESSAGESSTATE.MESSAGEID.TOSTRING (), OMESSAGESSTATE.JMESSAGE.SUBJECT); //system.console.writeline(OMESSTATE.JMESSAGE.BODY); // System.Console.WriteLine (oMessagesState.jMessage.Text);} private void timer1_Tick (object sender, System.EventArgs e) {x.Execute ();} 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 ());

转载请注明原文地址:https://www.9cbs.com/read-50012.html

New Post(0)