Using Delphi to implement mail attachment and transceiver function

Source: Internet
Author: User

Haven't sent a blog for a long time, do not know why not this thought to blog on the turn. Today, I'll send you a test of the previous period.

E-mail is closely related to our work life, and many messages are delivered by mail. So e-mail is very important to us, although you can log in to the mailbox every time, but the message is always to log in through the site, but also a self-download, so that the operation in slow speed is more laborious. Now there are some very e-mail management tools, such as Foxmail, these tools are quite perfect, but the function of a lot of smallpox is the same, the use of it is not shuangxin, if we can build a suitable for their own needs of the mail management tool is a very good choice.

After testing, the two components of Delphi, TIdPOP3 and TIDSMTP, are fully capable of sending and receiving mail to most mail servers.

Introduction to TIDPOP3 Components

TIdPOP3 is a component that is used to receive mail messages from the mail server to the client. It implements the RFC 1939 protocol.

You need to set several of its member properties when using the TIdPOP3 component.

Host: Specifies the mail server, typically the address of the POP3 mail server, such as pop3.126.com.

Username: User name, which is the mailbox name, such as [email protected].

Password: Email password, the component needs to log in with a password when sending and receiving mail.

Other Member properties

Connected: Returns its connection status to the mail server, which is true to indicate that it is already connected.

Checkmessages: Number of messages, if the connection server succeeds, you can get the number of messages on the server side.

member functions

Procedure Connect (const Atimeout:integer = Idtimeoutdefault);

Connect to the server function. The number of milliseconds to wait when the parameter is an invalid connection.

function Retrieveheader (const Msgnum:integer; Amsg:tidmessage): Boolean;

Receives the message header information, it has two parameters, Msgnum indicates in receiving the first few messages, starting from 1, amsg is the mail message component instance.

function Retrieve (const Msgnum:integer; Amsg:tidmessage): Boolean;

Receives the message body information, which is the same as the Retrieveheader parameter. The received message content will be saved in amsg.

function Delete (const msgnum:integer): Boolean;

Delete the first few messages in the mail server. Starting from 1.

Procedure Disconnect; Override

Close the connection.

Introduction to Tidmessage Components

Tidmessage is used to support mail message protocols such as POP3,SMTP,NNTP. Tidmessage supports the Multipurpose Internet Mail Extension (MIME) protocol.

The properties of the commonly used Tidmessage:

Subject: Message subject, this string is BASE64 encoded. So it needs to be decoded when it is used.

Messageparts: This is an instance of the Tidmessageparts class that is used to store message information. such as email content and attachment information. When parsing, it is necessary to determine whether it is an attachment or text, and if it is an attachment, its file name is BASE64 encoded. The judging constants are Tidtext, Tidattachment, respectively.

Body: This is a list of strings that contains the body content that makes up the message.

Form: The address information of the person who sent the message.

Recipients: Recipient address information.

Bcclist: CC address list.

CharSet: Indicates the character set used in the message information.

ContentType: Specifies the MIME media data type, describes the data contained in the body, and enables the user agent to decide how to display the data, often with text/html,text/xml.

Introduction to TIDSMTP Components

TIDSMTP is a Simple Mail Transfer protocol and an SMTP client derived from Tidmessageclient.

Its main function is to send mail messages.

Common Properties:

HOST:SMTP the address of the mail server, such as smtp.126.com. It is not the same as the POP3 address above.

AuthenticationType: Server Authentication type, it has atnone,atlogin two kinds, namely do not need authentication and need to authenticate with username and password.

Username: Username, here is a bit different from TIdPOP3, that is, it does not need a suffix, such as billanking2002

Password: Email login password. If AuthenticationType set Atlongin, you must set the password and user name.

Let's look at the program source code below:

Create a new Delphi project and add tidpop3,tidmessage,tidsmtp three components to the window

Add two buttons, one for receiving mail, one for sending mail. Add three edit boxes, one for the server address, one for the user name, and one for the password. and a Memo,webbrowser control.

The following is the source file code for its project.

Determines whether the input string is an e-mail address.

function Tform1.isemail (email:string): Boolean;

Var

s:string;

Etpos:integer;

Begin

etpos:= pos (' @ ', EMail); Main judgment there is no @ symbol

If etpos> 1 Then

Begin

s:= Copy (Email,etpos+1,length (EMail));

if (POS ('. ', s) > 1) and (POS ('. ', s) < length (s)) then result:= true

else result:= false;

End

else result:= false;

End

BASE64 encoding conversion, this is very important, because the title of the message, and the nearby file name is encoded by the conversion.

function Tform1.base64decode (strinput:string): string;

Var

strdecode:string;

Posstart:integer;

Posend:integer;

Begin

While POS (' =?gb2312?b. ', lowercase (strinput)) >0 do

Begin

Try

Posstart:=pos (' =?gb2312?b ', lowercase (strinput));

Posend:=pos ('? = ', lowercase (strinput));

Strdecode:=strdecode+copy (strinput,1,posstart-1) +iddecodermime1.decodestring (copy (strinput,posstart+11, POSEND-POSSTART-11));

Strinput:=copy (Strinput,posend+2,length (strinput)-posend-1);

Finally

Application.processmessages;

End

End

Strdecode: = Strdecode + strinput;

Result: = Strdecode;

End

Receive mail function:

Procedure Tform1.bitbtn1click (Sender:tobject);

Var

I,j,mnnn,mailcount:integer;

tmp:string;

Begin

Idpop31.host: = E_address.text;

Dpop31.username: = E_username.text

Dpop31.password: = E_password.text;

While True does

Begin

Idpop31.connect (); Connecting to the POP3 server

if (idpop31.connected = true) Then

Begin

Break

End

ELSE begin

Idpop31.password: = ' gozhyflkmcfs ';

End

End

Mailcount: = idpop31.checkmessages; Get the number of messages in the mailbox

MNNN: = 0;

For i:=1 to Mailcount do/traverse each message

Begin

Idmessage1.clear;

Idpop31.retrieveheader (I,IDMESSAGE1); Get header information for a message

TMP: = Idmessage1.subject; Get the title of the message

MEMO1.LINES.ADD ('-----------------------------------------------------');

TMP: = Base64decode (TMP); Message header BASE64 decoding

Memo1. Lines.add (TMP);

Idpop31.retrieve (I,IDMESSAGE1); Receive mail all content

For J:=0 to Idmessage1. Messageparts.count-1 do

Begin

Try

if (Idmessage1.messageparts.items[j] is tidattachment) then

Matches whether the mail entry is an attachment

Begin Tmp:=base64decode (Tidattachment (Idmessage1.messageparts.tems[j]). FileName);

TMP: = ' \ n ' +tmp;

DeleteFile (TMP); Tidattachment (Idmessage1.messageparts.items[j]).    SaveToFile (TMP); Save attachments in the specified directory with the original file name

End

ELSE begin

if (idmessage1. MESSAGEPARTS.ITEMS[J] was Tidtext) then

Begin

If Message1.body.text<> ' Then

Sethtml (Webbrowser1,tidtext (idmessage1. MESSAGEPARTS.ITEMS[J]). Body.text)

Else Memo1. Lines.add (Tidtext (idmessage1. MESSAGEPARTS.ITEMS[J]). Body.text);

End

End

Except

Continue

End

End

If CheckBox1. Checked = True Then

Begin

Try

Idpop31.       Delete (i); Delete a received message

Except

Continue

End

End

End

Idpop31. Disconnectsocket;

Idpop31.disconnect;

End

Send mail function function:

Procedure Tform1.bitbtn2click (Sender:tobject);

Var

filename:string;

Begin

Idsmtp1.host: = E_address.text;

IDSMTP1.         AuthenticationType: = Atlogin; {Atnone,atlogin}

Idsmtp1.username: = E_username.text;

Idsmtp1.password: = E_password.text;

FileName: = ' d:\comunition.rar1 ';

Tidattachment.create (Idmessage1.messageparts,filename); Add to Attachment

FileName: = ' D:\comunition.rar ';

Tidattachment.create (Idmessage1.messageparts,filename); Add multiple Attachments

IdMessage1.From.Address: = '[email protected]'; Where does the message come from

idmessage1.recipients.emailaddresses:= ' [email protected]; Recipient

IdMessage1.BccList.Add.Text: = '[email protected]'; CC Address List

IdMessage1.BccList.Add.Text: = '[email protected]';

IdMessage1.BccList.Add.Text: = '[email protected]';

idmessage1.subject:= ' mail client ';

IdMessage1.Body.Text: = Memo1.text; Here is the message body

Idmessage1.charset: = ' gb2312 '; Ensure the normal display of the attachment body character

Idmessage1.   ContentType: = ' text/html '; Hypertext Mode Transfer

IdMessage1.Body.Assign (Memo1.lines);

If IdSMTP1.AuthSchemesSupported.IndexOf (' LOGIN ') >-1 Then

Begin

Idsmtp1.authenticationtype: = Atlogin;

To save as login before connection is set up, here is not necessary

Idsmtp1.authenticate;

End

Try

Idsmtp1.connect (); Connecting to an SMTP server

Idsmtp1.authenticate;

Idsmtp1.send (IDMESSAGE1); Sending mailboxes to the server

Finally

If idsmtp1.connected then Idsmtp1.disconnect; Disconnecting from the server

End

End

At this point, the mail messaging function is basically complete. To do a full-featured messaging management tool, you also need to join the Mail management feature. These functions are implemented primarily by manipulating files locally. This allows you to manage these messages in your own way.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/billanking/archive/2010/12/18/6083616.aspx

Using Delphi to implement mail attachment and transceiver function

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.