我正在使用OLE通过Outlook发送电子邮件。我使用的代码是:
procedure SendOutlookMail;
const
olMailItem = 0;
var
OKToUse: boolean;
Outlook: OleVariant;
vMailItem: variant;
begin
OKToUse := false;
try
Outlook := GetActiveOleObject('Outlook.Application');
OKToUse := true;
except
try
Outlook := CreateOleObject('Outlook.Application');
OKToUse := true;
except
on e: exception do begin
ShowMessage(e.Message);
end;
end;
end;
if VarIsType(Outlook, varDispatch) then
ShowMessage('Outlook is varDispatch')
else
ShowMessage('Outlook is ***NOT*** varDispatch');
if OKToUse then begin
vMailItem := Outlook.CreateItem(olMailItem);
vMailItem.Recipients.Add('mike@example.com');
vMailItem.Subject := 'What a wonderful test email';
vMailItem.Body := 'This is a test --> how amazing';
vMailItem.Send;
end;
VarClear(Outlook);
end;这是从几个不同的SO问题中厚颜无耻地划出的-感谢所有人。
我遇到的代码问题是,当Outlook安装在PC上时,它是关闭的。当Outlook打开时,我收到一个消息框,上面写着"Outlook is varDispatch“,并发送和接收了一封邮件。当Outlook关闭时,我收到相同的消息框"Outlook is varDispatch",但随后“应用程序中发生错误”,我的应用程序突然关闭。
所以有两个问题:
1)如何检测Outlook是否正在运行?将OKToUse设置为true似乎不是一种正确的方式。
2)如果Outlook没有运行,我如何启动它,并在发送电子邮件后将其关闭?
我正在使用Delphi 10.1柏林,并尝试连接到Outlook 2007。
发布于 2016-08-09 23:34:29
在调用CreateItem之前添加以下内容
vNS := Outlook.GetNamespace('MAPI');
vNS.Logon;https://stackoverflow.com/questions/38849937
复制相似问题