Indy10 Delphi7 SMTP locks up with attachment

Giganews Newsgroups
Subject: Indy10 Delphi7 SMTP locks up with attachment
Posted by:  Jamie Cunningham (jami…@tampabay.rr.com)
Date: Thu, 21 Oct 2004

Hi! I'm trying to send a message with attachment. If I send the message
without the attachment it goes just fine... as soon as I attach a file it
locks the system up when I hit send... So I must be doing something wrong
(and there's probably more wrong than I realize) :)  I looked for some
examples, but all I can find are snippets that really don't help im the
overall flow for me, because I don't know if I'm missing a step before or
after, etc... anyway, if anybody has a good example of sending an HTML
Message with attachments with Delphi 7 and Indy 10, can you point me in the
right direction? Thanks a lot... and my code is below in case anybody wants
to tell me what's wrong with it! :)

Note: This code WAS originally working with Indy 9, so it may be that I've
missed several 9 to 10 conversions or something...

procedure TOutBoxForm.SendButtonClick(Sender: TObject);
  var
  GoodToGo : Boolean;
  X, SentCount, ErrorCount : Integer;
  HTMLMessageBody,PlainMessageBody : TStringList;
  ImageName, HeloName : String;
  NewText : TIDText;
  NewAttachment : TIDAttachment;
  begin
  GoodToGo := True;

  Timer1.Enabled := False;

  If GoodToGo = True then
      Begin
      SMTP.Disconnect;
      SMTP.Host :=
MainMenuForm.EmployeeQuery.FieldByName('SMTPServer').AsString;
      SMTP.Port :=
MainMenuForm.EmployeeQuery.FieldByName('SMTPPort').AsInteger;
      SMTP.UserName :=
MainMenuForm.EmployeeQuery.FieldByName('MailUserName').AsString;
      SMTP.Password :=
MainMenuForm.EmployeeQuery.FieldByName('MailPassword').AsString;

      HeloName := MainMenuForm.EmployeeQuery.FieldByName('Email').AsString;
      For X := 1 to POS('@',HeloName) do
        HeloName[X] := ' ';
      Heloname := Trim(HeloName);

      SMTP.HeloName := HeloName;
      try
        Sleep(1500);
        SMTP.Connect;
        // SMTP.Authenticate;
        Connected := True;
      except
          ShowMessage('Trouble connecting to MailServer');
      End;

      If Connected = True then
        Begin
        WorkingEMailMemo.Lines.Clear;
        WorkingEMailMemo.Lines := EMailMemo.Lines;

        SentCount := 0;
        ErrorCount := 0;
        ProgressBar1.Max := WorkingEMailMemo.Lines.Count;
        ProgressBar1.Position := 0;

        While WorkingEMailMemo.Lines.Count > 0 do
          Begin
          SMTPMessage.Clear;
          SMTPMessage.MessageParts.Clear;

          HTMLMessageBody := TStringList.Create;
          PlainMessageBody := TStringList.Create;

          HTMLMessageBody.AddStrings(HTMLDBMemo.Lines);
          PlainMessageBody.AddStrings(PlainTextDBMemo.Lines);

          NewText :=
TIdText.Create(SMTPMessage.MessageParts,HTMLMessageBody);
          NewText.ContentType:='text/html';

          NewText :=
TIdText.Create(SMTPMessage.MessageParts,PlainMessageBody);
          NewText.ContentType:='text/plain';

          EMailAttachmentQuery.First;
          While not(EMailAttachmentQuery.EOF) do
            Begin
            ImageName :=
EMailAttachmentQuery.FieldByName('FileName').AsString;

            NewAttachment := TIdAttachment.Create(SMTPMessage.MessageParts);
            NewAttachment.ContentType := 'image/jpeg';
            NewAttachment.contenttransfer := 'base64';
            NewAttachment.ExtraHeaders.Add('Content-ID : <'+ImageName+'>');
            EMailAttachmentQuery.Next;
            End;
          EMailAttachmentQuery.First;

          SMTPMessage.From.Address :=
MainMenuForm.EmployeeQuery.FieldByName('Email').AsString;
          SMTPMessage.From.Name :=
MainMenuForm.EmployeeQuery.FieldByName('Name').AsString;
          SMTPMessage.Subject := EMailQuery.FieldByName('Subject').AsString;

          SMTPMessage.Recipients.Clear;
          SMTPMessage.Recipients.EMailAddresses :=
WorkingEMailMemo.Lines[0];

          Try
            SMTP.Send(SMTPMessage);
            Application.ProcessMessages;

            EMailQuery.Edit;
            SentMemo.Lines.Add(WorkingEMailMemo.Lines[0]);
            EMailQuery.Post;

            WorkingEMailMemo.Lines.Delete(0);

            SentCount := SentCount + 1;
            SentCountLabel.Caption := IntToStr(SentCount);
          Except
            Application.ProcessMessages;

            EMailQuery.Edit;
            NotSentMemo.Lines.Add(WorkingEMailMemo.Lines[0]);
            EMailQuery.Post;

            WorkingEMailMemo.Lines.Delete(0);

            ErrorCount := ErrorCount + 1;
            ErrorCountLabel.Caption := IntToStr(ErrorCount);
          End;

          NotSentLabel.Caption := 'Not sent because of errors -
'+IntToStr(ErrorCount);
          SentLabel.Caption := 'Sent - '+IntToStr(SentCount);

          Application.ProcessMessages;
          ProgressBar1.Position := ProgressBar1.Position + 1;
          End; // End While WorkingEmail.Lines.Count > 0...

        SMTP.Disconnect;
        Connected := False;

        NewText.Free;
        NewAttachment.Free;
        HTMLMessageBody.Free;
        PlainMessageBody.Free;

        EMailQuery.Edit;
        EMailQuery['Sent'] := 'T';
        EMailQuery.Post;

        ShowMessage('Done Sending '+SentCountLabel.Caption+' EMails... There
were '+ErrorCountLabel.Caption+' Errors...');

        SentCountLabel.Caption := '0';
        ErrorCountLabel.Caption := '0';
        Progressbar1.Position := 0;
        End; // End If Connected = True...
      End; // End Are you sure...
  SMTP.Disconnect;
  Timer1.Enabled := True;
  end;

Replies