Delphi: Send An Email With An Attachment Using OLE
Join the DZone community and get the full member experience.Join For Free
In Delphi, this allows you to launch a new outlook window with an attachment. This will not work with Outlook Express. This code was modified from the version seen here More info here
uses ComObj //Delphi 5 or later procedure TfrmHUD1_1.EmailDoc( path : String ); const olMailItem = 0; var Outlook: OLEVariant; MailItem: Variant; begin try Outlook:=GetActiveOleObject('Outlook.Application') ; except Outlook:=CreateOleObject('Outlook.Application') ; end; MailItem := Outlook.CreateItem(olMailItem) ; MailItem.Recipients.Add('firstname.lastname@example.org') ; MailItem.Subject := 'Subject: Outlook Mail From Delphi'; MailItem.Body := 'Body of the email'; MailItem.Attachments.Add(path) ; MailItem.Display; //MailItem.Send to skip the window and just send the email VarClear(Outlook); end;
Opinions expressed by DZone contributors are their own.