-->

How is working with Outlook in Delphi different th

2019-04-16 04:29发布

问题:

I create a mapi message in my Delphi app, and users then simply send the message in their default mapi email client, i.e. the formatted message appears in their mail client and they click "send."

Everything works great when the email client is Thunderbird or Outlook Express, but things are stranger when it's Outlook (2007). The focus goes to Outlook, for example, but a user can't close the Outlook window, sometimes the user can't even use a mouse within the program--the arrow disappears within Outlook. I find myself having to close the app from Task Manager.

From my newbie perspective, the issue is one of controlling forms and focus more than something connected to simple or extended mapi; the latter seems irrelevant in this case.

Does anyone know what's going on here? And how I should change my code to deal with the issue?

This is the code:

MapiMail1 := TMapiMail.Create(self);
try
  MapiMail1.Recipients.Add(MainGrid.AllCells[aCol, aRow]);
  MapiMail1.Subject := '';
  MapiMail1.Body := '';
  MapiMail1.EditDialog := True;
  MapiMail1.Send;
finally
  MapiMail1.Free;
end;

回答1:

Outlook works great using OLE rather than MAPI. Try this:

USES OleCtrls, ComObj;

procedure TForm1.Button1Click(Sender: TObject);
const
  olMailItem = 0;
var
  Outlook: OLEVariant;
  MailItem: Variant;
  MailInspector : Variant;
  stringlist : TStringList;
begin
  try
   Outlook:=GetActiveOleObject('Outlook.Application') ;
  except
   Outlook:=CreateOleObject('Outlook.Application') ;
  end;
  try
    Stringlist := TStringList.Create;
    MailItem := Outlook.CreateItem(olMailItem) ;
    MailItem.Subject := 'subject here';
    MailItem.Recipients.Add('someone@yahoo.com');
    MailItem.Attachments.Add('c:\boot.ini');
    Stringlist := TStringList.Create;
    StringList.Add('body here');
    MailItem.Body := StringList.text;
    MailInspector := MailItem.GetInspector;
    MailInspector.display(true); //true means modal
  finally
    Outlook := Unassigned;
    StringList.Free;
  end;
end;