CarloM 3 Posted July 8, 2020 Hello, Last years I used Mapi for compose email and send it via outlook. I found this code that works well on my PC. Anybody can say me what is the best method for compose and send on outlook actually ? const olMailItem = 0; var Outlook, MailItem: OLEVariant; begin try Outlook := GetActiveOleObject('Outlook.Application'); except Outlook := CreateOleObject('Outlook.Application'); end; MailItem := Outlook.CreateItem(olMailItem); MailItem.Recipients.Add('test@gmail.com'); MailItem.Subject := 'your subject'; MailItem.htmlBody := Memo1.Lines.Text; MailItem.Display; Outlook := Unassigned; Share this post Link to post
sh17 26 Posted July 9, 2020 Not very different TMailOutlook = class(TObject) private const olMailItem = $00000000; private class function HaveActiveOleObject(const ClassName: string): boolean; public class procedure Execute(const _To,_ToCC,_ToBCC,_Subject,_Body,_Attachment : String); end; { TMailOutlook } class procedure TMailOutlook.Execute(const _To, _ToCC, _ToBCC, _Subject, _Body,_Attachment: String); var Outlook: OleVariant; MailV: Variant; begin if not HaveActiveOleObject('Outlook.Application') then Outlook := CreateOleObject('Outlook.Application') else Outlook:= GetActiveOleObject('Outlook.Application'); MailV := Outlook.CreateItem(olMailItem); if (_To <> '') then MailV.To := _To; if (_ToBCC <> '') then MailV.BCC:= _ToBCC; if (_ToCC <> '') then MailV.CC:= _ToCC; MailV.Subject := _Subject; MailV.Body := _Body; if _Attachment <> '' then if FileExists(_Attachment) then MailV.Attachments.Add(_Attachment); MailV.Display; end; //// Want to Bypass exception so we check this without using the activex unit class function TMailOutlook.HaveActiveOleObject( const ClassName: string): boolean; var ClassID: TCLSID; Unknown: IUnknown; oleresult: HResult; begin ClassID := ProgIDToClassID(ClassName); oleResult:= GetActiveObject(ClassID, nil, Unknown); result:= Succeeded(oleResult); end; 1 Share this post Link to post
CarloM 3 Posted July 9, 2020 Thank you, Used this class for years ago ? is it compatible with most outlook (2010.... 365 ) ? Share this post Link to post
MACCOB2004 0 Posted January 27, 2023 Hello sh17, let me confirm that it works correctly, even with Outlook 2019. Thanks from me too :) Share this post Link to post
erikv 0 Posted September 18, 2023 Thank you for the above solution Is it possible to check if mail is actually send? Share this post Link to post