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