LAN Web TCP Delphi

Title: A step by step guide to TCP/IP programming - Part 3
Question: Major Internet protocols - Sending a mail
Answer:
================================
(c) Romeo Lefter, May 2000
You are not allowed distribute
this turorial without my permission!
================================
We will stop for a little time our discussion about sockets. In order to develop some cool tcp/ip programs, we need to know how major Internet protocols works. So, now we will lokk a little at mail protocol.
You know what mail is. I'm shure you have installed on your computer an e-mail client (Outlook, Netscape Messenger, Eudora, etc.). The process of e-mail communication is divided in 2 parts: Sending and Receiving.
Sending a mail
==============
The protocol behind send-mail is SMTP an acronym for Simple Mail Transfer Protocol. When you send an e-mail your client program try to connect to your SMTP server on port 25 (this is a standard port for SMTP) and send some commands to the server. The entire protocol is documented on RFC (there you can find documentations for all major Internet protocols). Maybe you don't know, but you can manualy send an email using telnet program. Just connect to your SMTP server on port 25 and send some commands. I'll not explain now in detail this commands, because we will concentrate now to delphi implementation.
Start with a new project. Drop on mainform a NMSMTP component (located on FastNet pallete). First we need to fill some properties with some values:
Host
-----------------
The host properties indicate the address of your smtp server, like mail.flashmail.com. Put here the address of your SMTP server(if you don't know look at settings of your e-mail program)
Port
-----------------
The value of this properties is default set to 25. That because this is the standard port for SMTP. BUT, your Internet Service Provider can use another port in order to implement this service. This is not a rule, but sometimes some providers wants to change this value. Is a good start to leave this value untouched. Is a better idea to let the user of your next cool email client to change this value.
SubType
-----------------
Here you can specify how your email will be send: as text(mtPlain), as HTML (mtHTML), as Rich Text (mtEnriched), as Standard Generalized Markup Language(mtSgml) etc.
UserID
-----------------
Some SMTP servers allows only valid users to send email using there services. In some cases this value must be specified.
---------------------------------------
And now, we will look inside PostMessage properties. All subproperties included here are the most important properties for our sendmail program. Lets start!
Attachments
-----------------
Many time you want to attach some images, programs, I LOVE YOU "virus" (!!) and some other with your e-mail messages. Here you can put the path to your file. See the bellow code snippet:
if OpenDialog1.Execute then
NMSMTP1.Postmessage.Attachments.Add(OpenDialog1.FileName);
Body
-----------------
Here you put the message, the body of your e-mail.
Example:
NMSMTP1.Postmessage.Body.Add('Hi, This is my first message!');
Date
-----------------
Used to set the date of the message. This is a string property and you can set the date as you wish. Like: 'January, 23, 2000', '23.01.2000', 'I don't know the date'. You can leave it blank.
FromAddress
-----------------
Your email address like:'john@hotmail.com'.
FromName
-----------------
Your Name like: 'John Smith'
LocalProgram
-----------------
Your program identification. Is a string property. You can set it like:
NMSMTP1.Postmessage.LocalProgram:='My very cool email client!!';
ReplayTo
-----------------
This is very important. Is the address where the receiver of your email will replay. When he press replay button (from his email program) this address will appear in "To:" field.
Subject
-----------------
Is the subject of your message. Like:
NMSMTP1.PostMessage.Subject:='The first Message';
ToAddress, ToBlindCarbonCopy, ToCarbonCopy
-----------------
All of there are TStringList. ToAddress specify the addresses where the message will go, CarbonCopy is the additional receiver(there will be visible in the header of message), BlindCarbonCopy is like CarbonCopy with the one difference: the emails added here are not visible in the message header (good for implement BulkMail email system).
And Now, with this properties filled, our email program is ready to run. The only code you must add is.
NMSMTP1.SendMail;
Great! Now lets look to NMSMTP events:
OnConnect is called when your program try to connect to server
OnConnectionFailed is called when Connection to server failed
OnConnectionRequired is called when you call a method that require to establish first a connection
OnFailure is called when Failed to send mail
OnPacketSent is called when send data. Very usefull to monitorize the sending process
Example:
procedure TForm1.NMSMTP1PacketSent(Sender: TObject);
begin
StatusBar1.SimpleText := IntToStr(NMSMTP1.BytesSent)+' bytes of '+IntToStr(NMSMTP1.BytesTotal)+' sent';
end;
OnSuccess is called when your email was sent with success.
In the next part we will look inside of process of sending emails.