PhysiologyWeb Logo  Search
PhysiologyWeb Loading...

Delphi Code - Send Email from Microsoft Outlook - Display before Sending
Here is a simple Delphi procedure that uses Microsoft Outlook to send email. The new email to be sent is displayed first, and the user must press "send" before the email is sent. This is useful if the user wishes to check the email before sending, or if there are aspects of the newly generated email that cannot be easily automated, and the user needs to add additional information to the email. The code works well whether or not Outlook is already open. However, please note that the code will not work if Outlook is closed, and your installed Outlook requires any form of authentication upon launching the program. Also, if Outlook is closed and the email is sent, the sent email will not show up in the "sent" folder until the Outlook client is opened. The most relevant parts of the code are highlighted. Comments are included to explain the code. If you copy and paste the code into your program, be sure to change the form and procedure names to match your setup. For additional Excel automation solutions, see Microsoft Outlook Automation with Delphi.
procedure TForm1.UseDelphiToSendEmailFromMicrosoftOutlookDisplay(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 - Display before sending';
OutlookMailItem.Body := 'This email is being sent from Outlook... Display before sending...';
OutlookMailItem.Display;
//reference
//https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.display
end;
finally
OutlookMailItem := Unassigned;
OutlookApplication := Unassigned;
end;
end;
end;






Posted: Saturday, May 14, 2022