Use Delphi to design email addresses that can carry attachments

Source: Internet
Author: User
Tags mailmessage
Source: Jinqiao Forum
Users who never use client software such as Foxmail or outlook, carefully open IE, open favorites, click the address of a website, and enter the user name and password, add attachments, wait for ING ..., Failed to send, go back to the front page, send again, wait for ING ..., Until the message is sent successfully. Every time they experience these actions, they will not be able to suffer. How can we reduce their burden? Add the email sending function to your program, and do not forget to add the attachment function (the attachment function is too useful in email ), this allows your users to do everything with just one click, making it easier for them.

Will it be difficult to implement the functions described above? This may have been the case before, but now we have the Delphi and Indy components, and the development process is like sitting in front of the window and enjoying the view outside the window, while tasting the coffee in a cup, it is generally pleasant and natural. Now let's double-click Delphi to start a pleasant journey.

Put the idsmtp, idmessage, and opendialog components on the form of Delphi. These three components are the core components of the mail sending program. Most of the functions are implemented by them, there are some other labels, labelededit, and memo components. The specific layout is shown in:


(Figure 1)
 
The implementation idea of this program is as follows. The mail sending function is implemented by the Indy component. (Note: This is a very famous component used to develop network programs, starting with DELPHI6, this is a built-in component of Delphi. The latest version can be downloaded at http://www.nevrona.com/indy/. the idsmtp component is used to connect to and communicate with the mail sending server (SMTP server, the idmessage component is used to store and encode the mail content. After the email is written, the data is encoded and "filled" into the idmessage, and then connected and communicated with the SMTP server through idsmtp.

The main code for sending an email is as follows:

Procedure tmailerform. btnsendmailclick (Sender: tobject );
Begin
Statusmemo. Clear;

// Set SMTP
SMTP. HOST: = ledhost. Text; // specific SMTP used, you can go to the website where your application mailbox is located to find
SMTP. Port: = 25;

// Set the email content
Mailmessage. From. Address: = ledfrom. text;
Mailmessage. Recipients. emailaddresses: = ledto. Text + ',' + ledcc. text;

Mailmessage. Subject: = ledsubject. text;
Mailmessage. Body. Text: = body. text;

If fileexists (ledattachment. Text) then
Tidattachment. Create (mailmessage. messageparts,
Ledattachment. Text );

// Send an email
Try
Try
SMTP. Connect (1000 );
SMTP. Send (mailmessage );
Except t on E: exception do
Statusmemo. lines. insert (0, 'error: '+ E. Message );
End;
Finally
If SMTP. connected then
SMTP. Disconnect;
End;

End;

// Add the attachment code
Procedure tmailerform. btnattachmentclick (Sender: tobject );
Begin
If attachmentdialog. Execute then
Ledattachment. Text: = attachmentdialog. filename;
End;

The mail sending program is widely used. With the popularization of broadband, "always online" is possible. Adding the mail sending and receiving function to your application software will bring a lot of convenience to your users. If you think more about your software in software development, you can make your software popular!

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.