How do I add attachments to email using Indy?

Abstract: How do I add attachments to email using Indy?

Product: Borland�Developer Studio�2006�
Platform: Not Applicable

In order to add attachments to an email using Indy, create an instance of TIdAttachmentFile, passing in the MessageParts property of an instance of TIdMessage, together with a string specifying the location of a file to be attached.

{$APPTYPE CONSOLE}

uses
� SysUtils, IdSMTP, IdAttachment, IdMessage, IdMessageParts, IdEMailAddress, IdAttachmentFile;
var
� IdMessage1: TIdMessage;
� IdSMTP1: TIdSMTP;
� Addressee: TIdEmailAddressItem ;
� Attachment: TIdAttachment;
begin

� IdMessage1 := TIdMessage.Create(nil);
� IdSMTP1 := TIdSMTP.Create(nil);
� IdSMTP1.Host := 'smtp.borland.com'; // SMTP server host name

� IdMessage1.Body.Add('Hi Chee Wee, this is the secret plans for Highlander that we talked about.');
� IdMessage1.Subject := 'This message contains an attachment';
� IdMessage1.From.Text := 'Frank_Borland@borland.com';
� IdMessage1.From.Name :=� 'Frank Borland';

� Attachment := TIdAttachmentFile.Create(IdMessage1.MessageParts, 'c:\temp\HighlanderSecretPlans.zip');

� Addressee := IdMessage1.Recipients.Add;
� Addressee.Address := 'Chee_Wee_Chua@borland.com'; // email address of recipient
� Addressee.Name := 'Chee Wee Chua';

� IdSMTP1.Username := 'frankborland'; // SMTP user name
� IdSMTP1.Password := '1234notgonnatellya'; // SMTP user password
� IdSMTP1.Connect;
� IdSMTP1.Send(IdMessage1);
� IdSMTP1.Disconnect;

� Attachment.Free;
� IdMessage1.Free;
� IdSMTP1.Free;

end.