Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - 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 
Sun Developer Network 
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: conclusion
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2005-10-25

    Table of Contents:
  • Creating an Email Client with Borland Delphi: conclusion
  • What is POP3?
  • Code
  • Deleting a message

  • 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: conclusion - Deleting a message


    (Page 4 of 4 )

    To delete a message from the pop3 server you must first mark it for deletion and then disconnect. When you reconnect, that message will be gone. To mark a message for deletion you write: POP.Delete(messageID). So, when you press the delete button, the message will still be on your screen, but it will be marked for deletion on the server. The code that we are going to write is going to mark and delete the message in real time. So double click on the "delete message" button and enter the following code:

    procedure TForm1.Button2Click(Sender: TObject);
    begin
     if lv1.Selected <> nil then
    begin
     //confirm if the user wants to delete the message
    if MessageDlg('Are you sure you want to Delete the selected
    message?',
        mtConfirmation, [mbYes, mbNo], 0,) = mrYes then
     POP.Delete(lv1.Selected.Index + 1);

     pop.Disconnect;
    //call button1click to repopulate lv1
     Button1Click(Sender);
     end
      else
      //if the user pressed the button without selecting a message, inform them
       showmessage(' Please select messages to delete');

    end;

    Now, before we do anything else, add this just above the private declaration of form1:

    function FindAttachment(const stFilename: string): TidAttachmentfile

    and then add this anywhere in the implementation section:

    function TForm1.FindAttachment(const stFilename: string): TIdAttachmentfile;
        var
            I: Integer;
            Attachment: TIdAttachmentfile;
        begin
            for I := 0 to Pred(Mess.MessageParts.Count) do
            begin
                if (Mess.MessageParts.Items[I] is TIdAttachmentfile)
    then
                begin
                    Attachment := TIdAttachmentfile
    (Mess.MessageParts.Items[I]);
                    if TextIsSame(stFilename, Attachment.Filename)
    then
                    begin
                        Result := Attachment;
                        Exit;
                    end;
                end;
            end;
            Result := nil;
        end;

    What this code does is help search for attachments in messages.

    So far, we can download, read and delete messages, but we cannot as yet reply to these messages. This is where form2 and more importantly, form3 comes in. First, go to form1 and click on lv1, then go to the object inspector and click on the events tab, double click on the "OnDblclick" event and type the following:

     procedure TForm1.lv1DblClick(Sender: TObject);
    begin
    //clear form3's fields
    form3.Edit1.clear;
    form3.Edit2.clear;
    form3.Edit3.clear;
    form3.Memo1.Clear;
     if not POP.Connected then
          begin
             POP.connect;
          end;
     if lv1.Selected=nil then
    //fill fields with mess1 data i.e body,subject etc
    lv1.Selected.SubItems.Strings[0];
       POP.Retrieve(lv1.Selected.index ,Mess);
    form3.Edit1.Text:=label2.Caption;;
    form3.Edit2.Text:=label1.caption;
    form3.Edit3.text:=label3.Caption;
    form3.Memo1.Lines.Add(memo1.Lines.Text);
    form3.Label1.Caption:= FormatDateTime('dd mmm yyyy hh:mm:ss',
    Mess.Date);
    form3.Caption:=mess.Subject + ' ' + FormatDateTime('dd mmm yyyy
    hh:mm:ss', Mess.Date);
    form3.Show;
    end;

    Basically, this procedure transfers all the data of the selected message (i.e body, from, to subject, and so on) from form1 to the appropriate form fields in form3. Once form3 is shown, you will have the option of replying to the message or closing the form.

    Here's a screenshot of some spam I just received in my inbox:

    To be able to reply to the message, double click on the reply button and type the following code:

    procedure TForm3.btReplyClick(Sender: TObject);
    begin
    //clear fields for reply
    form2.edto.clear;
    form2.edfrom.Clear;
    form2.memo1.Clear;
    form2.edsubject.Clear;
    //fill them up!
    form2.edfrom.Text:=edit2.Text;
    form2.edto.Text:=edit1.Text;
    form2.edsubject.Text:='Re: '+edit3.Text;
    form2.memo1.Lines.Add('');
    form2.memo1.Lines.Add('--------Original Message-----------');
    form2.memo1.Lines.Add('From ' + edit1.Text);
    form2.memo1.Lines.Add('To ' + edit2.Text);
    form2.memo1.Lines.Add('Subject ' + edit3.Text);
     form2.memo1.Lines.Add('Date ' + label1.Caption);
    form2.memo1.Lines.Add(memo1.Lines.Text);
    form2.Caption:='Reply to ' + edit1.Text;

    form2.Show;
    form3.Close;
    end;

    Here's a screenshot of how I reply to the spam:

    The application as it is, is not very flexible, in terms of changing your email server login details. It might be that your details change for whatever reason, and that you would want to update the application. Well, an easy way (but by no means the safest) is to create a ini file and get your application to load your login credentials when it starts. Go to http://delphi.about.com/od/objectpascalide/l/aa120401a.htm  to see how to create and use ini files. The best and safest way to store your login credentials is to put them in a database.

    That's all there is to it! Indy10 makes it that difficult and that simple!

    NB: Some units declared in the uses clause may differ depending on what version of Indy10 you are using, so make sure to download the latest snapshot at: www.indyproject.org 


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · For thos of you who want to know more about how to use Delphi and MS Access go to...
     

    DELPHI-KYLIX ARTICLES

    - 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...
    - Using the Client Dataset in Two-Tiered Clien...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT