private static MailAddress BuildMailAddress(SafeRecipient recipient)
        {
            if ("EX" == recipient.AddressEntry.Type) //exhange recipient
            {
                string smtpAddress = (string)recipient.get_Fields(0x39FE001E);

                if (null == smtpAddress)
                {
                    
                }

                return new MailAddress(smtpAddress, recipient.Name);
            }
            else
            {
                return new MailAddress(recipient.Address, recipient.Name);
            }
        }
示例#2
0
        private bool SendEmailViaOutlook(EmailConfiguration emailConfig)
        {
            ClientConfiguration.EmailSettings config = Program.Configuration.Email;
            bool wasSuccessful = false;

            try
            {
                //now create mail object (but we'll not send it via outlook)
                _log.Trace("Creating outlook mail item");
                dynamic mailItem = _app.CreateItem(OlItemType.olMailItem);

                //Add subject
                if (!string.IsNullOrWhiteSpace(emailConfig.Subject))
                {
                    mailItem.Subject = emailConfig.Subject;
                }

                _log.Trace($"Setting message subject to: {mailItem.Subject}");

                //Set message body according to type of message
                switch (emailConfig.BodyType)
                {
                case EmailConfiguration.EmailBodyType.HTML:
                    mailItem.HTMLBody = emailConfig.Body;
                    _log.Trace($"Setting message HTMLBody to: {emailConfig.Body}");
                    break;

                case EmailConfiguration.EmailBodyType.RTF:
                    mailItem.RTFBody = emailConfig.Body;
                    _log.Trace($"Setting message RTFBody to: {emailConfig.Body}");
                    break;

                case EmailConfiguration.EmailBodyType.PlainText:
                    mailItem.Body = emailConfig.Body;
                    _log.Trace($"Setting message Body to: {emailConfig.Body}");
                    break;

                default:
                    throw new Exception("Bad email body type: " + emailConfig.BodyType);
                }

                //attachments
                if (emailConfig.Attachments.Count > 0)
                {
                    //Add attachments
                    foreach (string path in emailConfig.Attachments)
                    {
                        mailItem.Attachments.Add(path);
                        _log.Trace($"Adding attachment from: {path}");
                    }
                }

                if (config.SetAccountFromConfig || config.SetAccountFromLocal)
                {
                    Accounts accounts = _app.Session.Accounts;
                    Account  acc      = null;

                    if (config.SetAccountFromConfig)
                    {
                        //Look for our account in the Outlook
                        foreach (Account account in accounts)
                        {
                            if (account.SmtpAddress.Equals(emailConfig.From, StringComparison.CurrentCultureIgnoreCase))
                            {
                                //Use it
                                acc = account;
                                break;
                            }
                        }
                    }

                    //TODO: if no from account found, just use first one found to send - but should ghosts do this?
                    if (acc == null)
                    {
                        foreach (Account account in accounts)
                        {
                            acc = account;
                            break;
                        }
                    }

                    //Did we get the account?
                    if (acc != null)
                    {
                        _log.Trace($"Sending via {acc.DisplayName}");
                        //Use this account to send the e-mail
                        mailItem.SendUsingAccount = acc;
                    }
                }

                if (config.SaveToOutbox)
                {
                    _log.Trace("Saving mailItem to outbox...");
                    mailItem.Move(_folderOutbox);
                    mailItem.Save();
                }

                _log.Trace("Attempting new Redemtion SafeMailItem...");
                SafeMailItem rdoMail = new Redemption.SafeMailItem
                {
                    Item = mailItem
                };
                //Parse To
                if (emailConfig.To.Count > 0)
                {
                    System.Collections.Generic.IEnumerable <string> list = emailConfig.To.Distinct();
                    foreach (string a in list)
                    {
                        SafeRecipient r = rdoMail.Recipients.AddEx(a.Trim());
                        r.Resolve();
                        _log.Trace($"RdoMail TO {a.Trim()}");
                    }
                }
                else
                {
                    throw new Exception("Must specify to-address");
                }

                //Parse Cc
                if (emailConfig.Cc.Count > 0)
                {
                    System.Collections.Generic.IEnumerable <string> list = emailConfig.Cc.Distinct();
                    foreach (string a in list)
                    {
                        SafeRecipient r = rdoMail.Recipients.AddEx(a.Trim());
                        r.Resolve();
                        if (r.Resolved)
                        {
                            r.Type = 2; //CC
                        }

                        _log.Trace($"RdoMail CC {a.Trim()}");
                    }
                }

                if (emailConfig.Bcc.Count > 0)
                {
                    System.Collections.Generic.IEnumerable <string> list = emailConfig.Bcc.Distinct();
                    foreach (string a in list)
                    {
                        SafeRecipient r = rdoMail.Recipients.AddEx(a.Trim());
                        r.Resolve();
                        if (r.Resolved)
                        {
                            r.Type = 3; //BCC
                        }

                        _log.Trace($"RdoMail BCC {a.Trim()}");
                    }
                }

                /*
                 *  outlook_mail_item = self._outlook.outlook_application.CreateItem(win32com.client.constants.olMailItem)
                 *  outlook_mail_item = outlook_mail_item.Move(outbox)
                 *
                 *  outlook_mail_item.Subject = subject
                 *  outlook_mail_item.Body = body
                 *  outlook_mail_item.Save()
                 *
                 *  for file_ in self._config['attachments']:
                 *      outlook_mail_item.Attachments.Add(file_)
                 *
                 # Need to use Redemption to actually get it to send correctly.
                 #  new_email = win32com.client.Dispatch('Redemption.SafeMailItem')
                 #  new_email.Item = outlook_mail_item
                 #  new_email.Recipients.Add(self._config['destination'])
                 #  new_email.Recipients.ResolveAll()
                 #  new_email.Send()
                 */


                rdoMail.Recipients.ResolveAll();

                _log.Trace("Attempting to send Redemtion SafeMailItem...");
                rdoMail.Send();

                var mapiUtils = new Redemption.MAPIUtils();
                mapiUtils.DeliverNow();

                //Done
                wasSuccessful = true;

                _log.Trace("Redemtion SafeMailItem was sent successfully");

                if (config.SetForcedSendReceive)
                {
                    _log.Trace("Forcing mapi - send and receive");
                    _oMapiNamespace.SendAndReceive(false);
                    Thread.Sleep(3000);
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }
            _log.Trace($"Returning - wasSuccessful:{wasSuccessful}");
            return(wasSuccessful);
        }
        protected IRoutingItem MakeSMTPRoutingItem(SafeRecipient recipient)
        {
            AddressEntry addrEntry = recipient.AddressEntry;
            object MAPIProp = addrEntry.MAPIOBJECT;

            using (new ComObjectGovernor(MAPIProp))
            using (new ComObjectGovernor(addrEntry))
            {
                if ("EX" == addrEntry.Type) //exchange recipient
                {
                    string smtpAddress = null;

                    smtpAddress = GetUtils().HrGetOneProp(MAPIProp, 0x39FE001E) as string;

                    if (null == smtpAddress)
                    {
                        smtpAddress = recipient.get_Fields(0x39FE001E) as string;
                    }

                    if (null == smtpAddress)
                    {
                        string msg = String.Format(System.Threading.Thread.CurrentThread.CurrentCulture,
                            "Failed to get a valid smtp address for recipient: {0}", recipient.Name);
                        Logging.Trace.WriteLine(msg, "RedemptionMailConverter.BuildMailAddress");
                    }

                    return MakeSMTPRoutingItem(smtpAddress, recipient.Name);
                }
                else
                {
                    return MakeSMTPRoutingItem(recipient.Address, recipient.Name);
                }
            }
        }