• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

URGENT!!! Loop ListView POP3 Problem

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
sfern
Asked:
sfern
  • 8
  • 4
1 Solution
 
sfernAuthor Commented:
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
 
sfernAuthor Commented:
I'm getting -ERR message out of range (if it helps), when i try to send more than one message.
0
 
sfernAuthor Commented:
Question upgraded to 300 points...please help!!!!
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
RatjeCommented:
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
 
RatjeCommented:
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
 
sfernAuthor Commented:
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
 
sfernAuthor Commented:
I think that my biggest problem lies on marking and deleting the emails after they have been sent.
0
 
sfernAuthor Commented:
I know now exactly where the problem is.

It in Deleting the sent mail from the pop3 account after they have been sent.
0
 
sfernAuthor Commented:
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
 
RatjeCommented:
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
 
RatjeCommented:
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
 
sfernAuthor Commented:
Thanks very much for all your help.
Here are your points.

SFern. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now