Jump to content
CarloM

Sending email and compose via Outlook

Recommended Posts

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

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;

 

Share this post


Link to post

Thank you,

 

Used this class for years ago ? is it compatible with most outlook (2010.... 365 ) ?

Share this post


Link to post

Yes, definitely in Office 365.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×