/// <summary> ///receive new messages, do not delete old messages, write to the database after receiving messages/// </summary> Public Static voidGetnewmailintodatabase (stringUserName,stringPassWord,stringPopserver,intport,datetime maxdate) { stringCurpath =directory.getcurrentdirectory (); stringMailbox = String.Format ("{0}\\inbox", Curpath); //If The folder is not existed, create it. if(!directory.exists (mailbox)) {directory.createdirectory (mailbox); } mailserver oserver=Newmailserver (Popserver, UserName, PassWord, SERVERPROTOCOL.POP3); Mailclient oClient=NewMailclient ("Tryit"); //Enable SSL connection.Oserver.sslconnection =true; //Set 995 SSL PortOserver.port =995; Try{oclient.connect (oserver); Mailinfo[] Infos=Oclient.getmailinfos (); for(inti =0; I < Infos. Length; i++) {Mailinfo info=Infos[i]; Console.WriteLine ("Index: {0}; Size: {1}; UIDL: {2}", Info. Index, Info. Size, Info. UIDL); System.DateTime d=System.DateTime.Now; System.Globalization.CultureInfo cur=NewSystem.Globalization.CultureInfo ("en -US"); stringSdate = d.tostring ("YYYYMMDDHHMMSS", cur); //Receive Email from POP3 serverMail Omail =Oclient.getmail (info); ZWL. Bll. Erpnetemail MyModel=NewZWL. Bll. Erpnetemail (); Mymodel.emailcontent=""; Mymodel.emailstate="not read"; Mymodel.emailcontent=Omail.htmlbody; Mymodel.emailtitle=Omail.subject; Mymodel.fromuser=oMail.From.ToString (); foreach(Eagetmail.attachment attrachmentinchomail.attachments) {stringFilefoot = attrachment. Name.substring (attrachment. Name.lastindexof (".") +1, (attrachment. Name.length-attrachment. Name.lastindexof (".") -1));//Extended Name Switch(filefoot) { Case "Doc": Mymodel.fujian = sdate +". doc"; Break; Case "docx": Mymodel.fujian = sdate +". docx"; Break; Case "xls": Mymodel.fujian = sdate +". xls"; Break; Case "xlsx": Mymodel.fujian = sdate +". xlsx"; Break; Case "ppt": Mymodel.fujian = sdate +". ppt"; Break; Case "pptx": Mymodel.fujian = sdate +". pptx"; Break; Case "rar": Mymodel.fujian = sdate +". rar"; Break; Case "Zip": Mymodel.fujian = sdate +". zip"; Break; Case "txt": Mymodel.fujian = sdate +". txt"; Break; Case "jpg": Mymodel.fujian = sdate +". jpg"; Break; Case "PNG": Mymodel.fujian = sdate +". PNG"; Break; } //if (attrachment. Name.contains (". Doc") | | Attrachment. Name.contains (". docx"))//{ //Mymodel.fujian = Sdate + ". Doc"; //} //if (attrachment. Name.contains (". xls") | | Attrachment. Name.contains (". xlsx"))//{ //Mymodel.fujian = sdate + ". xls"; //} //if (attrachment. Name.contains (". ppt") | | Attrachment. Name.contains (". pptx"))//{ //Mymodel.fujian = sdate + ". ppt"; //} //if (attrachment. Name.contains (". rar"))//{ //Mymodel.fujian = sdate + ". rar"; //} //if (attrachment. Name.contains (". zip"))//{ //Mymodel.fujian = sdate + ". zip"; //} //if (attrachment. Name.contains (". txt"))//{ //Mymodel.fujian = sdate + ". txt"; //} stringFileName1 = System.Web.HttpContext.Current.Request.MapPath (".. /uploadfile") +"\\"+Mymodel.fujian; Attrachment. SaveAs (fileName1,true); } Mymodel.touser= ZWL.Common.PublicMethod.GetSessionValue ("UserName"); Mymodel.timestr=omail.receiveddate; Mymodel.add (); Console.WriteLine ("From : {0}", oMail.From.ToString ()); Console.WriteLine ("Subject: {0}\r\n", Omail.subject); stringFileName = System.Web.HttpContext.Current.Request.MapPath (".. /uploadfile") +"\\"+Mymodel.fujian; //Save Email to Local disk//Omail.saveas (FileName, true); //Mark Email as deleted from POP3 server.Oclient.delete (info); } //Quit and pure emails marked as deleted from POP3 server.Oclient.quit (); } Catch(Exception EP) {Console.WriteLine (ep. Message); }
C # receive Mail