C++Builder SendMail Example Code for sending an email with an attachment

By: Tim DelChiaro

Abstract: This example shows how to use a TSendMail action to send an e-mail with a file attached

    TSendMail (C++)

 

    Language:

  • C++

Contents

    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 C++Builder 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

void __fastcall TForm1::Button1Click(TObject *Sender) {
	if (OpenDialog1->Execute()) {
		if (FileExists(OpenDialog1->FileName, false)) {
			SendMail1->Attachments->Add(OpenDialog1->FileName);
			SendMail1->Execute();
		}
		else {
			throw new Exception("No such file");
		}
	}
}

    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

    See Also

Server Response from: ETNASC03