private void _ShowMail(object ignore) { _logger.Info("Showing mail client"); var message = new MapiHelperInterop.MapiMessage(); using (RecipientCollection.InteropRecipientCollection interopRecipients = _recipientCollection.GetInteropRepresentation()) { _logger.Info("Subject: " + _subject); message.Subject = _subject; message.NoteText = _body; message.Recipients = interopRecipients.Handle; message.RecipientCount = _recipientCollection.Count; _logger.Info("Adding {0} files ", _files.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; _logger.Info("Starting MAPI call"); try { int error = MapiHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, MAPI_DIALOG, 0); _logger.Info("MAPI result was: " + error); if (_files.Count > 0) { // Deallocate the files _DeallocFiles(message); } _LogErrorMapi(error); } catch (InvalidOperationException ex) { _logger.Error(ex); throw new MapiException("The MAPI call could not be completed due an InvalidOperationException.", ex); } catch (Exception ex) { _logger.Error(ex); } } _logger.Info("Done with MAPI"); }
/// <summary> /// Sends the mail message. /// </summary> private void _ShowMail() { var message = new MapiHelperInterop.MapiMessage(); using (var 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; var error = MapiHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, MAPI_DIALOG, 0); if (Files.Count > 0) { // Deallocate the files _DeallocFiles(message); } var errorCode = (MapiCodes)Enum.ToObject(typeof(MapiCodes), error); // Check for error if (errorCode == MapiCodes.SUCCESS || errorCode == MapiCodes.USER_ABORT) { return; } var errorText = GetMapiError(errorCode); Log.Error().WriteLine(null, "Error sending MAPI Email. Error: " + errorText + " (code = " + errorCode + ")."); MessageBox.Show(errorText, "Mail (MAPI) destination", MessageBoxButtons.OK, MessageBoxIcon.Error); // Recover from bad settings, show again if (errorCode != MapiCodes.INVALID_RECIPS) { return; } Recipients = new RecipientCollection(); _ShowMail(); } }
/// <summary> /// Sends the mail message. /// </summary> private void _ShowMail(bool sync) { MapiHelperInterop.MapiMessage message = new MapiHelperInterop.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); } if (!sync) { // Signal the creating thread (make the remaining code async) _manualResetEvent.Set(); } const int mapiDialog = 0x8; //const int MAPI_LOGON_UI = 0x1; const int successSuccess = 0; int error = MapiHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, mapiDialog, 0); if (_files.Count > 0) { // Deallocate the files _DeallocFiles(message); } // Check for error if (error != successSuccess) { _LogErrorMapi(error); } } }
/// <summary> /// Deallocates the files in a message. /// </summary> /// <param name="message">The message to deallocate the files from.</param> private void _DeallocFiles(MapiHelperInterop.MapiMessage message) { if (message.Files != IntPtr.Zero) { Type fileDescType = typeof(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); } }