Esempio n. 1
0
        private bool ReplyViaOutlook(EmailConfiguration emailConfig)
        {
            ClientConfiguration.EmailSettings config = Program.Configuration.Email;

            try
            {
                Items folderItems = _folderSent.Items;

                foreach (MailItem folderItem in folderItems)
                {
                    if (folderItem.UnRead)
                    {
                        EmailReplyManager emailReply = new EmailReplyManager();


                        folderItem.HTMLBody =
                            $"{emailReply.Reply} {Environment.NewLine}{Environment.NewLine}ORIGINAL MESSAGE --- {Environment.NewLine}{Environment.NewLine}{folderItem.Body}";
                        folderItem.Subject = $"RE: {folderItem.Subject}";

                        MailItem replyMail = folderItem.Reply();
                        replyMail.Move(_folderSent);

                        SafeMailItem rdoMail = new Redemption.SafeMailItem
                        {
                            Item = replyMail
                        };

                        rdoMail.Recipients.ResolveAll();
                        rdoMail.Send();

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

                        if (config.SetForcedSendReceive)
                        {
                            _log.Trace("Forcing mapi - send and receive");
                            _oMapiNamespace.SendAndReceive(false);
                            Thread.Sleep(3000);
                        }

                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error($"Outlook reply error: {e}");
            }
            return(false);
        }
Esempio n. 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);
        }