procedure TForm1.UseDelphiToSendEmailFromMicrosoftOutlook(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);
//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 from Outlook programmatically';
OutlookMailItem.Body := 'This email is being sent from Outlook...';
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, May 14, 2022
Last updated: Tuesday, March 18, 2025