Example of Using Indy to create email sending and receiving

Source: Internet
Author: User
Tags control label

1. Create a project and add the idsmtp1, idmessage1, and idpop31 (Indy control label group) controls to the form.

2. Set idsmtp1.host: = 'smtp .163.com '. (I used the 163 check. You can also select another server.) idsmtp1.username: =

'Caoyan528'; (this indicates the account name of the mailbox, for example, the mailbox is: aaaaaa@163.com, then username is aaaaaa) idsmtp1.password: = 'xxxxxxxx'; (here

Is the password of the mailbox, such as the aaaaaa@163.com mailbox password is 123456, the password is 123456 ).

3. Set idmessage1.from. Address: =

'Caoyan _ 528@163.com '; (Note that the email address here should exist in smtp.163.com and it is the username set above), idmessage1.recipients. emailaddh

SSEs: = 'caoyan528 @ gmail.com '(this indicates the recipient address, as long as it is an existing Email Address) idmessage1.subject: = 'first email customer

The source code of the topic is as follows:

Procedure tform1.button4click (Sender: tobject );
VaR
Filename: string;

Tmpemailitem: tidemailaddressitem;
// Tmpemailadd: tidemailaddresslist;
Begin
Try
Idsmtp1.host: = 'smtp .163.com ';

Idsmtp1.authenticationtype: = atlogin; // ensure that the logon mode is login.
Idsmtp1.username: = 'caoyan _ 528 ';
Idsmtp1.password: = 'xxxxxx ';
Filename: = 'e:/email sender/2003-12-22_253_04.jpg '; // The attachment file to be added
Tidattachment. Create (idmessage1.messageparts, filename); // Add an attachment

Idmessage1.from. Address: = 'caoyan _ 528@163.com ';
Idmessage1.recipients. emailaddresses: = 'caoyan528 @ gmail.com ';

Tmpemailitem: = idmessage1.recipients. Add; // Add an address list

Tmpemailitem. Text: = 'caoyan528 @ gmail.com '; // write a recipient

Idmessage1.bcclist. Add. Text: = 'caoyan _ 528@yahoo.com.cn '; // write multiple target addresses to achieve Group

Idmessage1.bcclist. Add. Text: = 'caoyan _ 528@163.com '; // write multiple target addresses to achieve Group

Idmessage1.subject: = 'first Mail Client ';
Idmessage1.body. Text: = memo1.text; // the body of the email.

Idmessage1.charset: = 'gb2312'; // ensure the normal display of Chinese characters in the attachment body
Idmessage1.body. Assign (memo1.lines );
If idsmtp1.authschemessupported. indexof ('login')>-1 then
Begin
Idsmtp1.authenticationtype: = atlogin; // before connection, save it as set above login. This is not necessary.
Idsmtp1.authenticate;
End;

Idsmtp1.connect (); // connect to the SMTP server
Idsmtp1.authenticate;
Idsmtp1.send (idmessage1); // send an email to the server
Finally
Idsmtp1.disconnect; // disconnect from the server
End;
End;

Since then, the content written in memo1 has been sent as an email. The attachment referred to by filename is also sent to the server.

 

4. receive emails

The above flowchart shows that the POP3 server is used to receive mails. You only need to connect to the POP3 server to download the mails. The procedure is as follows:

Set idpop31.host: = pop.163.com idpop31.username: = 'caoyan _ 000000'; idpop31.password: = 'xxxxx ';

(The settings here are the same as those for SMTP) the source code is as follows:

Procedure tform1.button5click (Sender: tobject );
VaR
Mailcount: integer;
I: integer;
TMP: string;
Begin
Idpop31.connect (); // connect to the POP3 server
Mailcount: = idpop31.checkmessages; // obtain the number of email mails
For I: = 1 to mailcount do // traverse each email
Begin
Idmessage1.clear;
Idpop31.retrieveheader (I, idmessage1); // get the mail header information
TMP: = idmessage1.subject; // get the mail title
Memo1.lines. Add (TMP );
Idpop31.retrieve (I, idmessage1); // receives all emails
TMP: = idmessage1.body. Text; // mail body
Memo1.lines. Add (TMP );
End;
Idpop31.disconnect; // disconnect
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.