예제 #1
0
파일: MailHelper.cs 프로젝트: zhk/greenshot
            /// <summary>
            /// Default constructor for creating InteropRecipientCollection.
            /// </summary>
            /// <param name="outer"></param>
            public InteropRecipientCollection(RecipientCollection outer)
            {
                _count = outer.Count;

                if (_count == 0)
                {
                    Handle = IntPtr.Zero;
                    return;
                }

                // allocate enough memory to hold all recipients
                int size = Marshal.SizeOf(typeof(MapiMailMessage.MapiHelperInterop.MapiRecipDesc));

                Handle = Marshal.AllocHGlobal(_count * size);

                // place all interop recipients into the memory just allocated
                IntPtr ptr = Handle;

                foreach (Recipient native in outer)
                {
                    MapiMailMessage.MapiHelperInterop.MapiRecipDesc interop = native.GetInteropRepresentation();

                    // stick it in the memory block
                    Marshal.StructureToPtr(interop, ptr, false);
                    ptr = new IntPtr(ptr.ToInt64() + size);
                }
            }
예제 #2
0
        /// <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();
            }
        }
예제 #3
0
파일: MailHelper.cs 프로젝트: zhk/greenshot
 /// <summary>
 /// Creates a blank mail message.
 /// </summary>
 public MapiMailMessage()
 {
     Files             = new List <string>();
     Recipients        = new RecipientCollection();
     _manualResetEvent = new ManualResetEvent(false);
 }
예제 #4
0
 /// <summary>
 /// Creates a blank mail message.
 /// </summary>
 public MapiMailMessage()
 {
     _files = new List<string>();
     _recipientCollection = new RecipientCollection();
     _manualResetEvent = new ManualResetEvent(false);
 }
예제 #5
0
            /// <summary>
            /// Default constructor for creating InteropRecipientCollection.
            /// </summary>
            /// <param name="outer"></param>
            public InteropRecipientCollection(RecipientCollection outer)
            {
                _count = outer.Count;

                if (_count == 0)
                {
                    _handle = IntPtr.Zero;
                    return;
                }

                // allocate enough memory to hold all recipients
                int size = Marshal.SizeOf(typeof(MapiMailMessage.MAPIHelperInterop.MapiRecipDesc));
                _handle = Marshal.AllocHGlobal(_count * size);

                // place all interop recipients into the memory just allocated
                int ptr = (int)_handle;
                foreach (Recipient native in outer)
                {
                    MapiMailMessage.MAPIHelperInterop.MapiRecipDesc interop = native.GetInteropRepresentation();

                    // stick it in the memory block
                    Marshal.StructureToPtr(interop, (IntPtr)ptr, false);
                    ptr += size;
                }
            }
예제 #6
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();
					}
				}
			}
		}