3

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?

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • If SMTP failed, what gives you the idea that Outlook won't? – Athena Jul 15 '16 at 20:23
  • 1
    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 Jul 15 '16 at 20:23
  • 1
    @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 Jul 15 '16 at 20:24
  • 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 Jul 15 '16 at 20:53
  • @Ares, because I witnessed Outlook work. – Jerry Lewis Jul 19 '16 at 13:31
  • @roryap, actually, using SMTP is a hack. I have to create my own send mail form (re-invent the wheel) and provide very limited functionality as opposed to what Outlook provides. Plus everyone is used to Outlook, I created my form to look as much like outlook as possible, it is however, not the same. – Jerry Lewis Jul 19 '16 at 13:34
  • SMTP is all fantastic except when the boss wants each user's signature to go out with each mail, or users want a record of sent mails in their 'sent items'. And a host of other reasons why you might want to pump messages through Outlook. Like the fact that Exchange online has a cap on the submission rate via SMTP, where if you send via Outlook queuing is done for you. There is no hard and fast rule that says SMTP is better in all cases. – Alan B Jun 23 '17 at 10:59

1 Answers1

2

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

Community
  • 1
  • 1
Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
  • Thanks, you pointed to one of the problems I am having. The ERP software needs to be run as administrator. Getting a reference to Outlook (running in user mode) fails. This is why it works if Outlook is closed first. This may help me to get my boss to configure out Exchange properly. – Jerry Lewis Jul 19 '16 at 15:38