tags:

views:

469

answers:

1

Hi,

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
+2  A: 

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.

Robert Wood
This works, thanks so much
Nathan Koop