gpt4 book ai didi

delphi - Outlook 对象模型 - 检测电子邮件是否已发送

转载 作者:行者123 更新时间:2023-12-03 15:55:17 25 4
gpt4 key购买 nike

我的测试 Delphi 2006 BDS 应用程序中有以下代码:

procedure TForm1.Button1Click(Sender: TObject);
const
olMailItem = 0;
var
Outlook: OleVariant;
vMailItem: variant;
begin
Outlook := CreateOleObject('Outlook.Application');
vMailItem := Outlook.CreateItem(olMailItem);

try
vMailItem.Recipients.add('anemailaddress@gmail.com');
vMailItem.Display(True); -- outlook mail message is displayed modally
except
end;

VarClear(Outlook);
end;

我需要能够检测用户是否从 Outlook 屏幕内发送电子邮件。我尝试了以下代码:

if vMailItem.Sent then
...

但收到错误消息“该项目已被移动或删除”。我认为这是因为邮件已移至已发送邮件文件夹。检测用户是否发送了电子邮件的最佳方法是什么?另外,如果用户确实发送了电子邮件,那么我还需要能够查看电子邮件正文。

提前致谢。

最佳答案

看来你必须使用 Send Event邮件项目的。如果您使用早期绑定(bind),这会容易得多,将“outlook[*].pas”文件之一放入“uses”子句中 RAD studio 的“..\OCX\Servers”文件夹中,然后:

uses
..., outlook2000;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
OutlookApplication: TOutlookApplication;
procedure OnMailSend(Sender: TObject; var Cancel: WordBool);
public
end;

[...]

procedure TForm1.FormCreate(Sender: TObject);
begin
OutlookApplication := TOutlookApplication.Create(Self);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
MailItem: _MailItem;
Mail: TMailItem;
begin
MailItem := OutlookApplication.CreateItem(olMailItem) as _MailItem;

Mail := TMailItem.Create(nil);
try
Mail.ConnectTo(MailItem);
Mail.OnSend := OnMailSend;

MailItem.Recipients.Add('username@example.com');
MailItem.Display(True);
finally
Mail.Free;
end;
end;

procedure TForm1.OnMailSend(Sender: TObject; var Cancel: WordBool);
begin
ShowMessage((Sender as TMailItem).Body);
end;
 


使用后期绑定(bind),您必须执行导入包装器所做的一些工作。最简单的例子可能是这样的:

 
type
TForm1 = class(TForm, IDispatch)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FCookie: Integer;
FMailItem: OleVariant;
procedure MailSent;
protected
function QueryInterface(const IID: TGUID; out Obj): HResult; override;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
stdcall;
public
end;

[...]

uses
comobj;

const
DIID_ItemEvents: TGUID = '{0006303A-0000-0000-C000-000000000046}';
SendItemDispID = 61445;

function TForm1.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if IsEqualIID(IID, DIID_ItemEvents) and GetInterface(IDispatch, Obj) then
Result := S_OK
else
Result := inherited QueryInterface(IID, Obj);
end;

function TForm1.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
begin
Result := S_OK;
if DispID = SendItemDispID then
MailSent;
end;


procedure TForm1.Button1Click(Sender: TObject);
const
olMailItem = 0;
var
Outlook: OleVariant;
CPContainer: IConnectionPointContainer;
ConnectionPoint: IConnectionPoint;
begin
Outlook := CreateOleObject('Outlook.Application');
FMailItem := Outlook.CreateItem(olMailItem);
FMailItem.Recipients.add('username@example.com');

if Supports(FMailItem, IConnectionPointContainer, CPContainer) then begin
CPContainer.FindConnectionPoint(DIID_ItemEvents, ConnectionPoint);
if Assigned(ConnectionPoint) then
ConnectionPoint.Advise(Self, FCookie);
CPContainer := nil;
end;

FMailItem.Display(True);

if Assigned(ConnectionPoint) then begin
ConnectionPoint.Unadvise(FCookie);
ConnectionPoint := nil;
end;

VarClear(FMailItem);
VarClear(Outlook);
end;

procedure TForm1.MailSent;
begin
ShowMessage(FMailItem.Body);
end;

关于delphi - Outlook 对象模型 - 检测电子邮件是否已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5507342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com