Solved

URGENT!!! Loop ListView POP3 Problem

Posted on 2004-04-14
12
266 Views
Last Modified: 2010-04-05
Hi there,

I'll try to explain myself the best whey i can.

I'm writing an application that goes inside a timer.

The application collects mails every minute and if something is found, the emails are retrieved to a listview component.

Then one by one it will filter it and send it via smtp, until none are found and back again.

It now works, it's collecting the emails and filtering them and sending them, but only one.

After it does ONE on the list i have to close the application and when i run it again it will do the next.

I've had a lot of problems with deleting the emails that have been already sent, because i has to be automatic (timer based) and it will run on the system tray.

I think that me problem lies on the LOOPs that i'm using.

Can you PLEASE HELP!!!!   It's for work, and i'm really behind.

I am willing to upgrade my points for a solution.

Thanks

SFern
0
Comment
Question by:sfern
  • 8
  • 4
12 Comments
 

Author Comment

by:sfern
ID: 10825340
Here is the code:

It will go on a timer procedure:

P.S: Sorry about the mess in the code, i just don't know what to do anymore :(

procedure TOBJ_MainForm.Button3Click(Sender: TObject);
Const
    CRLF = #13#10; //For SMTP ActiveX

var
  i, mCount: integer; //For pop3 connect and retrive
  msg: TIdMessage;  //For pop3 connect and retrive
  email: IChilkatEmail; //For ChilKat Mail SMTP

begin
//Connect and retrieve messages to the listbox
//If pop.Connected=False then


  pop.Connect;
  try
    mCount := pop.CheckMessages;
    msg := TIdMessage.Create(self);
    memo1.Lines.Add('Connected');
    try
    for i := mCount downto 1 do

    begin

        pop.Retrieve(i, msg);
        // use only 0'th part (Plain Text)
        if msg.MessageParts.Count > 0 then
          AddToListView(TIdText(msg.MessageParts[0]).Body.Text, msg.From.Text)


        else
          AddToListView(msg.body.text, msg.from.text);
          EditMailSubject.Text:=msg.from.address; //Get the Sender
    //end;
      msg.Clear;

    end;

    finally
      msg.Free;
      memo1.Lines.Add('Messages Retrieved');

    end;
  finally
 end;
memo1.Lines.Add('Get the listview items');
//Put the contents of the mail to editboxes in smtp section
If ListView1.Items.Count = 0 then
begin
pop.Disconnect;
pop.Destroy;
memo1.Lines.Add('No Messages found');
exit;
end;


If ListView1.Items.Count > 0 then
     begin

       ID.Text:=( ListView1.Items[0].Caption);
       Edit9.Text:=( ListView1.Items[0].SubItems[0]);
       Edit10.Text:=( ListView1.Items[0].SubItems[1]);
       Edit11.Text:=( ListView1.Items[0].SubItems[2]);
       Edit12.Text:=( ListView1.Items[0].SubItems[3]);
       SendMailTo.Text:=( ListView1.Items[0].SubItems[4]);
memo1.Lines.Add('listview items passed');

      If StrToInt(ID.Text) <= 50 then SMTPBody.Lines.LoadFromFile('Chinese\'+ID.Text+'.txt') //If its Chinese
    else
       SMTPBody.Lines.LoadFromFile('Japanesse\'+ID.Text+'.txt'); // If its Japanesse
       memo1.Lines.Add('Start Parse Section');

//Start Filtering
//Search and Replace Title
SMTPBody.SelStart := AnsiPos('<Title>',SMTPBody.Text) - 1;
SMTPBody.SelLength := Length('<Title>');
SMTPBody.SelText := Edit10.Text;

//Search and Replace Firstname
SMTPBody.SelStart := AnsiPos('<Firstname>',SMTPBody.Text) - 1;
SMTPBody.SelLength := Length('<Firstname>');
SMTPBody.SelText := Edit11.Text;

//Search and Replace Surname
SMTPBody.SelStart := AnsiPos('<Surname>',SMTPBody.Text) - 1;
SMTPBody.SelLength := Length('<Surname>');
SMTPBody.SelText := Edit12.Text;

//Search and Replace Account Number
SMTPBody.SelStart := AnsiPos('<Account Number>',SMTPBody.Text) - 1;
SMTPBody.SelLength := Length('<Account Number>');
SMTPBody.SelText := Edit9.Text;

//Search and Replace Email
SMTPBody.SelStart := AnsiPos('<Email>',SMTPBody.Text) - 1;
SMTPBody.SelLength := Length('<Email>');
SMTPBody.SelText := SendMailTo.Text;
memo1.Lines.Add('End of parse section');

memo1.Lines.Add('Send mail section');
//send the mail
ChilkatMailMan1.UnlockComponent('unlockCode');
    ChilkatMailMan1.SmtpHost := (Edit5.Text);
    ChilkatMailMan1.SmtpPort := StrToInt(Edit6.Text);
    ChilkatMailMan1.SmtpUsername := (Edit7.Text);
    ChilkatMailMan1.SmtpPassword := (Edit8.Text);

    email := EmailFactory1.NewEmail();
    email.Subject := (OBJ_MAIL_SUBJECT.Text);
    email.Body := (SMTPBody.Text);
    email.AddTo('',SendMailTo.Text);
    email.From := EditMailSubject.Text;


    ID.Clear;
    Edit9.Clear;
    Edit10.Clear;
    Edit11.Clear;
    Edit12.Clear;
    SMTPBody.Clear;
    //memo1.Lines.Add('Mail has been send !');
    //ListView1.SetFocus;
    //ListView1.Selected.ImageIndex := 9;


       memo1.Lines.Add('See if mail has not been sent');
       if (ChilkatMailMan1.SendEmail(email) = 0) then
         begin

      //ListView1.SetFocus;
      //ListView1.Selected.ImageIndex := 1;
      Memo1.Lines.Add('Error found');
    end

     else
     begin
     memo1.Lines.Add('See if email has been sent');
     //ListView1.Selected.ImageIndex := 9;
     memo1.Lines.Add('message sent');

     //pop.Delete(i);  //Big problem here :(

     ListView1.Items[ListView1.Items.Count-1].Selected := True;
     //ListView1.SetFocus;

     //Select For Deletion on POP account
    if ListView1.Selected <> nil then
  begin
    //showbusy(true);
    If ListView1.Items.Count <> 0 then
    POP.Delete(ListView1.Selected.Index + 1);
    //POP.Delete(ListView1.Selected.Index + 0);
    //ListView1.Selected.ImageIndex := 3;
    //Showbusy(false);
  end;
 memo1.Lines.Add('Item marked for deletion');

     //ToolButton2Click(Sender);
     Listview1.items.delete(0);
     memo1.Lines.Add('item 0 delete frim listview');
     //ListView1.Refresh;
     showmessage('the end');

     //ChilkatMailMan1.Free;

       pop.Disconnect;



     //ListView1.Clear;
     //exit;


    end;
     end;

   end;
0
 

Author Comment

by:sfern
ID: 10825469
I'm getting -ERR message out of range (if it helps), when i try to send more than one message.
0
 

Author Comment

by:sfern
ID: 10825774
Question upgraded to 300 points...please help!!!!
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Expert Comment

by:Ratje
ID: 10832040
Looks like you are not looping through the listview:

If ListView1.Items.Count > 0 then
begin

 // your code

end;

Should be something like :
If ListView1.Items.Count > 0 then
begin
 for x := 1 to listview1.items.count do    // or for x:=  0 to listview1.items.count -1 do
 begin

  // your code

 end ;

// more code if needed
end;

Rat
0
 
LVL 2

Expert Comment

by:Ratje
ID: 10832062
I would start by breaking the code into seperate procedures :

procedure Tform1.Button1Click(Sender: TObject);
begin
  GetMailProcedure ;
  SendMailProcedure ;
  CleanUpProcedure ;
end ;

easier to read, maintain and troubleshoot :))

Rat
0
 

Author Comment

by:sfern
ID: 10833510
It's now looping but this code is giving me, -ERR message out of range.


    POP.Delete(ListView1.Selected.Index + 1);

although after clicking ok it will curry on and send the mails.


0
 

Author Comment

by:sfern
ID: 10833677
I think that my biggest problem lies on marking and deleting the emails after they have been sent.
0
 

Author Comment

by:sfern
ID: 10834278
I know now exactly where the problem is.

It in Deleting the sent mail from the pop3 account after they have been sent.
0
 

Author Comment

by:sfern
ID: 10835349
Ok, i managed to find a way to get it to work and it's now working :)

I know it's not part of the question but as i found the answer myself, could you please tell me how to copy items from one listview to another please?

I'll give you the points staight away.

Thanks

SFern.
0
 
LVL 2

Expert Comment

by:Ratje
ID: 10840215
Should be something like this : ( copy the entire listview)

procedure TForm1.Button1Click(Sender: TObject);
var
 idxItem, idxSub : Integer;
 ItemCount, SubCount: Integer;
begin
  with ListView1 do
  begin
    ItemCount := 0;
    SubCount  := 0;
    if Items.Count = 0 then
      ItemCount := 0
    else
      ItemCount := Items.Count;

     if Itemcount > 0 then
     begin
      for idxItem := 1 to ItemCount do
      begin
        with Items[idxItem - 1] do
        begin
          Listview2.Items.Add ;
          Listview2.Items[idxItem -1].Caption := Caption ;
          if SubItems.Count = 0 then
             SubCount := 0
          else
             SubCount := Subitems.Count;
          if Subcount > 0 then
          begin
            for idxSub := 0 to SubItems.Count -1 do
            begin
              Listview2.Items[idxItem - 1].SubItems.Add(subitems[idxsub]) ;
              ShowMessage(subitems[idxsub]);
            end ;
          end ;

        end ;
       end ;
      end ;
  end ;
end;

Rat
0
 
LVL 2

Accepted Solution

by:
Ratje earned 300 total points
ID: 10840392
Cleaned it up a bit :

procedure TForm1.Button1Click(Sender: TObject);
var
 idxItem, idxSub, IdxImage: Integer;
begin
  with ListView1 do
  begin
     if Items.Count > 0 then
     begin
      for idxItem := 1 to Items.count do
      begin
        with Items[idxItem - 1] do
        begin
          IdxImage := ImageIndex;
          Listview2.Items.Add ;
          Listview2.Items[idxItem -1].Caption := Caption ;
          Listview2.Items[idxItem -1].ImageIndex :=idxImage ;
          if Subitems.Count > 0 then
          begin
            for idxSub := 0 to SubItems.Count -1 do
            begin
              Listview2.Items[idxItem - 1].SubItems.Add(subitems[idxsub]) ;
            end ;
          end ;

        end ;
       end ;
      end ;
  end ;
end;
0
 

Author Comment

by:sfern
ID: 10840994
Thanks very much for all your help.
Here are your points.

SFern. :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now