Outlook Interop Email - 2 mailitems issue

0

In my winforms application(VB net) I need to open Outlook app using Interop and open mailitem with some prefilled data (like subject). The AIP(Azure Information Protection) defaults to the previous email classification setting which is not desirable. To resolve this, I open and close a mailitem before displaying the actual email.

Here is the first variant - getting an error The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

Dim oAppt As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
'Fix for the email classification issue
Dim nMail As Microsoft.Office.Interop.Outlook.MailItem = oAppt.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
nMail.Subject = "---"
nMail.Display(False)
nMail.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)    

if nMail IsNot Nothing Then
    System.Runtime.InteropServices.Marshal.ReleaseComObject(nMail)
End If  

Dim newMail = oAppt.CreateItem(OlItemType.olMailItem) <--Fails Here with The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
newMail.Subject = emailSubject
newMail.To = emailTo
newMail.CC = emailCc

...

Another variant...instantiating another Outlook app - fails with InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).

Dim oAppt1 As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
'Fix for the email classification issue
Dim nMail As Microsoft.Office.Interop.Outlook.MailItem = oAppt1.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
nMail.Subject = "---"
nMail.Display(False)
nMail.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard)    

if nMail IsNot Nothing Then
    System.Runtime.InteropServices.Marshal.ReleaseComObject(nMail)
End If  
If oAppt1 IsNot Nothing Then
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oAppt1)
End If

Dim oAppt2 As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application() <-- fails here

Dim newMail = oAppt2.CreateItem(OlItemType.olMailItem)
newMail.Subject = emailSubject
newMail.To = emailTo
newMail.CC = emailCc

....

Another thing to note is that if Outlook App is running, both variants run without any problem.

.net
outlook
rpc
com-interop
asked on Stack Overflow Oct 15, 2020 by val f

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0