TSendMail (Delphi)
Description
This example shows how to use a TSendMail action to send an e-mail with a file attached.
To create the project
- Create a Delphi VCL Forms Application
 - Drag and drop on the main form (Form1) a button (TButton), a file open dialog (TOpenDialog), and an action list (TActionList).
 - Add a TSendMail action:
 
- Right-click the action list object and select Action List Editor....
 - Select New Standard Action (Ctrl+Ins).
 - From the Internet action class, select TSendMail.
 
- Write the following code in the button's Onclick event.
 
Code
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if FileExists(OpenDialog1.FileName) then
    begin
      SendMail1.Attachments.Add(OpenDialog1.FileName);
      SendMail1.Execute;
    end
    else
      raise Exception.Create('No such file');
end;
Result
When the button (Button1) is pressed, the file open dialog (OpenDialog1) is shown. The user must select a file. When the Open button is pressed, the selected file is added as an attachment to the e-mail and the e-mail is shown for editing (with the help of any e-mail application available on the computer).
Uses
- Vcl.ExtActns.TSendMail.Attachments ( fr | de | ja )
 - Vcl.ActnList.TCustomAction.Execute ( fr | de | ja )
 - Vcl.Dialogs.TOpenDialog.Execute ( fr | de | ja )
 - Vcl.Dialogs.TOpenDialog.FileName ( fr | de | ja )