We help IT Professionals succeed at work.

send email with outlook (from delphi-application). Send-Window does not open.

formi
formi asked
on
Hi

using Delphi 10.1 and outlook 365.
There are only some clients where I have the problem: I can not send EMail from my application. On my computer and most other clients-computers it works. But there are computers, where only outlook is startet but no send-window is opend. Retring a second time, outlook is startet also a second time...
  OutlookApplication1.Connect;
  try
    MailItem1 := OutlookApplication1.CreateItem(olMailItem)  as _MailItem;
    while toAdress <> '' do
    begin
      if Pos(';',toAdress) > 0 then
      begin
        MailItem1.Recipients.Add(Copy(toAdress,1,Pos(';',toAdress)-1));
        Delete(toAdress,1,Pos(';',toAdress));
      end
      else
      begin
        MailItem1.Recipients.Add(toAdress);
        toAdress := '';
      end;
    end;
    MailItem1.CC := ToCC;
    MailItem1.BCC := ToBCC;
    MailItem1.Subject := Subject;
    if EMailOutlookAbsender = '' then  //local definitions?
    begin
      if Pos('@',getOptionMandantS('EMail_Absender','')) > 0 then
        MailItem1.SendUsingAccount := OutlookApplication1.Session.Accounts.Item(getOptionMandantS('EMail_Absender',''));
    end
    else MailItem1.SendUsingAccount := OutlookApplication1.Session.Accounts.Item(EMailOutlookAbsender); //globale Einstellung
    Mailitem1.BodyFormat := olFormatHTML;
    //the default signature is added
    ObjectInspector := Mailitem1.getinspector;

    if ObjectInspector.EditorType = olEditorWord then
    begin
      WordEditor := ObjectInspector.WordEditor;
      WordEditor.Characters.item(1).InsertBefore(Htmltext);
    end
    else
    begin
      Mailitem1.HtmlBody := StringReplace(Mailitem1.HtmlBody,'<body>','<body>'+Htmltext,[rfReplaceAll,rfIgnoreCase]);
    end;
    if AttachedFileNames <> nil then
    begin
      for i := 0 to AttachedFileNames.Count - 1 do
        MailItem1.Attachments.Add(AttachedFileNames[i], EmptyParam, EmptyParam, EmptyParam);
    end;
    MailItem1.Display(true);

Open in new window

Any idea where the problem is?
Thanks, Peter
Comment
Watch Question

Lukasz Zielinskisoftware developer
Top Expert 2008

Commented:
Well if it works on some PCs but doesn't on others there are two options:
- it does not depend on your code but Outlook version and/or settings
- your code fails somewhere between OutlookApplication1.Connect; and MailItem1.Display(true);

Just looking at your code there is nothing suspicious, but the problem might be here:
OutlookApplication1.Session.Accounts.Item() <- maybe there are no items?
or maybe there is a problem with using word editor.

But it's impossible to tell just by looking at your code.
So if there is PC on which you can reproduce it every time - just make special build or little test app that would out put debug info to make sure your code reaches MailItem1.Display(true); point.

To save time I would suggest putting debug info after each line of your code

ziolko.

Author

Commented:
Now I displayed the errors and tested with Office home and Business 2016

1. (translated from german): "no pointer can be assigned to a executed object that is registered with OLE outlook.application.16" . If I search in the registry I find such an entry (see images). This occurs if outlook is not execueted. If outlook runs I get the error "Interface not supported"
2. in the taskmanager, the process outlook.exe is visible but when I want to check if outlook is running with "GetProcessList" this process isn't in the list.
Error-MessageRegistry
Lukasz Zielinskisoftware developer
Top Expert 2008

Commented:
do you know which line of your code generates this error?

Author

Commented:
it's this one: OutlookApplication1.Connect
software developer
Top Expert 2008
Commented:
it means that OLE Automation Server was not initialized correctly.
first thing to check:
can you start outlook on this PC from your start menu?  what's important it has to be the same windows account that starts your app.

if the answer is "yes" then try changing component you use.
I guess you use default TOutlookApplication but check what's in your "uses" is it Outlook2000, Outlook2010 or OutlookXP.

you can go to Delphi -> Component -> Install packages and switch between those 3 versions.

If this fails also - then only other thing that comes to my mind is to skip TOutlookApplication and use so called "late binding" and OleVariant types.

I will be away until at least Sunday, if do not succeed by then - just post here and I will try to prepare sample app using "late binding"

ziolko.

Author

Commented:
Hi Lukasz, thanks a lot for your help. Late binding is the solution. The only thing is that outlook has to be executed and since I can not detect the process  with "GetProcessList". But this is another subject and if I don't find the solution I'll start another topic.