Setting email attachment name using TIdMessageBuilderHtml
<p>I'm trying to attach a PDF document to an email using Delphi (D10S) and I'd like to set the name to something other than the name of the file on the file system.</p>

<p>I found the following thread (from 2011) where Remy Lebeau states that you can achieve this by setting the <code>Name</code> property of the <code>TIdMessageBuilderAttachment</code> object when attaching them to the email using the <code>TIdMessageBuilderHtml</code>:</p>

<blockquote>
  <p><a href="http://codeverge.com/embarcadero.delphi.winsock/how-to-name-attachment-files-const/1072516" rel="nofollow">How to name attachment files constructed by TIdMessageBuilderHtml</a></p>
</blockquote>

<p>However, as simple as that sounds, it does not seem to work for me. The email comes through, but the attachment comes through with the original file name, not the one I've specified. </p>

<p>The following is a snippet of code that I expect to do as I've described but, for whatever reason, does not. In this case, I'd like the filename to come through as <code>desired_filename.pdf</code>, but it comes through as <code>undesired_filename.pdf</code>. I've removed the mail server credentials for obvious reasons:</p>

<pre><code>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 := '&lt;Insert Sender Address&gt;';
  FMessage.Sender.Name := '&lt;Insert Sender Name&gt;';
  FMessage.From.Address := '&lt;Insert From Address&gt;';
  FMessage.From.Name := '&lt;Insert From Name&gt;';
  FMessage.Recipients.EMailAddresses := '&lt;Insert Recepient Address&gt;';
  FMessage.Subject := 'Attachment Test';

  FSMTP.Host := '&lt;Insert Mail Host&gt;';
  FSMTP.Username := '&lt;Insert Username&gt;';
  FSMTP.Password := '&lt;Insert Password&gt;';
  FSMTP.Connect;
  FSMTP.Send(FMessage);
  FSMTP.Disconnect;
end;
</code></pre>

<p>I've tested this in D10S and XE and both do the same. Any ideas what I'm doing wrong?</p>