如何使 Outlook 自动化? [英] How to automate Outlook?

查看:83
本文介绍了如何使 Outlook 自动化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我们的 ERP 系统发送电子邮件.我尝试使用 SMTP,但它仅适用于内部邮件,而无法用于抱怨无法中继或其他内容的外部邮件.我认为经理要么不想,要么不知道如何正确配置 Exchange.

I'm trying to send e-mail from our ERP system. I tried using SMTP but it only works for internal mail and fails for external mail complaining about unable to relay or something. I think the manager either doesn't want to or know how to configure Exchange properly.

所以我的老板让我使用 Outlook.问题是我的代码在调试中工作正常,但如果 Outlook 处于打开状态,则几乎在所有情况下都会失败.我确实让它在我修改供应商安装时起作用,但我们不希望这样做.我们正在使用直观的 ERP 8.5.它将其库文件存储在标准文件夹中,并且有一个用于任何自定义代码或继承的供应商对象的自定义文件夹.

So my boss told me to use Outlook. The problem is my code works fine in debug but fails if Outlook is open, which it will be in almost every case. I did get it to work my modifying the vendors installation, but we would prefer not to do that. We are using Intuitive ERP 8.5. It stores its library files in the standard folder and there is a custom folder for any custom code or inherited vendor objects.

程序文件\IntuitiveERP.exe程序文件\IntuitiveERP\Custom程序文件\IntuitiveERP\Standard

Program Files\IntuitiveERP.exe Program Files\IntuitiveERP\Custom Program Files\IntuitiveERP\Standard

如果我将程序目录放在 C: 的根目录下,并结合标准和自定义文件夹,则无论 Outlook 是打开还是关闭,代码都可以工作.我们不希望修改供应商的安装,因为这可能会导致更新出现问题.

If I put the program directory on the root of C: and combine the standard and custom folders the code works whether Outlook is open or closed. We would prefer not to modify the vendor's installation because if may cause problems with updates.

'Fails with Cannot create ActiveX component.
objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application)
'Fails with Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.
objOutlook = New Outlook.Application
mobjEmail = CType(objOutlook.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)

With mobjEmail
    .CC = strEmployeeEmail
    .Subject = String.Format(Constants.RFQ.Email.Subject, strRFQID)
    .To = strTo
    .Body = Constants.RFQ.Email.Body
    .Attachments.Add(String.Format(Constants.RFQ.Output.FullPath, strRFQID))
    .Display(True)
End With

知道如何在打开时获得对 Outlook 的引用吗?任何替代解决方案?

Any idea how to get a reference to Outlook when its open? Any alternative solutions?

推荐答案

你可以试试这个:

Try
    objOutlook = Marshal.GetActiveObject("Outlook.Application")
Catch ex As Exception
    objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application)
End Try

请注意,以管理员身份在 Visual Studio 中运行并在 Outlook 已在用户模式下运行时访问 Outlook 时会出现问题.请参阅.尝试直接从 bin 文件夹中运行 EXE(不要以管理员身份运行).

Note that there are issues when running inside Visual Studio as Administrator and accessing Outlook when it is already running in user mode. See this. Try just running the EXE directly from the bin folder (don't run as administrator).

这篇关于如何使 Outlook 自动化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆