Recent

Author Topic: emailing from application  (Read 6901 times)

draggon

  • New Member
  • *
  • Posts: 41
emailing from application
« on: October 02, 2018, 02:46:35 pm »
I am trying to send and e-mail from applcation. I found piece of code in one forum:

Code: Pascal  [Select][+][-]
  1. procedure send_email(r, s, b: String);
  2.  
  3. const
  4.   olMailItem = 0;
  5.   olByValue = 1;
  6.  
  7. var
  8.   OutlookApp, MailItem: OLEVariant;
  9.  
  10. begin
  11.   try
  12.     begin
  13.       OutlookApp:= GetActiveOleObject('Outlook.Application');
  14.     end;
  15.   except
  16.     begin
  17.       OutlookApp:= CreateOleObject('Outlook.Application');
  18.     end;
  19.   end;
  20.  
  21.   try
  22.     MailItem:= OutlookApp.CreateItem(olMailItem);
  23.     MailItem.Recipients:= 'my email address';  // or r
  24.     MailItem.Subject:= s;
  25.     MailItem.Body:= b;
  26.     MailItem.Send;
  27.   finally
  28.     //myAttachments:= VarNull;
  29.     OutlookApp:= VarNull;
  30.   end;
  31. end;

procedure is called form another unit:

Code: Pascal  [Select][+][-]
  1. send_email('e-mail adress', 'subject', 'body');

Message Exception of class 'EOleException' occurs, Property is read-only.

If r variable is entered inside the procedure another message occurs: piece of assembler code.

Thank you

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: emailing from application
« Reply #1 on: October 02, 2018, 08:14:39 pm »
Code: Pascal  [Select][+][-]
  1.     MailItem:= OutlookApp.CreateItem(olMailItem);
  2.     MailItem.Recipients:= 'my email address';  // or r

Message Exception of class 'EOleException' occurs, Property is read-only.

Indeed, the Recipients property is read-only (its documentation even says so).  The Recipients is a collection, you need to call its Add() method to add individual entries to the collection, eg:

Code: Pascal  [Select][+][-]
  1. MailItem.Recipients.Add('my email address');  // or r
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: emailing from application
« Reply #2 on: October 02, 2018, 08:58:50 pm »
You may have added the solution is not a cross platform one, Remy.
Specialize a type, not a var.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: emailing from application
« Reply #3 on: October 02, 2018, 09:19:44 pm »
You may have added the solution is not a cross platform one, Remy.

Why does it matter?  The OP is using ActiveX/COM, which is a Windows-specific technology, so the code is platform-specific anyway, so the solution will be, too.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #4 on: October 03, 2018, 09:54:29 am »
Thanks Remy,
Code: Pascal  [Select][+][-]
  1. MailItem.Recipients.Add('my email address');

works. There is one issue - when the procedure is called like this:

Code: Pascal  [Select][+][-]
  1. send_email('e-mail adress', 'subject', 'body');

there is runtime error with some assembler code window displayed.

Data from procedure call is OK as checked in ShowMessage:

Code: Pascal  [Select][+][-]
  1. rocedure send_email(r, s, b: String);
  2.  
  3. const
  4.   olMailItem = 0;
  5.   olByValue = 1;
  6.  
  7. var
  8.   OutlookApp, MailItem: OLEVariant;
  9.  
  10. begin
  11.   try
  12.     begin
  13.       OutlookApp:= GetActiveOleObject('Outlook.Application');
  14.     end;
  15.   except
  16.     begin
  17.       OutlookApp:= CreateOleObject('Outlook.Application');
  18.     end;
  19.   end;
  20.  
  21.   try
  22.     MailItem:= OutlookApp.CreateItem(olMailItem);
  23.     ShowMessage(r + s + b);                                     //data OK
  24.     MailItem.Recipients.Add(r);
  25.     MailItem.Subject:= s;
  26.     MailItem.Body:= b;
  27.     MailItem.Send;
  28.   finally
  29.     //myAttachments:= VarNull;
  30.     OutlookApp:= VarNull;
  31.   end;
  32. end;    

No runtime error and successfull e-mail sending occurs if recipient, subject, body strings are hard copied in procedure send_email, therefore data from outside are ignored.

Thank you

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: emailing from application
« Reply #5 on: October 03, 2018, 07:39:34 pm »
there is runtime error with some assembler code window displayed.

I can't answer that, as I don't use FreePascal myself.  You will just have to debug the code for yourself and find out why it is crashing.  However, I will say that COM expects BSTR strings, not Pascal strings, so try casting your strings to WideString (a wrapper for BSTR) when passing them to COM, eg:

Code: Pascal  [Select][+][-]
  1. procedure send_email(r, s, b: String);
  2. const
  3.   olMailItem = 0;
  4.   olByValue = 1;
  5. var
  6.   OutlookApp, MailItem: OLEVariant;
  7. begin
  8.   try
  9.     OutlookApp := GetActiveOleObject('Outlook.Application');
  10.   except
  11.     OutlookApp := CreateOleObject('Outlook.Application');
  12.   end;
  13.  
  14.   try
  15.     MailItem := OutlookApp.CreateItem(olMailItem);
  16.     try
  17.       MailItem.Recipients.Add(WideString(r));
  18.       MailItem.Subject := WideString(s);
  19.       MailItem.Body := WideString(b);
  20.       MailItem.Send;
  21.     finally
  22.       MailItem := VarNull;
  23.     end;
  24.   finally
  25.     OutlookApp := VarNull;
  26.   end;
  27. end;

On a side note, you do not need to explicitly set OLEVariant to null when you are done using it.  It is managed by the compiler and will be cleaned up automatically when it goes out of scope, eg:

Code: Pascal  [Select][+][-]
  1. procedure send_email(r, s, b: String);
  2. const
  3.   olMailItem = 0;
  4.   olByValue = 1;
  5. var
  6.   OutlookApp, MailItem: OLEVariant;
  7. begin
  8.   try
  9.     OutlookApp := GetActiveOleObject('Outlook.Application');
  10.   except
  11.     OutlookApp := CreateOleObject('Outlook.Application');
  12.   end;
  13.  
  14.   MailItem := OutlookApp.CreateItem(olMailItem);
  15.   MailItem.Recipients.Add(WideString(r));
  16.   MailItem.Subject := WideString(s);
  17.   MailItem.Body := WideString(b);
  18.   MailItem.Send;
  19. end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: emailing from application
« Reply #6 on: October 03, 2018, 11:12:05 pm »
Personally I use X-Mailer - https://github.com/MFernstrom/xmailer

Unless you need to use Outlook specifically, I'd recommend that, and it's cross platform afaik.

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #7 on: October 18, 2018, 11:26:27 am »
Hi, I am back with the same topic. e-mailing works, what does not work is how to treat the situation when Outlook is not running.

Code: Pascal  [Select][+][-]
  1. procedure send_email(r, s, b: ShortString);
  2.  
  3. const
  4.   olMailItem = 0;
  5.   olByValue = 1;
  6.  
  7. var
  8.   OutlookApp, MailItem: OLEVariant;
  9.  
  10. begin
  11.   try
  12.       OutlookApp:= GetActiveOleObject('Outlook.Application');
  13.   except
  14.  
  15.       //OutlookApp:= CreateOleObject('Outlook.Application');
  16.       // here I just want to use ShowMessage with information that Outlook is not available
  17.  
  18.   end;
  19.  
  20.   try
  21.     MailItem:= OutlookApp.CreateItem(olMailItem);
  22.     MailItem.Recipients.Add(WideString(r));
  23.     MailItem.Subject:= WideString(s);
  24.     MailItem.Body:= WideString(b);
  25.     MailItem.Send;
  26.   finally
  27.     OutlookApp:= VarNull;
  28.   end;
  29. end;

Runtime error is generated instead of code execution in except section.

Thanks

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: emailing from application
« Reply #8 on: October 18, 2018, 07:03:23 pm »
Personally I use X-Mailer - https://github.com/MFernstrom/xmailer

Unless you need to use Outlook specifically, I'd recommend that, and it's cross platform afaik.
Thanks for that link.  X-Mailer is simple to configure and works really well (tested with Gmail account)
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #9 on: October 19, 2018, 08:22:32 am »
Unfortunately I need to use Outlook specifically since this is company choice.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: emailing from application
« Reply #10 on: October 22, 2018, 11:31:06 pm »
Runtime error is generated instead of code execution in except section.

What kind of runtime error exactly? GetActiveOleObject() should be raising an exception that the 'except' can catch.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #11 on: October 24, 2018, 12:42:20 pm »
Hi, the message is Exception of class EOleSySError, Operation unavailable. At the same time appears window for loggin to Outlook.

Thank you

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: emailing from application
« Reply #12 on: October 24, 2018, 12:53:38 pm »
Yeah, right, of course you or your application needs to be logged in to Outlook.... You can also do that is code.
Specialize a type, not a var.

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #13 on: October 24, 2018, 01:07:04 pm »
When I am logged in Outlook it is OK. I want to prevent crashing application when user forgets log to Outlook just give him info to do it instead.

draggon

  • New Member
  • *
  • Posts: 41
Re: emailing from application
« Reply #14 on: October 24, 2018, 01:15:57 pm »
Thanks for support, I found on another forum that this happens only when running from IDE. Running compiled application is fine.

 

TinyPortal © 2005-2018