Mega Code Archive
A step by step guide to TCPIP programming Part 5
Title: A step by step guide to TCP/IP programming - Part 5
Question: Major Internet protocols - Receiving a mail
Answer:
================================
(c) Romeo Lefter, May 2000
You are not allowed distribute
this turorial without my permission!
================================
In order to receive emails messages you must know behind this "procedure" is a protocol. The name fo this protocol is POP3 and the port used by it is 110. Also, for receiving mail is another protocol (recent) named IMAP. In Delphi standard component you haven't an encapsulation of this protocol. So we will receive email now using POP3 (Post Office Protocol).
The messages sent oner the internet are stored on some servers. Lets undestand how this work. Open your email client program. In it, you have a special folder named Inbox. Here are stored all your received messages. Like in your inbox folder, the messages are stored on the POP3 server. Messages are stored on server until you download and delete then. But these actions are realised by your email program.
Lets start our program. Start a new project and drop a NMPOP3 component on main form. Now I will desribe any property that will need to set:
AttachFilePath
-------------------
Some messages has attachments. So, we need a directory where this attachments will be put after download. If you leave blank this property, the attachments will be downloaded in your default directory.
DeleteOnRead
-------------------
If you set this property to true, the message will be deleted from server after you download it. If it is false, the message will not be deleted.
Host
-------------------
Like in sendmail program, the address of your POP3 server.
Port
-------------------
Default 110. You need to change it only if your ISP changed the service port.
UserId, Password
-------------------
Any POP3 client need an UserName and Password in order to access there account.
With this properties filled the first step of our POP3 client is done.
Connecting to server
--------------------
For this we have the Connect method. Just attach the code bellow on OnClick event of a button:
NMSMTP1.Connect;
After that our program will try to connect to the server. If connection is OK, the NMPOP3 component will send to the server our userName and Password. If all is OK, we are connected to the server.
We know if the connection is succesfull using OnConnect event. This event is called when we succesfull connect to the server. If the connection failed, the OnConnectFailed event is called.
Listing Messages
----------------
We don't know what messages are on server, so we need to list then. For this we have the list method:
NMPOP31.list;
But, don't forget. We need first to be connected. If acidentaly you try ti use list method before you are connected an error will be generated and the OnConnectionRequired event is called.
For write the messages titles in a memo(for example) you need to place your code in the OnList event:
procedure TForm1.NMPOP31List(Msg, Size: Integer);
begin
Memo3.Lines.Add(IntToStr(Msg)+' / '+IntToStr(Size));
end;
Msg is the message number and Size is the message Size.
This is not best method. If your user will see somethink like this
1 220k
2 10k
3 17k
is really bad. He wants to know the message subject, the sender etc. For that we will use the GetSummary Method. With GetSummary(message_number) method you will know all the details. Look bellow:
GetSummary(1);
Edit6.Text := NMPOP31.Summary.From;
Edit7.Text := NMPOP31.Summary.Subject;
Edit8.Text := IntToStr(NMPOP31.Summary.Bytes);
Edit9.Text := NMPOP31.Summary.MessageID;
In this example we get informations about first message.
For getting the mail from server we use GetMailMessage(msg_nr). All the mail elements can be accessed using MailMessage:
NMPOP31.GetMailMessage(StrToInt(Edit5.Text));
Edit6.Text := NMPOP31.MailMessage.From;
Edit7.Text := NMPOP31.MailMessage.Subject;
Edit9.Text := NMPOP31.MailMessage.MessageID;
Memo2.Lines.Assign(NMPOP31.MailMessage.Head);
Memo1.Lines.Assign(NMPOP31.MailMessage.Body);
If NMPOP31.MailMessage.Attachments.Text '' then
ShowMessage('Attachments:'+#10#13+NMPOP31.MailMessage.Attachments.Text);
And that's all. In the next part, like in sendmail, we will look in detail at POP3 protocol.