private void ShowMail() { var message = new MAPIHelperInterop.MapiMessage(); using (RecipientCollection.InteropRecipientCollection interopRecipients = Recipients.GetInteropRepresentation()) { message.Subject = Subject; message.NoteText = Body; message.Recipients = interopRecipients.Handle; message.RecipientCount = Recipients.Count; // Check if we need to add attachments if (Files.Count > 0) { // Add attachments message.Files = AllocAttachments(out message.FileCount); } // Signal the creating thread (make the remaining code async) _manualResetEvent.Set(); const int MAPI_DIALOG = 0x8; //const int MAPI_LOGON_UI = 0x1; const int SUCCESS_SUCCESS = 0; int error = MAPIHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, MAPI_DIALOG, 0); if (Files.Count > 0) { // Deallocate the files DeallocFiles(message); } // Check for error if (error != SUCCESS_SUCCESS) { LogErrorMapi(error); } } }
private static void DeallocFiles(MAPIHelperInterop.MapiMessage message) { if (message.Files == IntPtr.Zero) { return; } Type fileDescType = typeof(MapiFileDescriptor); int fsize = Marshal.SizeOf(fileDescType); // Get the ptr to the files var runptr = (int)message.Files; // Release each file for (int i = 0; i < message.FileCount; i++) { Marshal.DestroyStructure((IntPtr)runptr, fileDescType); runptr += fsize; } // Release the file Marshal.FreeHGlobal(message.Files); }