MAPISendMail Hangs

M

Marius Groenendijk

Hello Group!

MAPISendMail hangs forever.

It only works if in the VS.NET debugger I hit a breakpoint
anywhere *after* MAPILogon and *before* MAPISendMail.
Without breakpoint the app hangs forever on MAPISendMail.

Funny thing is, if the app starts a minimised Notepad just before
MAPISendMail, it (MAPI) will continue after which the app kills
the Notepad proces. This is a very ugly trick.

This problem occurs in a large MDI app but not in a small test
Windows.Forms app!
(If the MAPI calls are put on a separate thread then MAPILogin fails).

What could be the reason and fix?

Thanx in advance for any pointers,
Marius.

-----------------------

Some code snippets:

' hwnd = IntPtr.Zero or MDI.Handle: No difference
' flags = MAPI_LOGON_UI+MAPI_NEW_SESSION: No difference

' no problem
dim result as Integer = MAPILogon(hwnd, Nothing, Nothing, MAPI_LOGON_UI, 0,
session)

Dim msg as MapiMessage
msg.Subject = subject
msg.NoteText = body
msg.MessageType = ""
msg.RecipCount = 1

Dim recip As MapiRecip
recip.RecipClass = MAPI_TO
recip.Name = toEmail

size = Marshal.SizeOf(recip.GetType)
ptr = Marshal.AllocHGlobal(size)
Marshal.StructureToPtr(recip, ptr, False)
msg.Recips = ptr

' *BLOCK* if no 'pause'
msg = MAPISendMail(session, hwnd , msg, MAPI_LOGON_UI, 0)

' log off

---

<DllImport("mapi32.dll")> _
Public Shared Function MAPILogon(ByVal hwnd As IntPtr, _
ByVal username As String, _
ByVal password As String, _
ByVal flags As Integer, _
ByVal reserved As Integer, _
ByRef session As IntPtr) As Integer
End Function

<DllImport("mapi32.dll")> _
Public Shared Function MAPILogoff(ByVal session As IntPtr, _
ByVal hwnd As IntPtr, _
ByVal flags As Integer, _
ByVal reserved As Integer) As Integer
End Function

<DllImport("mapi32.dll")> _
Public Shared Function MAPISendMail(ByVal session As IntPtr, _
ByVal hwnd As IntPtr, _
ByRef message As MapiMessage, _
ByVal flags As Integer, _
ByVal reserved As Integer) As Integer
End Function

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Private Structure MapiMessage
Public Reserved As Integer
Public Subject As String
Public NoteText As String
Public MessageType As String
Public DateReceived As String
Public ConversationID As String
Public Flags As Integer
Public originator As IntPtr
Public RecipCount As Integer
Public Recips As IntPtr
Public FileCount As Integer
Public Files As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Private Structure MapiRecipient
Public Reserved As Integer
Public RecipClass As Integer
Public Name As String
Public Address As String
Public EIDSize As Integer
Public EntryID As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Private Structure MapiFile
Public Reserved As Integer
Public Flags As Integer
Public Position As Integer
Public PathName As String
Public FileName As String
Public FileType As IntPtr
End Structure

--------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top