3
votes

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.

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.

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

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

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

1
If SMTP failed, what gives you the idea that Outlook won't? - Athena
Don't use Outlook. That's such a hack. Then Outlook needs to be installed wherever your app runs. Use SMTP and tell them to configure the relays in Exchange. It is what it is (and it's not too tricky, to be honest). - rory.ap
@Ares -- Because SMTP requires explicit relays to be "allowed" in Exchange due to security concerns to prevent rogue software from bouncing mail off your server. Outlook is different; it's designed to work with Exchange in a AD domain setting, and it doesn't use SMTP. - rory.ap
If it is designed to use Outlook, it will be susceptible to breaking any time an Office or Windows update is installed. Use the SMTP method if at all possible. - Tony Hinkle
@Ares, because I witnessed Outlook work. - Jerry Lewis

1 Answers

2
votes

You can try this:

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

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).