Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Multi-threaded SendEmail request returns General Mapi Failure

Tags:

vb.net

mapi

I'm using the MAPI code by Dave Brooks.

I am attempting to programatically send out a Crystal Report that is generated.

When I run through the code without threading everything runs fine. The problem is when I use threading I get the return error "General MAPI failure [2]".

I have never used threading before and understand that there are dangers involved. Can anyone provide any insight into this issue? NOTE: I've removed exception handling to make the code clearer.

Private Sub RunReport()
    SetParameters()
    SaveReportFile()


    Dim operation As New ThreadStart(AddressOf SendEmail)
    Dim theThread As New Thread(operation)
    theThread.Start()
End Sub

Public Sub SendEmail()
   Dim m As MAPI
   m = New MAPI()
   Dim email As String
   For Each email In emailAddress
       m.AddRecipientBCC(email)
   Next email
   m.AddAttachment(@"c:\temp\report.pdf")
   m.SendMailPopup("Requested Report", "")
End Sub
like image 501
Nathan Koop Avatar asked Mar 05 '09 18:03

Nathan Koop


2 Answers

A very late answer, but thought I'd add it anyway as I just encountered this and couldn't find an answer elsewhere.

You need to set your thread's appartment state to STA before it is started using:

theThread.SetApartmentState(ApartmentState.STA);

Note that threads from the threadpool (e.g. used by BackgroundWorker component) are MTA.

like image 189
rnwood Avatar answered Oct 18 '22 15:10

rnwood


I encountered this same error (General MAPI failure [2]) and came across this solution early in my debugging; however, the cause for my error was due to running my application as administrator while outlook was running as my user. I had a hard time finding the cause of my error so hopefully this will help someone on the same search as me.

If the above answer doesn't solve your problem, try running your application without elevated privileges if possible or find a way to call MAPI using user impersonation.

like image 40
patrickbadley Avatar answered Oct 18 '22 13:10

patrickbadley