/// <summary> /// Sends the mail message. /// </summary> private void ShowMail() { NativeMethods.MapiMessage message = new NativeMethods.MapiMessage(); using (RecipientCollection.InteropRecipientCollection interopRecipients = _recipientCollection.GetInteropRepresentation()) { message.Subject = _subject; message.NoteText = _body; message.Recipients = interopRecipients.Handle; message.RecipientCount = _recipientCollection.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 = (int)NativeMethods.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) { throw new Exception(LogErrorMapi(error)); } } }
/// <summary> /// Deallocates the files in a message. /// </summary> /// <param name="message">The message to deallocate the files from.</param> private static void DeallocFiles(NativeMethods.MapiMessage message) { if (message.Files == IntPtr.Zero) { return; } Type fileDescType = typeof(NativeMethods.MapiFileDescriptor); int fsize = Marshal.SizeOf(fileDescType); // Get the ptr to the files int 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); }