rainbowsoftware
asked on
Attachment with MailMessage Component
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.btnSendMailCli ck(Sender: TObject);
begin
StatusMemo.Clear;
//setup SMTP
SMTP.Host := ledHost.Text;
SMTP.Port := 25;
//setup mail message
MailMessage.From.Address := ledFrom.Text;
MailMessage.Recipients.EMa ilAddresse s := ledTo.Text + ',' + ledCC.Text;
MailMessage.Subject := ledSubject.Text;
MailMessage.Body.Text := Body.Text;
if FileExists(ledAttachment.T ext) then
TIdAttachment.Create(MailM essage.Mes sageParts, ledAttachment.Text);
//send mail
try
try
SMTP.Connect(1000);
SMTP.Send(MailMessage);
except on E:Exception do
StatusMemo.Lines.Insert(0, 'ERROR: ' + E.Message);
end;
finally
if SMTP.Connected then SMTP.Disconnect;
end;
end; (* btnSendMail Click *)
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.btnSendMailCli
begin
StatusMemo.Clear;
//setup SMTP
SMTP.Host := ledHost.Text;
SMTP.Port := 25;
//setup mail message
MailMessage.From.Address := ledFrom.Text;
MailMessage.Recipients.EMa
MailMessage.Subject := ledSubject.Text;
MailMessage.Body.Text := Body.Text;
if FileExists(ledAttachment.T
TIdAttachment.Create(MailM
//send mail
try
try
SMTP.Connect(1000);
SMTP.Send(MailMessage);
except on E:Exception do
StatusMemo.Lines.Insert(0,
end;
finally
if SMTP.Connected then SMTP.Disconnect;
end;
end; (* btnSendMail Click *)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thus everytime you get to TIdAttachment.Create(MailM