Error sending mail with attachment

Giganews Newsgroups
Subject: Error sending mail with attachment
Posted by:  Hugo Pedersen (oz8…@hotmail.com)
Date: Fri, 4 Jun 2010

I use the following code for sending messages with attached files. But for
some strange reason the attached file changes name somewhere in the process
and ends up being called attxxxxx.dat and that is not OK :-)

Delphi 2009, Indy 10

procedure TIndyMail.SendAttachment(aHost, aRecipient, aSender, aSender_Name,
aSubject, aMessage, aFilename: string);
var
  IdMessage: TIdMessage;
  IdSMTP: TIdSMTP;
  Attachment: TIdAttachmentFile;
begin
  IdMessage := TIdMessage.Create(nil);
  IdSMTP := TIdSMTP.Create(nil);
  Attachment := nil;
  try
    with IdSMTP do
      begin
        Host := aHost;
        Port := fPort;
      end;
    with IdMessage do
      begin
        From.Address := aSender;
        From.Name := aSender;
        Recipients.EMailAddresses := aRecipient;
        Subject := aSubject;
        Body.Text := aMessage;
    end;
    if aFilename <> '' then
      Attachment := TIdAttachmentFile.Create(IdMessage.MessageParts,
TFileName(aFilename));
    IdSMTP.Connect;
    try
      IdSMTP.Send(IdMessage);
    except
      on E: Exception do
        begin
          Logfile.Error(E.Message);
        end;
    end;
  finally
    if IdSMTP.Connected then
      IdSMTP.Disconnect;
    if Assigned(Attachment ) then
    FreeAndNil(Attachment);
    if Assigned(IdMessage) then
    FreeAndNil(IdMessage);
    if Assigned(IdSMTP) then
    FreeAndNil(IdSMTP);
  end;
end;

--
/* Vy 73 de OZ8HP / OZ1IIQ
/* Hugo Pedersen
/*http://www.hugopedersen.dk

Replies