Пример #1
0
        public virtual MAPI SendMail(MailMessage mailMessage)
        {
            const int mapiLogonUi = 0x00000001;
            const int mapiDialog = 0x00000008;
            const int how = mapiLogonUi | mapiDialog;

            _message = new MapiMessage { subject = mailMessage.Subject(), noteText = mailMessage.Body() };

            _message.recips = GetRecipients(mailMessage, out _message.recipCount);
            _message.files = GetAttachments(mailMessage, out _message.fileCount);

            var errorId = MAPISendMail(new IntPtr(0), new IntPtr(0), ref _message, how, 0);
            if (errorId > 1)
                throw new MAPIException(errorId);

            return this;
        }
Пример #2
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                var mm =
                new MailMessage().AddToAddress(txtTo.Text)
                                             .AddCCAddress(txtCC.Text)
                                             .Subject(txtSubject.Text)
                                             .Body(txtBody.Text);

                foreach (ListViewItem item in lvwAttachments.Items)
                {
                    mm.AddAttachment(item.Tag.ToString());
                }

                mm.Send();
            }
            catch (MAPIException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #3
0
        static IntPtr GetAttachments(MailMessage mailMessage, out int fileCount)
        {
            fileCount = 0;
            if (mailMessage.AttachmentFilePaths == null || mailMessage.AttachmentFilePaths.Count == 0)
                return IntPtr.Zero;

            var size = Marshal.SizeOf(typeof(MapiFileDesc));
            var intPtr = Marshal.AllocHGlobal(mailMessage.AttachmentFilePaths.Count * size);

            var mapiFileDesc = new MapiFileDesc { position = -1 };
            var ptr = (int)intPtr;

            foreach (string strAttachment in mailMessage.AttachmentFilePaths)
            {
                mapiFileDesc.name = Path.GetFileName(strAttachment);
                mapiFileDesc.path = strAttachment;
                Marshal.StructureToPtr(mapiFileDesc, (IntPtr)ptr, false);
                ptr += size;
            }

            fileCount = mailMessage.AttachmentFilePaths.Count;
            return intPtr;
        }
Пример #4
0
 public void Before()
 {
     _mapiStub = new MAPIStub();
     _mailMessage = new MailMessage(_mapiStub);
 }
Пример #5
0
        static IntPtr GetRecipients(MailMessage mailMessage, out int recipCount)
        {
            recipCount = 0;

            var recipients = new List<MapiRecipDesc>();
            recipients.AddRange(mailMessage.ToAddresses.Select(toAddress => new MapiRecipDesc { recipClass = (int)AddressType.To, name = toAddress }));
            recipients.AddRange(mailMessage.CCAddresses.Select(ccAddress => new MapiRecipDesc { recipClass = (int)AddressType.CC, name = ccAddress }));
            recipients.AddRange(mailMessage.BCCAddresses.Select(bccAddress => new MapiRecipDesc { recipClass = (int)AddressType.BCC, name = bccAddress }));

            if (recipients.Count == 0)
                return IntPtr.Zero;

            var size = Marshal.SizeOf(typeof(MapiRecipDesc));
            var intPtr = Marshal.AllocHGlobal(recipients.Count * size);

            var ptr = (int)intPtr;
            foreach (var mapiDesc in recipients)
            {
                Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
                ptr += size;
            }

            recipCount = recipients.Count;
            return intPtr;
        }