I'm having problems sending MAPI Mail from an ASP.NET applcation (in VB)
(which worked fine in traditional asp)
When run on our test server, it returns an COMException (0x80010106):
[Collaboration Data Objects - [UNKNOWN_ERROR(8 0010106)]]] exception from
MAPI.
(from the global.asa when trying to create MAPI session I get a .net
exception "Cannot change thread mode after it is set")
According to a post on the microsoft.publi c.in.csharp
"The 80010106 error usually occurs due to incompatible
threading models. ASP.NET uses an MTA thread pool
to service requests for the web service and looks like
the CDO component is apartment threaded (lives in an STA).
In traditional ASPX pages, you'd use the ASPCOMPAT
attribute set to true in the <%@ Page %> directive of an
ASPX page to make a ASP.NET use an STA thread
(instead of an MTA thread) to access the apartment threaded
COM component."
So I set the ASPCOMPAT attribute of the aspx page that calls this method to
"true".
This now appears to create the object correctly.
But returns a new error.
[Collaboration Data Objects - [MAPI_E_UNCONFIG URED(8004011C)]]
I think this may be due to the identity of the STA MAPI Component, being
different from the impersonated ASP.NET user in the Web.config file, but i'm
not sure.
Does anyone know....
a) How I can confirm this is the case?
b) Ensure that the MAPI component is running under the same identity?
I have included below the original legacy code from the traditional asp
application and the new COM Interop code from the ASP.NET version of my
application.
Any suggestions are welcome! particularly code examples ;-)
thank you very much.
Chris V.
'--------------------------------original mail.inc used in asp page
(works).----------------------------------------
Dim oSession
Set oSession = CreateObject("M API.Session")
oSession.Logon "", "", False, True, 0, True,
Application("Ex changeServerLoc ation") & vblf &
Application("Ex changeServerMai lbox")
Dim oOutbox
Set oOutbox = oSession.Outbox
Dim oMessage
Set oMessage = oOutbox.Message s.Add
oMessage.Subjec t = Subject
oMessage.Text = Body
Dim oRecipient
For i = 0 To oVec.Count - 1
Set oRecipient = oMessage.Recipi ents.Add
oRecipient.Name = oVec(i)
oRecipient.Reso lve
Set oRecipient = Nothing
Next
oMessage.Send
oSession.Logoff
Set oMessage = Nothing
Set oOutbox = Nothing
Set oSession = Nothing
'-----------------MAPIMESSAGE.VB (called from test.aspx with aspcompat set
to true.)-----------------------
Imports Microsoft.Offic e.Interop
Imports MAPI
Public Class MAPIMail
Implements IQPWMessageSend er
Private mExchangeServer As String
Private mMailbox As String
Public Sub New(ByVal MAPIExchangeSer ver As String, ByVal MAPIMailbox As
String)
mExchangeServer = MAPIExchangeSer ver
mMailbox = MAPIMailbox
End Sub
Public Function SendMail(ByVal CommaDelimitedR ecipients As String, _
ByVal Subject As String, ByVal Body As String) As Boolean Implements
IQPWMessageSend er.SendMail
Dim recipients As String()
Dim oSession As New MAPI.Session
oSession.Logon( "", "", False, True, 0, True, mExchangeServer & vbLf
& mMailbox)
Dim oOutbox As Object
oOutbox = oSession.Outbox
Dim oMessage As MAPI.Message
oMessage = oOutbox.Message s.Add
oMessage.Subjec t = Subject
oMessage.Text = Body
Dim oRecipient As Object
recipients =
QPWDataHelper.C SVToStringArray (CommaDelimited Recipients)
recipients = QPWMessage.Remo veDups(recipien ts)
Dim recip As String
For Each recip In recipients
If Trim(recip) <> "" Then
oRecipient = oMessage.Recipi ents.Add
oRecipient.Name = Trim(recip.ToSt ring)
oRecipient.Reso lve()
oRecipient = Nothing
End If
Next
If recipients.Leng th > 0 Then
Try
oMessage.Send()
Catch ex As Exception
QPWLog.WriteToL og("Mapi error" & ex.Message)
End Try
Else
QPWLog.WriteToL og("Did not send MAPI message as there were no
recipients.")
End If
oSession.Logoff ()
oMessage = Nothing
oOutbox = Nothing
oSession = Nothing
End Function
End Class
'--------------------------------------