Delphi uses Indy's tidpop3 to receive emails

Source: Internet
Author: User
Recently tested the tidpop3 mail receiving function in the Indy control package of Delphi. Because some email servers must support the encryption method, this test uses the encryption method (SSL) directly ), if you do not need the encryption method, you only need to change the service port and disconnect the iohandler of the POP3 control.
 
In use, you should first connect to the server, then read or delete the server, and then disconnect the server.
The program code is as follows:
 
{The New POP3 mail receiving function package uses the POP3 protocol for the unencrypted (SLL) Port: 110. If the SSL encryption protocol is used, the port number: 995 function package provides a connection to read, delete, disconnect the four functions before use, you must first connect, after the connection can be read, delete, and other operations, and finally need to disconnect directly use the encrypted protocol SSL .} unit upop3_email; interfaceuses idpop3, idmessage, idbasecomponent, sysutils, idattachment, classes, idsslopenssl, idtext, windows, role; // connection POP3 server function // entry parameter: // host: POP3 server address, encrypted by SLL. The default port is 995 // account: email account (email address)
// Psw: Account Password
// Exit parameter: returns true if the connection is successful. If the connection fails, returns falsefunction connectpop3server (host, account, psw: string; var errmsg: string): Boolean; // disconnect the POP3 mail server procedure disconnectpop3server; // obtain the number of mails and return the current number of mails. Function mail_getcount: integer; // Delete the email // entry parameter: // index: email index No. // exit parameter: true for success; falsefunction mail_delete (Index: integer): Boolean; {function entry parameter for obtaining email title information: Index: Corresponding email index No, starting from 1, emailtime: mail sending time emailsubject: Mail title emailbody: the formal content of the mail emailsendaddress: the sender address errmsg: returned error message returned parameter: true: returned success, false: return failure} function mail_getemailinfo (Index: integer; var emailtime: tdatetime; var emailsubject, emailbody, emailsendaddress, emailattachfilename, errmsg: string): Boolean; var POP3: tidpop3; opensslhandler: response; idmessage: tidmessage; connectok: Boolean; implementationfunction connectpop3server (host, account, psw: string; var errmsg: string): Boolean; begin if pop3.connected then pop3.disconnect; pop3.host: = Host; pop3.username: = Account; pop3.password: = psw; try pop3.connect; If pop3.connected then result: = true; failed t on E: exception do begin result: = false; errmsg: = E. message; end; connectok: = result; end; // disconnect the POP3 mail server procedure disconnectpop3server; begin pop3.disconnect; connectok: = false; end; // obtain the number of mails, function mail_getcounta: integer; begin result: = 0; if not connectok then exit; try result: = pop3.checkmessages; // The number of retrieved mails cannot be on E: exception do result: =-10; // error end; // Delete email // entry parameter: // index: Mail index number // exit parameter: Success true, failed falsefunction mail_delete (Index: integer): Boolean; begin result: = false; if not connectok then exit; try pop3.delete (INDEX); Result: = true; failed t on E: exception do end; {function for obtaining mail title information entry parameter: Index: corresponding mail index number, starting from 1 emailtime: mail sending time emailsubject: Mail title emailbody: official content of the email: emailsendaddress: sender address: emailattachfilename: name of the attachment file. The attachment is saved directly in the tmp directory of the current directory. errmsg: returned error message. Return parameter: true: returned success, false: return failure} function mail_getemailinfo (Index: integer; var emailtime: tdatetime; var emailsubject, emailbody, emailsendaddress, emailattachfilename, errmsg: string): Boolean; var count, I: integer; filename: string; begin result: = false; if not connectok then exit; try idmessage. clear; // read content emailbody: = ''; If pop3.retrieve (index, idmessage) then begin emailsubject: = idmessage. subject; // The title emailsendaddress: = idmessage. sender. text; // The email Sending address emailtime: = idmessage. date; // email sending time emailsendaddress: = idmessage. from. address; // obtain the number of attachments in the current email count: = idmessage. messageparts. count; if Count = 0 then // There is no attachment begin emailbody: = idmessage. body. text end else // The attachment begin for I: = 0 to count-1 do begin if (idmessage. messageparts. items [I] Is tidtext) then begin emailbody: = tidtext (idmessage. messageparts. items [1]). body. text; end; If (idmessage. messageparts. items [I] Is tidattachment) then begin emailattachfilename: = tidattachment (idmessage. messageparts. items [I]). filename; // name of the attachment file filename: = extractfilepath (paramstr (0); forcedirectories (filename + '\ TMP'); filename: = filename + '\ TMP \' + emailattachfilename; tidattachment (idmessage. messageparts. items [I]). savetofile (filename); // Save the attachment continue; end; Result: = true; end; begin t on E: exception do end; Initialization POP3: = tidpop3.create (NiL); opensslhandler: = tidssliohandlersocketopenssl. create (NiL); opensslhandler. ssloptions. method: = sslvtlsv1; // sslvsslv23; // sslvsslv3; pop3.iohandler: = opensslhandler; pop3.port: = 995; // The encrypted port number pop3.usetls: = timeout; pop3.readtimeout: = 1000*60*5; // pop3.connecttimeout: = 1000*60*5; // The connection times out for 5 minutes. idmessage: = tidmessage. create (NiL); connectok: = false; finalization idmessage. free; pop3.free; opensslhandler. free; end.
 
 
 

Due to the use of live writer's [Code insertion] plug-in, the coloring of Delphi code comments is not very thorough, so you have manually modified it, resulting in inconsistent colors.

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.