sending mails in threads

Giganews Newsgroups
Subject: sending mails in threads
Posted by:  Tom (nospam@nospamplease.com)
Date: Fri, 28 Apr 2006

Hello,

I am new to threading and have just begun with the thread demo that is
shipped with delphi.
I have changed the program in a first step to "fill" 100 progressbars
"parallel". This wasn't really a hard job.
When I now "replace" the progressbars with my "mail method" my program
shows a strange behavior:

When I start the program 101 threads are started (task-manager says)
But the mails are sent one after the other... Most of the time the
order is:
thread1, thread2, thread3,... sometimes thread68 starts rigth after
thread45, then 46,47,...
But the main problem is [the reason why I post it here] sending a mail
takes about one second to a mail server in my 100MBit lan.

To reach this speed I can also send all mails the "normal way".

Below you can find the source code - this is a prototype for testing.
[The idea is to pass an array of mail addresses and mail bodies and let
the thread send them in a loop]

Is this the right way ?  [The application has to send 10000-13000 mails
at once - NO SPAM - a newsletter for fans of music artists]

Thanks for your help

Thomas

------------------ source start -------------------------

  TMailoutThreads = Class(TThread)
  private
    Box : TMyPaintBox;
    SMTP : TidSMTP;
    Msg : TidMessage;
    aktuelle_Adresse : Integer;
    Server : TServerdaten;
    Maildaten : TMaildaten;
    Adressen  : Array of TAdressen;
    Procedure DoVisualMailout;
    procedure Versende_Email (Index : Integer);
  protected
    Procedure VisualMailout;
    Procedure Execute; override;
  public
    Constructor Create (ABox : TMyPaintBox;AServer : TServerdaten;
AMaildaten : TMaildaten; AAdressen : Array of TAdressen);
  end;

implementation

{ TMailoutThreads }

constructor TMailoutThreads.Create(ABox : TMyPaintBox;AServer :
TServerdaten; AMaildaten : TMaildaten; AAdressen : Array of TAdressen);
VAR I : Integer;
begin
  Box := ABox;
  Server := AServer;
  SMTP := TidSMTP.Create(nil);
  Msg := TidMessage.Create(nil);
  Maildaten := AMaildaten;
  aktuelle_Adresse := 0;
  SetLength(Adressen,High(AAdressen)+1);
  For I := 0 To High(AAdressen) Do
    Begin
      Adressen[I].Mailadresse := AAdressen[I].Mailadresse;
      Adressen[I].Daten := TStringList.Create;
      Adressen[I].Daten.Clear;
      Adressen[I].Daten.Assign(AAdressen[I].Daten);
    End;
  FreeOnTerminate := True;
  inherited Create(False);
end;

procedure TMailoutThreads.DoVisualMailout;
VAR I : Integer;
begin
  Box.Fortschritt := (aktuelle_ADresse * 100) DIV (High(Adressen))+1;
  Box.Invalidate;
  Box.Repaint;
  Versende_Email (aktuelle_Adresse);
end;

procedure TMailoutThreads.Execute;
Begin
  VisualMailout;
End;

procedure TMailoutThreads.VisualMailout;
VAR I : Integer;
begin
  For I := Low(Adressen) To High(Adressen) Do
    Begin
      aktuelle_Adresse := I;
      Synchronize(DoVisualMailout);
      If Terminated
        Then
          Begin
//            FreeAndNil(Msg);
//            FreeAndNIl(SMTP);
            Exit;
          End;
    End;
End;

Procedure TMailoutThreads.Versende_Email;
Var I                  : Integer;
    Fehler            : Boolean;
begin
  Fehler := False;
  With Msg Do
    Begin
      Clear;
      From.Address := Server.ReplyTo;
      From.Text := Server.ReplyTo;
      Recipients.EMailAddresses := Adressen[Index].Mailadresse;
      Subject := Maildaten.Betreff;
      Body.Assign (Adressen[Index].Daten);
      ReceiptRecipient.Text := '';
    End;
//  For I := 0 To Maildaten.Attachments.Count -1 DO
//    TIDAttachment.Create(Msg.Messageparts,Maildaten.Attachments[I]);
  SMTP.Port := Server.SmtpServerPort;
  CASE Server.SmtpAuthType of
    0 :  SMTP.AuthType := atNone;
    1 :  SMTP.AuthType := atDefault;
  END;
  SMTP.Username := Server.SmtpServerUser;
  SMTP.Password := Server.SmtpServerPassword;
  SMTP.Host := Server.SmtpServerName;
  try
    try
codesite.SendMSg('MAIL los');
      SMTP.Connect;
      SMTP.Send(Msg);
    except
      on e : exception do
        Begin
          Fehler := True;
        End;
    end;
  finally
    SMTP.Disconnect;
codesite.SendMSg('MAIL wech');
    If Fehler
      then
        Begin
        End;
  end;
------------------ source end -------------------------

Replies