Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
procedure TForm1.UseDelphiToSendEmailWithAttachmentsFromMicrosoftOutlook(Sender: TObject);
var
OutlookApplication, OutlookMailItem: Variant;
begin
//be sure ComObj and Variants units are included in the "uses" clause

OutlookApplication := Null;
OutlookMailItem := Null;

try
//create Outlook OLE
OutlookApplication := CreateOleObject('Outlook.Application');
except
OutlookApplication := Null;
//add error/exception handling code as desired
end;

If VarIsNull(OutlookApplication) = False then
begin
try
//Create Outlook Mail Item
try
OutlookMailItem := OutlookApplication.CreateItem(0); //olMailItem = $00000000
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem
except
OutlookMailItem := Null;
//add error/exception handling code as desired
end;

If VarIsNull(OutlookMailItem) = False then
begin
OutlookMailItem.To := 'recipient@example.com'; //change the recipient email address
OutlookMailItem.Subject := 'Sending email with three attachments from Outlook programmatically';
OutlookMailItem.Body := 'This email, containing three attachments, is being sent from Outlook...';
OutlookMailItem.Attachments.Add('C:\PhysiologyWeb\delphi_code_examples\test.docx'); //change file name
OutlookMailItem.Attachments.Add('C:\PhysiologyWeb\delphi_code_examples\test.xlsx'); //change file name
OutlookMailItem.Attachments.Add('C:\PhysiologyWeb\delphi_code_examples\test.pptx'); //change file name
OutlookMailItem.Send;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/Outlook.MailItem.Send(method)
end;
finally
OutlookMailItem := Unassigned;
OutlookApplication := Unassigned;
end;
end;
end;




Posted: Saturday, September 3, 2022
Last updated: Tuesday, March 18, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL