Delphi 2006 and Indy, Sending attachments not work ...

Hi Experts,

I seem to be having a problem with sending attachments via Indy 10 and Delphi 2006.

I have attached the code for you too look at, the code works fine until i add the code to attach a file.

function TPurchaseOrdersFrm.SendMail(FileToSend: String): String;
var
  MailMsg        : TidMessage;
  MailFile       : TidAttachment;
begin
  // send the mail with the attachment
  MailMsg := TidMessage.Create(idSMTP);
  // from
  MailMsg.From.Address := 'someone@domain.com'; //GetOption('FromAddress');
  MailMsg.From.Name    := 'Someones Name'; //Application.Title;
  // to
  MailMsg.Recipients.Add;
  MailMsg.Recipients.Items[0].Address := 'someone@domain.com'; //GetOption('ToAddress');
  MailMsg.Subject      := 'Subject';
  // the attachment
  MailFile             := TidAttachment.Create(MailMsg.MessageParts, FileToSend);
  MailFile.DisplayName := MailMsg.Subject;

  idSMTP.Connect();
  idSMTP.Send(MailMsg);

  // close connection and free memory
  idSMTP.Disconnect;
  MailFile.Free;
  MailMsg.Free;
end;

Please help, your help would be greatly appreciated.

Regards,





Trevor
LVL 1
trevsoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
try this:

procedure TForm1.Button5Click(Sender: TObject);
var
  MailMsg        : TidMessage;
  MailFile       : TIdAttachmentFile;
begin
// send the mail with the attachment
  MailMsg := TidMessage.Create(IdSMTP1);
  // from
  MailMsg.From.Address := 'omcrew@gazeta.pl'; //GetOption('FromAddress');
  MailMsg.From.Name    := 'myself'; //Application.Title;
  // to
  MailMsg.Recipients.Add;
  MailMsg.Recipients.Items[0].Address := 'omcrew@gazeta.pl'; //GetOption('ToAddress');
  MailMsg.Subject      := 'Subject';
  // the attachment
  MailFile             := TidAttachmentFile.Create(MailMsg.MessageParts, fileToSend);
  MailFile.DisplayName := MailMsg.Subject;

  idSMTP1.Connect();
  idSMTP1.Send(MailMsg);

  // close connection and free memory
  idSMTP1.Disconnect;
  MailFile.Free;
  MailMsg.Free;
end;


ziolko.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ziolkoCommented:
just don't use my mail address please:)

ziolko.
0
ziolkoCommented:
also Indy samples can be found here:
http://www.indyproject.org/DemoDownloads/Indy_10_MailClient.zip

ziolko.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ziolkoCommented:
alternative version:

procedure TForm1.Button5Click(Sender: TObject);
var
  MailMsg        : TidMessage;
  MailFile       : TIdAttachmentFile;
begin
// send the mail with the attachment
  MailMsg := TidMessage.Create(IdSMTP1);
  // from
  MailMsg.From.Text := 'someone@host.com'; //GetOption('FromAddress');
  MailMsg.From.Name    := 'myself'; //Application.Title;
  // to
  MailMsg.Recipients.EMailAddresses := 'dest@host.com';
  MailMsg.Subject      := 'Subject';
  // the attachment
  MailFile             := TidAttachmentFile.Create(MailMsg.MessageParts, fileToSend);
  MailFile.DisplayName := MailMsg.Subject;

  idSMTP1.Connect();
  idSMTP1.Send(MailMsg);

  // close connection and free memory
  idSMTP1.Disconnect;
  MailFile.Free;
  MailMsg.Free;
end;

ziolko.
0
trevsoftAuthor Commented:
Hi ziolko,

Yes that worked fine, the TidAttachmentFile made the difference.

Just one more thing ... how do add more addresses to the To Field in an email. I tried seperating them with ';' but it doesnt like it to be that way.

im trying to use:
MailMsg.Recipients.Items[0].Address

and attaching a string list to the code but it doesnt seem to like it.

Any ideas ????

Regards,




Trevor
0
ziolkoCommented:
>>I tried seperating them with ';' but it doesnt like it to be that way

you tried that like this:
MailMsg.Recipients.Items[0].Address := 'rcp1@host.com;rcp2@host.com';
or like this:
MailMsg.Recipients.EMailAddresses := 'rcp1@host.com;rcp2@host.com';

ziolko
0
ziolkoCommented:
unfrt. I won't be able to check this until monday by try also:

MailMsg.Recipients.EMailAddresses := 'rcp1@host.com,rcp2@host.com'
or
MailMsg.Recipients.Add.Text := 'rcp1@host.com';
MailMsg.Recipients.Add.Text := 'rcp2@host.com';

zioko.
0
ziolkoCommented:
did it work?

ziolko.
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.