Home arrow Delphi-Kylix arrow Page 3 - Creating an Email Client with Borland Delphi
DELPHI-KYLIX

Creating an Email Client with Borland Delphi


If you have ever wanted to send email messages from your Delphi application, this article is for you. Jacques Noah explains how to use the open source Indy suite to set up an full fledged Mail Client.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 40
October 18, 2005
TABLE OF CONTENTS:
  1. · Creating an Email Client with Borland Delphi
  2. · What you need
  3. · Coding
  4. · Improvements/Remarks

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating an Email Client with Borland Delphi - Coding
(Page 3 of 4 )

Now that we've finished building the GUI we can start coding. Add "idreplysmtp" to your uses list, then double click on btMsg and add the following code:

procedure Tform1.btMsgClick(Sender: TObject);
begin

  //setup idSMTP connection parameters
  idSMTP.Host :=your host name;
  idSMTP.Port := 25; //smtp service usually runs on this port
  idSMTP.Password:=your password;

  //setup idmessage parameters
  idmessage.From.address:=edFrom.Text;
  idmessage.Recipients.EMailAddresses:=edTo.Text;
  idmessage.CCList.EMailAddresses:=edCC.Text;
  idmessage.BccList.EMailAddresses:=edBC.Text;
  idmessage.Subject :=edSubject.Text;
  idmessage.Body.Text := memo1.Lines.Text;

  //check if receipt confirmation is required
  if checkbox1.checked then

    //if required, set the sendback email address to your email
address
    idmessage.ReceiptRecipient.Text:=edfrom.Text;

    //send the message
    try
  try
      idSMTP.Connect;
      idSMTP.send(idmessage);

//if an exception occurs…
      except on E: EIdSMTPReplyError do
    begin

       //…then show the message
       ShowMessage(E.Message);
    end;
    end;
  finally

    //disconnect from server
    if IdSMTP.Connected then
      IdSMTP.Disconnect;
  end;
end;

Let's go through this bit of code:

The first thing I've done is set up the server connection by providing the password and host name. The hostname is usually written this way: mail.xxx.com or smtp.xxxx.com.

Then I've continued to fill the idmessage component with the email headers, which in this case are: from, to, CC, BCC, subject and body. Please note that I also put a condition in place to check whether the user requires a delivery confirmation. This will enable you to check whether the message has actually been delivered to the recipient. I connected to the server and sent the message and then I closed the connection to the server. It is very important that you close the server connection, as you will get an error if you want to send another message. It is also good memory management. The "idreplysmtp" unit contains the definitions for all possible errors or exceptions that can be raised throughout this operation.

Now double click the browse button and add the following code:

procedure Tform1.btBrowseClick(Sender: TObject);
begin
  if opendialog.Execute then
    TIdAttachmentFile.Create(idmessage.MessageParts,
opendialog.FileName);
    AddAttachments;
end;

The browse button calls up the opendialog and allows you to select a file to attach to your message. Then it goes on to call the AddAttachments procedure to add the filename(s) to the listview.

Add this procedure somewhere in the implementation section:

procedure Tform1.AddAttachments;
var li: TListItem;
   idx: Integer;
begin
//clear the attachment listview
   lvAttachments.Items.Clear;
//loop through Idmessage and count parts
   for idx := 0 to Pred(Idmessage.MessageParts.Count) do
      begin
         li := lvAttachments.Items.Add;
// Check if Idmessage contains any attachments… 
         if Idmessage.MessageParts.Items[idx] is TIdAttachmentFile then
            begin
//if so, get the file names…
               li.Caption := TIdAttachmentFile
(Idmessage.MessageParts.Items[idx]).Filename;
//and add them to the listview
               li.SubItems.Add(TIdAttachmentFile
(Idmessage.MessageParts.Items[idx]).ContentType);
            end
         else
            begin
               li.Caption := Idmessage.MessageParts.Items[idx].ContentType;
            end;
      end;
end;

In short, the above procedure adds files to the listview component by looping through the idMessage component, checking whether it contains any attachments. If it does contain attachments, then it gets the filenames and lists them in the listview. This way you can see what files you've attached to your message. Make sure to add "procedure AddAttachments" in the public section of your form.

That's it! You have a fully functioning email application. All of our requirements have been met.


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials