Ejemplo n.º 1
0
        public void Send(MailMessage msg)
        {
            if (msg == null) throw new ArgumentNullException("msg");

            Outlook.Application app = new Outlook.Application();
            var ns = app.GetNamespace("MAPI");
            ns.Logon();

            try
            {
                var fld = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
                Outlook.MailItem mail = (Outlook.MailItem)fld.Items.Add();

                foreach (var to in msg.To)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olTo;
                }
                foreach (var to in msg.CC)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olCC;
                }
                foreach (var to in msg.Bcc)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olBCC;
                }

                mail.Subject = msg.Subject;
                if (msg.IsBodyHtml)
                {
                    mail.HTMLBody = msg.Body;
                }
                else
                {
                    mail.Body = msg.Body;
                }

                foreach (var a in msg.Attachments)
                {
                    var tmpFile = _tmpService.Create(a.Name);
                    using (var fs = File.OpenWrite(tmpFile))
                    {
                        a.ContentStream.CopyTo(fs);
                    }
                    var olAttachment = mail.Attachments.Add(tmpFile, Type.Missing, Type.Missing, a.Name);
                }

                mail.Display();
            }
            catch (COMException ex)
            {
                Logging.Client.Error("Unable to send mail throug Outlook", ex);
                _vmf.ShowMessage(ex.ErrorCode == E_ABORT
                        ? OutlookMailSenderResources.AbortErrorMessage
                        : OutlookMailSenderResources.GenericErrorMessage,
                    OutlookMailSenderResources.ErrorCaption);
            }
            finally
            {
                ns.Logoff();
            }
        }
Ejemplo n.º 2
0
        public void Send(MailMessage msg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }

            Outlook.Application app = new Outlook.Application();
            var ns = app.GetNamespace("MAPI");

            ns.Logon();

            try
            {
                var fld = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
                Outlook.MailItem mail = (Outlook.MailItem)fld.Items.Add();

                foreach (var to in msg.To)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olTo;
                }
                foreach (var to in msg.CC)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olCC;
                }
                foreach (var to in msg.Bcc)
                {
                    var r = mail.Recipients.Add(to.Address);
                    r.Type = (int)Outlook.OlMailRecipientType.olBCC;
                }

                mail.Subject = msg.Subject;
                if (msg.IsBodyHtml)
                {
                    mail.HTMLBody = msg.Body;
                }
                else
                {
                    mail.Body = msg.Body;
                }

                foreach (var a in msg.Attachments)
                {
                    var tmpFile = _tmpService.Create(a.Name);
                    using (var fs = File.OpenWrite(tmpFile))
                    {
                        a.ContentStream.CopyTo(fs);
                    }
                    var olAttachment = mail.Attachments.Add(tmpFile, Type.Missing, Type.Missing, a.Name);
                }

                mail.Display();
            }
            catch (COMException ex)
            {
                Logging.Client.Error("Unable to send mail throug Outlook", ex);
                _vmf.ShowMessage(ex.ErrorCode == E_ABORT
                        ? OutlookMailSenderResources.AbortErrorMessage
                        : OutlookMailSenderResources.GenericErrorMessage,
                                 OutlookMailSenderResources.ErrorCaption);
            }
            finally
            {
                ns.Logoff();
            }
        }