How to build a text mail message with file attachments and send it via SMTP

procedure TForm1.btnSendClick(Sender: TObject);
  //Connecting to an SMTP service
  clSMTP1.Server := '';
  clSMTP1.UserName := '';
  clSMTP1.Password := 'secret';
  //Composing a new message
  clMailMessage1.BuildMessage('A message text follows here.', ['C:\MyFiles\']);
  clMailMessage1.From.FullAddress := '';
  clMailMessage1.Subject := 'Subject line';
  //Sending the message
  //Closing the connection


Add Feedback
Your sample code does not show how to add file attachments.
Homer Jones (October 11, 2018 at 9:43 PM)
The attachment is added automatically when you call the BuildMessage method and specify the attachment file path. You can specify multiple files using this method:

clMailMessage1.BuildMessage('A message text follows here.', ['C:\MyFiles\', 'C:\MyFiles\AnotherFile.docx']);

For sure, you can add attachments manually using the TclMailMessage.Bodies.AddAttachment method. But in such a case, you will have to build a valid MIME bodies structure. In case of a text-plain message with attachments, this structure is simple: one text body and some more attachment bodies. In case of a multipart-alternative message, which includes both text, HTML, inline images, etc., this structure becomes complicated. See the following article for more details:

Sergey Shirokov (November 15, 2019 at 11:54 AM)

Add Feedback