示例#1
0
        /// <summary>
        /// Sends an email described by the given message object.
        /// </summary>
        /// <param name="message">The object describing the email message.</param>
        /// <returns>Returns true if the message was sent, false if the user aborted.</returns>
        public bool SendEmail(EmailMessage message)
        {
            // Translate files & recipients to unmanaged MAPI structures
            using (var files = Unmanaged.CopyOf(GetFiles(message)))
                using (var recips = Unmanaged.CopyOf(GetRecips(message)))
                {
                    // Create a MAPI structure for the entirety of the message
                    var mapiMessage = new MapiMessage
                    {
                        subject    = message.Subject,
                        noteText   = message.BodyText,
                        recips     = recips,
                        recipCount = recips.Length,
                        files      = files,
                        fileCount  = files.Length
                    };

                    // Determine the flags used to send the message
                    var flags = MapiSendMailFlags.None;
                    if (!message.AutoSend)
                    {
                        flags |= MapiSendMailFlags.Dialog;
                    }
                    if (!message.AutoSend || !message.SilentSend)
                    {
                        flags |= MapiSendMailFlags.LogonUI;
                    }

                    // Send the message
                    var returnCode = MAPISendMail(IntPtr.Zero, IntPtr.Zero, mapiMessage, flags, 0);

                    // Process the result
                    if (returnCode == MapiSendMailReturnCode.UserAbort)
                    {
                        return(false);
                    }
                    if (returnCode != MapiSendMailReturnCode.Success)
                    {
                        Log.Error("Error sending email. MAPI error code: {0}", returnCode);
                        errorOutput.DisplayError(MiscResources.EmailError, $"MAPI returned error code: {returnCode}");
                        return(false);
                    }
                    return(true);
                }
        }
示例#2
0
        private static MapiSendMailReturnCode SendMailW(SystemEmailClients.MapiSendMailDelegateW mapiSendMailW, EmailMessage message, MapiSendMailFlags flags)
        {
            using (var files = Unmanaged.CopyOf(GetFilesW(message)))
                using (var recips = Unmanaged.CopyOf(GetRecipsW(message)))
                {
                    // Create a MAPI structure for the entirety of the message
                    var mapiMessage = new MapiMessageW
                    {
                        subject    = message.Subject,
                        noteText   = message.BodyText,
                        recips     = recips,
                        recipCount = recips.Length,
                        files      = files,
                        fileCount  = files.Length
                    };

                    // Send the message
                    return(mapiSendMailW(IntPtr.Zero, IntPtr.Zero, mapiMessage, flags, 0));
                }
        }