Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using lumisoft. net. IMAP; using lumisoft. net. IMAP. client; using system. io; using lumisoft. net. mail; using system. net. mime; using system. net. mail; using lumisoft. net. mime; namespace emailtest {public partial class form1: FORM {publi C form1 () {initializecomponent ();} private void handle (Object sender, lumisoft. net. eventargs <imap_r_u> E) {console. writeline (E. value. tostring ();} private void button#click (Object sender, eventargs OE) {imap_client client = new imap_client (); try {// connect to the mail server by passing in the mail server address and the port number for the IMAP Protocol // SSL 993 other 143 // client. connect ("imap.qq.com", 993, true); // client. login ("[email protected]", "*******"); Client. connect ("imap.163.com", 143, false); client. login ("[email protected]", "*****"); client. getfolders (null ). tolist (). foreach (F => {console. writeline (F. foldername); var S = client. folderstatus (F. foldername); S. tolist (). foreach (SIT => {console. writeline ("Total: {0}, unread: {1}, recent {2}", sit. messagescount, sit. messagescount, sit. unseencount) ;}); client. selectfolder ("inbox"); var seqset = imap_t_seqs ET. parse ("1000: *"); var items = new imap_t_fetch_ I [] {New partition (), new imap_t_fetch_ I _uid (), new imap_t_fetch_ I _flags (), new partition (), new imap_t_fetch_ I _rfc822 ()}; // seqset valid client when the first fetch parameter is false. fetch (false, seqset, items, (S, e) =>{ try {var email = E. value as imap_r_u_fetch; // using (var ctx = new dbtentities () // {// var ent = new t_email (); // E NT. flags = Email. flags. flags. tostring (); // ent. receivedate = Email. internaldate. date; // ent. subject = Email. envelope. subject; // ent. uid = Email. UID. UID; // CTX. t_email.addobject (ENT); // CTX. savechanges (); //} console. writeline (""); console. writeline ("title:" + email. UID. UID + "," + email. internaldate. date + "," + email. envelope. subject); console. writeline ("------------ content ------------------------"); If (email. rfc822! = NULL) {email. rfc822.stream. position = 0; var mine = mail_message.parsefromstream (email. rfc822.stream); email. rfc822.stream. close (); // console. writeline (mine. bodyhtmltext); // console. writeline (mine. body. mediatype); If (mine. attachments. count ()> 0) {var list = mine. attachments. tolist (); foreach (var att in list) {var part = ATT. body as mime_ B _singlepartbase; string filename = @ "C: \ XX \" + Att. contenttype. param_name; file. writeallbytes (filename, part. data) ;}}} catch (exception ex) {console. writeline ("handle-err:" + ex. message) ;}}) ;}catch (exception ex) {console. writeline (ex. message) ;}finally {}}}}
View code
IMAP receive email