/// <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); }
/// <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(); 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)); } } }