Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - Creating an Email Client with Borland Delp...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Creating an Email Client with Borland Delphi
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 35
    2005-10-18

    Table of Contents:
  • Creating an Email Client with Borland Delphi
  • What you need
  • Coding
  • Improvements/Remarks

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Delphi-Kylix Articles
    More By Jacques Noah


       · Shh....Delphi makes it easy to create a mail client!! I want to learn how to use...
       · If i do create an MSAccess database to store email addresses in a Text field of the...
       · To use MS Access with Delphi, go to...
       · It's worth noting that Borland includes very capable POP3 and SMTP components....
       · Actually Delphi does not have a naive SMTP/POP3 server or client, it has been...
       · But someone could go out and spend 50 or 60 bucks on the Indy In Depth book, then...
       · I agree, upon discovering Indy components, the first thing i did was to buy a Indy...
     

    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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek