Ejemplo n.º 1
0
        /// <summary>
        /// Sends the mail message.
        /// </summary>
        private void _ShowMail(object deleteFilesOnExit)
        {
            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);
                }

                // Signal the creating thread (make the remaining code async)
                _manualResetEvent.Set();

                const int MAPI_DIALOG = 0x8;
                //const int MAPI_LOGON_UI = 0x1;
                int error = MAPIHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, MAPI_DIALOG, 0);

                if (_files.Count > 0)
                {
                    // Deallocate the files
                    _DeallocFiles(message);
                    if ((bool)deleteFilesOnExit)
                    {
                        foreach (string file in _files)
                        {
                            try {
                                if (File.Exists(file))
                                {
                                    LOG.Debug("Deleting file " + file);
                                    File.Delete(file);
                                }
                            } catch (Exception e) {
                                LOG.Error("Can't delete file " + file, e);
                            }
                        }
                    }
                }
                MAPI_CODES errorCode = (MAPI_CODES)Enum.ToObject(typeof(MAPI_CODES), error);

                // Check for error
                if (errorCode != MAPI_CODES.SUCCESS && errorCode != MAPI_CODES.USER_ABORT)
                {
                    _LogErrorMapi(errorCode);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends the mail message.
        /// </summary>
        private void _ShowMail()
        {
            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);
                }

                // Signal the creating thread (make the remaining code async)
                _manualResetEvent.Set();

                const int MAPI_DIALOG = 0x8;
                //const int MAPI_LOGON_UI = 0x1;
                int error = MAPIHelperInterop.MAPISendMail(IntPtr.Zero, IntPtr.Zero, message, MAPI_DIALOG, 0);

                if (_files.Count > 0)
                {
                    // Deallocate the files
                    _DeallocFiles(message);
                }
                MAPI_CODES errorCode = (MAPI_CODES)Enum.ToObject(typeof(MAPI_CODES), error);

                // Check for error
                if (errorCode != MAPI_CODES.SUCCESS && errorCode != MAPI_CODES.USER_ABORT)
                {
                    string errorText = GetMapiError(errorCode);
                    LOG.Error("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 == MAPI_CODES.INVALID_RECIPS)
                    {
                        _recipientCollection = new RecipientCollection();
                        _ShowMail();
                    }
                }
            }
        }