在AX中有两种发送邮件的方式,一种是使用类SysMailer通过CDO.Message组件发送,另一种是通过SysINetMail这个类使用AX封装的MAPI这个类去发送邮件,其中后者实际上是调用Outlook的COM组件实现的,当然会把已经发送的邮件保存在客户端的Outlook中,本文主要讨论使用后者所遇到的问题.
我们在使用批处理发送邮件的功能时会出现一个对话框让点击确定,批处理发送邮件的功能是通过BatchRun的sendMail方法实现的,简化一下其代码,核心代码如下:
static void SendMail(Args _args)
{
SysINetMail mail = new SysINetMail();
;
mail.sendMail("wanglai@126.com","Hello Outlook Mail","SendMail By Outlook Test",false);
}
{
SysINetMail mail = new SysINetMail();
;
mail.sendMail("wanglai@126.com","Hello Outlook Mail","SendMail By Outlook Test",false);
}
可以看出是通过SysINetMail去实现的,sendMail会调用一系列的方法,当调用到SysINetMail的
receiver = _mapi.resolveName(name, #MAPI_DIALOG);
这一行时,会出现如下对话框:
这样每次都要点这个玩意确实比较痛苦.问题的原因是AX的MAPI这个类被Outlook认为是一个不安全.其实AX中已经有解决这个问题的办法了,只要使用MAPIEX这个类就可以了,搞不清楚微软为什么不改造一下SysINetMail这个类使用MAPIEX,或许觉得让全世界人民都点这个Allow按钮比较酷吧,呵呵.
可以看一下类SmmOutlookEMail的sendEMail方法,简化一下代码如下:
static void ExtendedMAPISendMail(Args _args)
{
MapiEx mapiEx;
MapiExMail mapiExMail;
boolean mapiInitialised;
COM outlookNameSpace;
COM outlook;
COM folder;
COM item;
str entryId;
str storeId;
InteropPermission permission;
#define.outlookapplication("outlook.application")
#define.mapi("mapi")
#smmMSOutlook2002ObjectModelConstants;
permission = new InteropPermission(InteropKind::ComInterop);
permission.assert();
outlook = new COM (#outlookapplication);
outlookNameSpace = outlook.getNameSpace(#mapi);
outlookNameSpace.logon();
folder =outlookNameSpace.getDefaultFolder(#olFolderInbox);
storeId = folder.storeId();
item = outlook.createItem(#olMailItem);
if(item)
{
item.subject("Hello Outlook");
item.body("Send Mail By Outlook");
item.saveSentMessageFolder(outlookNameSpace.getDefaultFolder(#olFolderSentMail));
item.save();
entryId = item.entryId();
mapiEx = new MapiEx();
if(mapiEx && mapiEx.mapiInitialised())
{
mapiInitialised = true;
if (!mapiEx.logon("","",0) || !mapiEx.openMessageStore(storeId))
{
mapiInitialised = false;
mapiEx.logout();
mapiEx.finalize();
return;
}
mapiExMail =mapiEx.getMailFromEntryId(entryId);
if (mapiExMail)
{
mapiExMail.addRecipient('wanglai@126.com',"",#olTo);
}
mapiExMail.send();
mapiExMail.close();
}
}
if (mapiInitialised)
{
mapiEx.logout();
mapiEx.finalize();
}
}
这样就不会弹出那个框了.
{
MapiEx mapiEx;
MapiExMail mapiExMail;
boolean mapiInitialised;
COM outlookNameSpace;
COM outlook;
COM folder;
COM item;
str entryId;
str storeId;
InteropPermission permission;
#define.outlookapplication("outlook.application")
#define.mapi("mapi")
#smmMSOutlook2002ObjectModelConstants;
permission = new InteropPermission(InteropKind::ComInterop);
permission.assert();
outlook = new COM (#outlookapplication);
outlookNameSpace = outlook.getNameSpace(#mapi);
outlookNameSpace.logon();
folder =outlookNameSpace.getDefaultFolder(#olFolderInbox);
storeId = folder.storeId();
item = outlook.createItem(#olMailItem);
if(item)
{
item.subject("Hello Outlook");
item.body("Send Mail By Outlook");
item.saveSentMessageFolder(outlookNameSpace.getDefaultFolder(#olFolderSentMail));
item.save();
entryId = item.entryId();
mapiEx = new MapiEx();
if(mapiEx && mapiEx.mapiInitialised())
{
mapiInitialised = true;
if (!mapiEx.logon("","",0) || !mapiEx.openMessageStore(storeId))
{
mapiInitialised = false;
mapiEx.logout();
mapiEx.finalize();
return;
}
mapiExMail =mapiEx.getMailFromEntryId(entryId);
if (mapiExMail)
{
mapiExMail.addRecipient('wanglai@126.com',"",#olTo);
}
mapiExMail.send();
mapiExMail.close();
}
}
if (mapiInitialised)
{
mapiEx.logout();
mapiEx.finalize();
}
}