Automatic Email Error with VB.net in Office 2013 Environment

0

I have a vb.net script that sends automatic emails from Outlook. It currently works in the Office 2010 Suite. I upgraded my environments to 2013 and now it is not working. What I'm doing isn't complicated. All I want to do is grab a .pdf and attache it to an email. Then add the recipients and send the email. The place where it crashes is the actual sending of the email. It throws this execption

System.Runtime.InteropServices.COMEception occurred in Microsoft.VisualBasic.dll
Additional information: Operation aborted (Execption from HRESULT: 0x80004004 (E_ABORT))

I'm thinking because I upgraded environments something isn't referenced correctly. But I can't seem to find a fix. Below is my code, any help would be greatly appreciated! Thanks in advanced.

Imports Outlook = Microsoft.Office.Interop.Outlook
Module Module1

   Sub main()
      sendEmail()
   End Sub

   Public Sub sendEmail()
      Dim ol As Object
      Dim mail AS Object
      ol = CreateObject("Outlook.Application")
      mail = ol.CreateItem(0)
      mail.To = "email@example.com"
      mail.Subject = "Subject Line"
      mail.Body = "Body text"
      mail.Attachments.Add("path\to\attachment.pdf")

      mail.Send()                 'crashes on this line
      mail = Nothing
      ol = Nothing
   End Sub
End Module

Also I am running on Windows 7 with the Office 2013 suite. Looking at the App.config my supported Runtime version is v4.0 and the .NetFramework version is v4.5

vb.net
email
visual-studio-2013
outlook
asked on Stack Overflow Dec 2, 2015 by Mike Bellistri • edited Dec 2, 2015 by David Zemens

1 Answer

0

Since your solution is automating Outlook cross-process you first need to ensure that these types of applications are authorized so check your anti-virus state in Outlook's Trust Center:

https://msdn.microsoft.com/en-us/library/office/ff864479.aspx

I would also wrap the CreateObject call in a Try/Catch block to ensure that isn't failing to begin with. Also make sure that you have an Outlook profile created and that your application isn't running under a non-authorized security context like via a Task Scheduler process or in a web application.

answered on Stack Overflow Dec 2, 2015 by Eric Legault

User contributions licensed under CC BY-SA 3.0