Introduction and example of Delphi email sending and receiving programming

Source: Internet
Author: User
Tags mailmessage
In Delphi, The tnmpop control ------- receives emails, POP3
POP3 protocol commands

Command description
The user transmits the user name to the POP3 server.
Pass the user password to the POP3 server
Stat obtains the number of emails of the current user.
List to obtain the mail number and size
Betr obtains all the mail information, including the mail header and content.
Top
Dele deletes a specified email.
Noop null operation
Quit host Dialog

Control Properties
1. attachfilepath
Specifies the path for storing attachments. If it is null, it indicates the current path.
2. bytesrecvd
Number of bytes received
3. bytestotal
Total number of bytes in this transmission
4. Connected
Returns true if a connection is established with the POP3 server; otherwise, false.
Example:
If not nmpop31.connect then
Begin
Nmpop31.port: = 110;
Nmpop31.userid: = 'username ';
Nmpop31.password: = 'userpassword ';
Nmpop31.connect;
End;
5. deleteonread
If the attribute is true, the email is deleted from the server after being downloaded.
6. Host
Server Host Name or IP address
7. localip
Return the customer's IP address. If there are multiple IP addresses, only the first IP address is returned.
8. mailcount
Number of returned mails
9. mailmessage
After the getmailmessage method is called, this attribute returns the retrieved mail information, which is a class
Includes the following parts:
L attachments: This is a tstringlist object: contains the name of the attachment to receive the email
L body: This is a tstringlist object: contains the body of the received email.
L from: the sender's e_mail address
L head: This is a tstringlist object that contains the mail header title.
L messageid: ID of the email
L subject: Subject of the email
10. Password, port, userid
User Password, protocol port, and user identification name submitted to POP3 server
11. remoteip
IP address of the POP3 server
12. reportlevel
Indicates the call status with POP3 server?
13. Status
Returns the current status information.
14. Summary
Is a class that contains the mail statistics. It includes the following parts:
L bytes message bytes;
L from sender's email address
L messageid: ID of the email
L subject: the subject of the email.
15. Timeout
The Unit is milliseconds. When the socket does not respond within the specified time, an exception is triggered.
16. transactionreply
Returns the execution result of the last command.
**************************************** **************************
Control Method:
1. Abort
Stop this reception and disconnect from the POP3 server.
2. Connect
Connection to the POP3 server, but the host, port, userid, and password attributes are set in advance.
Example:
Nmpop31.host: = "mail.wofoo.com ";
Nmpop31.port: = 110;
Nmpop31.reportlevel: = 1;
Nmpop31.timeout: = 1000;
Nmpop31.connect;
3. deletemessage (mailnumber: integer)
Delete an email whose email number is mailnumber from the POP3 server. Note: The Reset is not actually deleted. If the reset is called before the edge is disconnected, it will be restored.
4. Disconnect
Disconnect from the POP3 server.
5. getmailmessage (mailnumber: integer)
Download an email whose email number is mailnumber from the POP3 server
6. getsummary (mmailnumber: integer)
Obtain an email message whose email number is mailnumber.
7. List
Retrieve a list from the email server. This list contains the number and number of cells of each email. Each item in the list triggers an onlist event.
8. Reset
Restores all emails marked as deleted on the server.
**************************************** **********************
Control event:
1. onauthenticationfailed (VAR handled: Boolean)
This event is triggered when the user name and password cannot pass when logging on to the server.
After correction, if the handled parameter is set to true, the system will log on again.
2. onauthenticationneeded (VAR handled: Boolean)
This event is triggered if an operation needs to authenticate the user.
Example:
Procedure tform1.nmpop31 authenticationneeded (VAR handled: Boolean );
VaR newpass, newid: string;
Begin
Newpass: = nmpop31. Password;
Newid: = nmpop31.userid;
Inputquery ('authentication need', 'input userid', newid );
Inputquery ('authentication need', 'input password', newpass );
Nmpop31.userid;: = newid;
Nmpop31.password: = newpass;
Handled: = true;
End;
3. onconnect
This event is triggered when the connection to the server is successful.
4. onconnectfailed
This event is triggered when the connection to the server fails.
5. onconnectrequired (VAR handled: Boolean)
Most pop controls are interesting only when they are successfully connected to the server. This event is triggered if the method is not successfully connected to the server. In this case, call the connect method again and set the handled parameter to true;
6. ondisconnect
This event is triggered when the connection is disconnected from the server. If you want to access the VCL control, you 'd better first determine whether the control exists.
7. onfailure
This event is triggered when an error occurs when you delete the email.
8. oninvalidhost
This event is triggered when a host is connected and the specified host name does not exist.
9. onlist
When the program calls the list process, this event is triggered every time one item is retrieved. Msg parameter is mail
Part number. The size parameter is the number of bytes of the email.
10. onpacketrecvd
This event is triggered every time a data block is received. The bytestotal and bytesrecvd attributes can be used to display the mail receipt progress.
11. onreset
This event is triggered when the program calls the reset process to restore emails with deleted tags.
12. onretrievestar
This event is triggered when the program calls the getmailmessage or getsummary process to start data retrieval.
13. onretrieveend
This event is triggered when the program calls the getmailmessage process or the getsummary process to successfully retrieve data.

Instance 1:
The procedure is as follows:
Procedure tfrmmail. formcreate (Sender: tobject );
Begin
// Server information
Edtip. Text: = 'mail .wofoo.com'
Edtport. Text: = '123 ';
// User information
Edtuser. Text: = 'mis ';
Edtpass. passwordchar: = '*';
Edtpass. Text: = '20140901 ';
// Others
Edtmail. Text: = '';
Edtsubject. Text: = '';
Memo1.lines. Clear;
// POP3 control parameter settings
Nmpop31.deleteonread: = false;
Nmpop31.timeout: = 5000;
Nmpop31.deleteonread: = false;
Nmpop31.reportlevel: = status_basic;
// Display other options
Memo1.scrollbars: = ssboth;
End;
First, you must log on to the server and implement it in The onclick event of btnconnect. The Code is as follows:

Procedure tfrmmail. btnconnect click (Sender: tobject );
Begin
Nmpop31.host: = edtip. text;
Nmpop31.port: = strtoint (edtport. Text );
Nmpop31.userid: = edtuser. text;
Nmpop31.password: = edtpass. text;
Nmpop31.connect;
End;
Login successful handling:
Procedure tfrmmail. nmpop31 connect (Sender: tobject );
Begin
Showmessage ('Connection successful ');
End;
Failed to log on:
Procedure tfrmmail. nmpop31 connect (Sender: tobject );
Begin
Showmessage ('Connection failed ');
End;
After successful login, you can get the mail information. Click the get email button to process the email.
Procedure tfrmmail. btngetmailclick (Sender: tobject );
Begin
Nmpop31.list;
End;
When the list method is used, the onlist event is triggered when information is retrieved. The parameter of this event contains the mail size and other information. Add the information to the listview control.
Procedure tfrmmail. nmpop31list (MSG, size: integer );
VaR
Listitem1: tlistitem;
Begin
Listitem1: = listview1.items. Add;
Listitem1.caption: = jsonstr (MSG );
Listitem1.subitems. Add (batch STR (size ));
End;
In actual use, it is possible that the user can obtain the email information before logging on to the server.
Nmpop3 triggers the onconnectionrequired event. You can request a new connection here.
Server.
Procedure tfrmmail. nmpop31connectionrequired (VAR handled: Boolean );
Begin
Handled: = true;
Nmpop31.connect;
End;
You can select the desired email in the listview control.
Procedure tfrmmail. listview1click (VAR handled: Boolean );
VaR index, I: integer;
Begin
If listview1.selected: = nil then
Exit;
// Obtain the selected ID
Index: = strtoint (listview1.selected. capton );
// Obtain the email
Nmpop31.getmessage (INDEX );
// Obtain email information
Edtmail. Text: = nmpop31.mailmessage. From;
Edtsubject. Text: = nmpop31.mailmessage. subject;
Memo1.lines. Assign (nmpop31.mailmessage. Body );
End;
Other information display
Procedure tfrmmail. nmpop31success (Sender: tobject );
Begin
Statusbar1.simpletext: = 'Operation successful ';
End;
Procedure tfrmmail. nmpop31failure (Sender: tobject );
Begin
Statusbar1.simpletext: = 'Operation failed ';
End;
Procedure tfrmmail. nmpop31connectionfailed (Sender: tobject );
Begin
Showmessage ('Connection failed ');
End;
Procedure tfrmmail. nmpop31invalidhoat (VAR handled: Boolean );
Begin
Showmessage ('invalid host ');
Handled: = true;
End;
Finally exit the email server
Procedure tfrmmail. btndisconnectonclick (Sender: tobject );
Begin
Nmpop31.disconnect;
End;
Procedure tfrmmail. nmpop31disconnect (Sender: tobject );
Begin
Statusbar1.simpletext: = 'disconnected ';
End;

Send mail nmsmtp

SMTP commands

Command description
Helo client uses helo commands to identify the server
Mail initialization Email transmission
After the MAIL command, Rcpt identifies a single email recipient
No Noop operation, server response OK
After data RCPT (usually), it indicates that all Email recipients are identified and data transmission is initialized.
Help client can use this command to query what commands are supported by the server
Quit session ends

Nmsmtp
Attribute
1. clearparams attributes
If it is true, the value of the postmessage attribute is cleared after the mail is sent.
2. encodetype
Specify the email encoding method. Uumime-mime encoding; uncode --- uencoding Encoding
3. finalheader
Specify the final message header.
4. Host
The host name or IP address of the SMTP server.
5. localip
Returns the IP address of the client. If multiple IP addresses exist, only one IP address is returned.
6. Port
Port of the specified server
7. postmessage
Is a class that stores the mail information to be sent. The details are as follows:
L fromname: sender name
L fromadress: the sender's email address.
L replaytoadress: reply address
L orgranization: address of the sender.
L localprogram: client program used by the sender
L toadress: The Receiving address of the recipient of the tstringlist object.
L tocc: tstringlist object, CC email address.
L tobcc: The tstringlist object, which is the BCC email address.
L attachments: tstringlist object, the specified attachment.
L body: tstringlist object, mail body.
L subject: the subject of the email.
L date: the mail sending date. If it is null, it indicates the current date.
9. replaynumber
Read-only attribute. The SMTP server returns the Response Message.
10. Status
Current status information.
11. Timeout
The Unit in milliseconds. If the specified time socket does not respond, an exception is triggered.
12. transactionreply
Returns the execution result of the last command.
13. userid
The specified user name.

Control Method

1. Abort
Stop sending and disconnect from the SMTP server
2. clearparameters
Clear postmessage.
3. Connect
Log on to the SMTP server and set the host and port attributes first. The onvalidhost event is illegally triggered by the host, and the onconnectfalied event is triggered by connection failure. If the connection is successful, the onconnect event is triggered.
4. Disconnect
Disconnect from the server. Trigger the ondisconnect event.
5. expandlist (maillist: string): Boolean;
This process retrieves a mail list on the SMTP server. If an onmaillistreturn event is triggered, you can obtain this mail list.
6. Sendmail
Send the email. Set the host, port, and postmessage in advance.
7. Verify (username: string): Boolean;
Check whether the user name is valid. The valid value is true;

Event:
1. onattachmentnotfound
If the email specified by postmessage is not found, this event is triggered and a parameter is sent to the attachment name.
2. onauthenticationfailed
This event is triggered when SMTP requires the user name and password to be sent, but the user name and password are not given in advance or the error is given.
3. onconnect
This event is triggered after the connection to the server is successful.
4. onconnectfailed
An error occurred while connecting to the server.
5. ondisconnect
Disconnect from the server to trigger this event.
6. onenacodestart
This event is triggered when the attachment starts encoding.
7. onenacodeend
This event is triggered when the attachment encoding ends. The filename parameter is an encoded attachment.
8. onfailure
This event is triggered when the email fails to be sent.
9. onheaderincomplete
When an email is sent, It is triggered when the header information provided by postmessage is incomplete.
10. onhostresolved and onivalidhost
This event is triggered when the host name of the server is found. Otherwise, onivalidhost is triggered.
11. onmaillistreturn
When the program calls expandlist, this event is triggered once every time an email address is retrieved.
12. onrecipientnotfound
This event is triggered if the recipient specified by postmessage is no longer valid.
13. onsendstart
This event is triggered when the email is sent.
14. onsuccess
This event is triggered when the email is sent successfully.

Instance:
Procedure tfrmsendmail. btnsendclick (Sender: tobject );
Begin
If edtadress. Text = ''then
Exit;
If edtport. Text = ''then
Exit;
If edtsendusertext = ''then
Exit;
If edtreceiver. Text = ''then
Exit;
Nmsmtp1.host: = edtadress. text;
Nmsmtp1.port: = strtoint (edport. Text );
Nmsmtp1.userid: = edtsenduser. text;
Nmsmtp1.conncet;
End;
Procedure tfrmsendmail. nmsmtp1 connectionfailed (Sender: tobject );
Begin
Showmessage ('Connection failed ');
End;
Procedure tfrms endmail. nmsmtp1 connection (Sender: tobject );
Begin
Sendmessage (handle, wm_login, 0, 0); // custom message transmission. Wm_login event generation
End;
Procedure tfrmsendmail. wmlogin (var msg: TWM); // process custom messages
Begin
// Send an email here.
Nmsmtp1.postmessage. fromadress: = edtusermail. text;
Nmsmtp1.postmessage. fromname: = edtsenderuser. text;
Nmsmtp1.postmessage. Subject: = edtsubject. text;
Nmsmtp1.postmessage. toadress. Add (edtreceiver. Text );
Nmsmtp1.postmessage. attachments. addstrings (listbox1.items );
Nmsmtp1.postmessage. Body. Assign (memo1.lines );
Nmsmtp1.sendmail;
Nmsmtp1.disconncet;
End;
 

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.