?
Solved

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

Posted on 2008-01-30
10
Medium Priority
?
1,345 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:trevsoft
  • 7
9 Comments
 
LVL 21

Accepted Solution

by:
ziolko earned 2000 total points
ID: 20794906
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
 
LVL 21

Expert Comment

by:ziolko
ID: 20794955
just don't use my mail address please:)

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 20794978
also Indy samples can be found here:
http://www.indyproject.org/DemoDownloads/Indy_10_MailClient.zip

ziolko.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:ziolko
ID: 20795081
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
 
LVL 1

Author Comment

by:trevsoft
ID: 20803271
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
 
LVL 21

Expert Comment

by:ziolko
ID: 20803871
>>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
 
LVL 21

Expert Comment

by:ziolko
ID: 20803894
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
 
LVL 21

Expert Comment

by:ziolko
ID: 20821530
did it work?

ziolko.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21186027
Forced accept.

Computer101
EE Admin
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

600 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