C # receive Mail

Source: Internet
Author: User
Tags ssl connection

 /// <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.