I have an application which integrates with Outlook, just to send an email with attachments (VB.NET .net v4).
Because I need it to run with multiple versions of Outlook, I am using late binding.
On my local system, and another deployment system, everything works fine (Outlook 2016).
On 3 particular systems, I can't create the "Outlook.Application" object, because an exception is thrown and, unfortunately, the error tells me nothing. This just happens if Outlook is running. (All systems running win 10)
I am running the application from a network share, however I already tried to run locally from c: drive and documents directory. Also tried running as administrator.
Another thing I have tried, was to check if Outlook is running to get the object, instead of creating, but the error remains if Outlook is running.
This is what I am using:
Public Class Email
Public Shared Sub EnviaEmail(ByVal pEnderecoDestino() As String, ByVal pAssunto As String, ByVal pBody As String, Optional ByVal pEnderecoCC() As String = Nothing, Optional ByVal pEnderecoBCC() As String = Nothing, Optional ByVal pAnexos() As String = Nothing)
Dim objOutlook As Object
Dim objMensagem As Object
Dim objEnderecos As Object
Dim objEndereco As Object
Dim objAnexos As Object
Try
Dim fechaApp As Boolean = False
If Process.GetProcessesByName("OUTLOOK") Is Nothing Then fechaApp = True
objOutlook = CreateObject("Outlook.Application")
objMensagem = objOutlook.CreateItem(0)
With objMensagem
.Subject = pAssunto
.BodyFormat = 1
.Body = pBody
End With
objEnderecos = objMensagem.Recipients
For i As Integer = 0 To pEnderecoDestino.Length - 1 Step 1
If Not String.IsNullOrEmpty(pEnderecoDestino(i)) Then
objEndereco = objEnderecos.Add(pEnderecoDestino(i).Trim())
objEndereco.Type = 1
objEndereco.Resolve()
End If
Next i
If Not pEnderecoBCC Is Nothing Then
For i As Integer = 0 To pEnderecoBCC.Length - 1 Step 1
If Not String.IsNullOrEmpty(pEnderecoBCC(i)) Then
objEndereco = objEnderecos.Add(pEnderecoBCC(i).Trim())
objEndereco.Type = 3
objEndereco.Resolve()
End If
Next i
End If
If pEnderecoCC IsNot Nothing Then
For i As Integer = 0 To pEnderecoCC.Length - 1 Step 1
If Not String.IsNullOrEmpty(pEnderecoCC(i)) Then
objEndereco = objEnderecos.Add(pEnderecoCC(i).Trim())
objEndereco.Type = 2
objEndereco.Resolve()
End If
Next i
End If
objAnexos = objMensagem.Attachments
If pAnexos IsNot Nothing Then
For i As Integer = 0 To pAnexos.Length - 1 Step 1
objAnexos.Add(pAnexos(i))
Next i
End If
objMensagem.Send()
If fechaApp Then objOutlook.Quit()
Catch ex As COMException
Select Case ex.ErrorCode
Case -2147467260
Throw New Exception("Permission denied to access MS Outlook", ex.InnerException)
Case -2147221164
Throw New Exception("MS Outlook not installed" , ex.InnerException)
Case Else
Throw ex
End Select
Finally
objAnexos = Nothing
objEndereco = Nothing
objEnderecos = Nothing
objMensagem = Nothing
objOutlook = Nothing
End Try
End Sub
End Class
The fact you only see the error if Outlook is already running most likely means Outlook and your app are running in different security contexts. Outlook is a singleton, so it attempts to connect to the already running instance. COM system however will not marshal calls between processes with different security contexts.