Configure email attachment name using TIdMessageBuilderHtml

I am trying to attach a PDF document to email using Delphi (D10S) and I would like to set a name for something other than the file name in the file system.

I found the following thread (from 2011) where Remy Lebeau states that you can achieve this by setting the property of the Nameobject TIdMessageBuilderAttachmentwhen attaching them to an email using TIdMessageBuilderHtml:

How to name attachment files created by TIdMessageBuilderHtml

However, as simple as it seems, this does not work for me. A letter arrives, but the application comes with the original file name, not the one I specified.

Below is a snippet of code that I expect to do as I described, but for some reason does not. In this case, I would like the file name to desired_filename.pdfappear as, but it appears as undesired_filename.pdf. I deleted the mail server credentials for obvious reasons:

procedure TForm4.Button1Click(Sender: TObject);
var
  FMessageBuilder : TIdMessageBuilderHtml;
  FSMTP : TIdSMTP;
  FMessage : TIdMessage;
  FAttachment : TIdMessageBuilderAttachment;
begin
  FMessage := TIdMessage.Create();
  FMessageBuilder := TIdMessageBuilderHtml.Create;
  FSMTP := TIdSMTP.Create;

  FAttachment := FMessageBuilder.Attachments.Add('c:\undesired_filename.pdf');
  FAttachment.Name := 'desired_filename.pdf';
  FMessageBuilder.FillMessage(FMessage);

  FMessage.Sender.Address := '<Insert Sender Address>';
  FMessage.Sender.Name := '<Insert Sender Name>';
  FMessage.From.Address := '<Insert From Address>';
  FMessage.From.Name := '<Insert From Name>';
  FMessage.Recipients.EMailAddresses := '<Insert Recepient Address>';
  FMessage.Subject := 'Attachment Test';

  FSMTP.Host := '<Insert Mail Host>';
  FSMTP.Username := '<Insert Username>';
  FSMTP.Password := '<Insert Password>';
  FSMTP.Connect;
  FSMTP.Send(FMessage);
  FSMTP.Disconnect;
end;

I tested this on D10S and XE, and both did the same. Any ideas what I'm doing wrong?

+4
source share
2 answers

TIdMessageBuilderAttachmenthas the properties FileNameand Name. When adding an attachment to the builder, these values ​​are assigned to the corresponding properties of the object TIdAttachmentthat is added to the collection TIdMessage.MessageParts.

Name, Name Content-Type, :

Content-Type: media/type; name="desired_filename.pdf"

FileName, FileName Content-Disposition, :

Content-Disposition: attachment; filename="undesired_filename.pdf"

, Content-Disposition . .

TIdMessageBuilderAttachment FileName, . , TIdMessageBuilderAttachment , . TIdAttachmentFile FileName , , . TIdAttachmentFile FileName . StoredPathName, , FileName , .

@fantaghirocco TStream TIdAttachmentMemory TIdMessage TIdAttachmentFile. , FileName , .

, TIdMessageBuilderAttachment , FileName StoredPathName. TIdAttachmentFile.FileName TIdMessage, :

FAttachment := FMessageBuilder.Attachments.Add('c:\undesired_filename.pdf');
FAttachment.Name := 'desired_filename.pdf';
FMessageBuilder.FillMessage(FMessage);

for I := 0 to FMessage.MessageParts.Count-1 do
begin
  if FMessage.MessageParts[I].PartType = mptAttachment then
    FMessage.MessageParts[I].FileName = FMessage.MessageParts[I].Name;
end;
+3

TIdMessageBuilderAttachments.Add, TStream TIdMessageBuilderAttachment.FileName , XE4, Indy 10.6.0.4975.

stream := TFileStream.Create('c:\undesired_filename.pdf', fmOpenRead);
FAttachment := FMessageBuilder.Attachments.Add(stream, 'application/pdf');
FAttachment.FileName := 'desired_filename.pdf';
+5

All Articles