. Net uses the pop3 component to receive emails (including attachment downloads)

Source: Internet
Author: User
Many users use pop3 and jmail components to receive emails and attachments on the Internet. however, the Code is a bit dizzy, and a single run is not functional. here I also found a demo for writing materials and tested it. There is a problem with wood. you can use it directly.

Read and unread emails will be filtered in the future. (I plan to use the mail component or smtp provided by vs to write a demo.) I hope you can communicate with each other. optimize the Code. This demo is just a prototype.

First, reference the LumiSoft. Net. dll file in the project.

Protected void Page_Load (object sender, EventArgs e)

{List <Mime> dd = GetEmails (); // you can find the content, subject, sender, and other information of the email. You can view foreach (Mime mdd in dd) through quick monitoring of debugging status {// The Mail content output on the page ShowEmail (mdd );}} // obtain the mail List public List <Mime> GetEmails (){
// You must first set the information string pop3Server = "pop.sina.com.cn"; // The email server, for example, "pop.sina.com.cn"; or "pop.qq.com", as if sina is faster than int pop3Port = 110; // use "110" to enable the port number. It is best to check the port number bool pop3UseSsl = false for your mailbox server; string username = ""; // your mailbox username string password = ""; // your email password List <string> gotEmailIds = new List <string> (); List <Mime> result = new List <Mime> (); using (POP3_Client pop3 = new POP3_Client () {// establish a connection with the Pop3 server pop3.Connect (pop3Server, pop3Port, pop3UseSsl); // verify the identity pop3.Authenticate (username, password, false ); // obtain the mail Information List POP3_ClientMessageCollection infos = pop3.Messages; foreach (POP3_ClientMessage info in infos) {// each Email has a unique Id within the Pop3 server range, check whether this Id exists to know whether this email has been received before if (gotEmailIds. contains (info. UID) continue; // get the content of this email byte [] bytes = info. messageToByte (); // record the Id of this email gotEmailIds. add (info. UID); // parse the message Mime = mime sent from the Pop3 server. parse (bytes); result. add (mime) ;}} return result ;}

// Displays the email content.

Public void ShowEmail (Mime m) {// obtain the attachment foreach (MimeEntity entry in m. attachments) {string fileName = entry. contentDisposition_FileName; // obtain the file name string path = Server. mapPath ("~ \ Attch \ "+ fileName); if (File. exists (path) {Random random = new Random (); int newfile = random. next (1, 100000); path = Server. mapPath ("~ \ Attch \ "+ newfile. toString (); Directory. createDirectory (path); path + = "\" + fileName;} byte [] data = entry. data; FileStream pFileStream = null; pFileStream = new FileStream (path, FileMode. create); pFileStream. write (data, 0, data. length); pFileStream. close ();} Response. write ("From:" + m. mainEntity. from. toAddressListString (); Response. write ("To:" + m. mainEntity. to. toAddressListString (); Response. write ("Time:" + m. mainEntity. date); // send time Response. write ("Subject:" + m. mainEntity. subject); // Subject Response. write ("Plain Body:" + m. bodyText); // content Response. write ("Html Body:" + m. bodyHtml); // HTML format content}

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.