Attachment with MailMessage Component

Posted on 2005-04-18
Last Modified: 2010-04-05
I found following code for sending an e-mail with the Indy component MailMessage:

First time I send an e-mail with an attachment it is OK, but second time the e-mail sent is attached the same file twice. And the third time I send an e-mail it is attached with the same e-mail three times.

But when I close the application and send an e-mail the file is only attached once.
So why do I have do close my application to avoid the many attachments with the same file?

procedure TMailerForm.btnSendMailClick(Sender: TObject);

  //setup SMTP
  SMTP.Host := ledHost.Text;
  SMTP.Port := 25;

  //setup mail message
  MailMessage.From.Address := ledFrom.Text;
  MailMessage.Recipients.EMailAddresses := ledTo.Text + ',' + ledCC.Text;

  MailMessage.Subject := ledSubject.Text;
  MailMessage.Body.Text := Body.Text;

  if FileExists(ledAttachment.Text) then
    TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text);

  //send mail
    except on E:Exception do
      StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message);
    if SMTP.Connected then SMTP.Disconnect;

end; (* btnSendMail Click *)
Question by:rainbowsoftware
    LVL 15

    Expert Comment

    You are not clearing the variable MailMessage and recreating it.
    Thus everytime you get to TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text) it is adding the attachment again
    LVL 15

    Accepted Solution

    If you dont want to clear it then maybe just use a flag to check if you have already attached the item to the mail message

      if FileExists(ledAttachment.Text) and (not bAttached) then
        TIdAttachment.Create(MailMessage.MessageParts, ledAttachment.Text);
        bAttached := True;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now