示例#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);
        }
示例#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);
        }
示例#3
0
        public EmailConfiguration(List <object> args)
        {
            this.settings = Program.Configuration.Email;
            var emailConfigArray = args;

            if (emailConfigArray.Count != 8)
            {
                throw new Exception(
                          $"Incorrect number of email config array items - got {emailConfigArray.Count}, expected 8");
            }

            this.Id          = Guid.NewGuid();
            this.To          = new List <string>();
            this.Cc          = new List <string>();
            this.Bcc         = new List <string>();
            this.Attachments = new List <string>();

            this.From = emailConfigArray[0].ToString();
            //TODO: from just going to be the first account we find already registered in outlook
            //if (this.From.Equals("CurrentUser", StringComparison.CurrentCultureIgnoreCase))
            //{
            //    this.From = $"{Environment.UserName}@{System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName}";
            //}

            this.To  = ParseEmail(emailConfigArray[1].ToString(), settings.RecipientsToMin, settings.RecipientsToMax);
            this.Cc  = ParseEmail(emailConfigArray[2].ToString(), settings.RecipientsCcMin, settings.RecipientsCcMax);
            this.Bcc = ParseEmail(emailConfigArray[3].ToString(), settings.RecipientsBccMin, settings.RecipientsBccMax);

            var emailContent = new EmailContentManager();

            this.Subject = emailConfigArray[4].ToString();

            if (this.Subject.Equals("random", StringComparison.InvariantCultureIgnoreCase))
            {
                this.Subject = emailContent.Subject;
            }

            this.Body = emailConfigArray[5].ToString();
            if (this.Body.Equals("random", StringComparison.InvariantCultureIgnoreCase))
            {
                this.Body = emailContent.Body;

                this.Body +=
                    $"{Environment.NewLine}{Environment.NewLine}CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, may contain information that is protected by the DoD Privacy Act. This e-mail transmission is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that you are not authorized to read, print, retain, copy, disclose, distribute, or use this message, any part of it, or any attachments. If you have received this message in error, please immediately notify the sender by telephone or return e-mail and delete this message and any attachments from your system without reading or saving in any manner. You can obtain additional information about the DoD Privacy Act at http://dpclo.defense.gov/privacy. Thank you.{Environment.NewLine}Timestamp: {DateTime.Now} ID: {this.Id}";
            }

            this.BodyType = EmailBodyType.PlainText;


            if (!string.IsNullOrEmpty(emailConfigArray[6].ToString()))
            {
                emailConfigArray[6] = emailConfigArray[6].ToString().Trim();
                if (emailConfigArray[6].ToString().Equals("HTML", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.BodyType = EmailBodyType.HTML;
                }
                else if (emailConfigArray[6].ToString().Equals("RTF", StringComparison.InvariantCultureIgnoreCase))
                {
                    this.BodyType = EmailBodyType.RTF;
                }
            }

            if (!string.IsNullOrEmpty(emailConfigArray[7].ToString()))
            {
                var a = emailConfigArray[7].ToString().Split(Convert.ToChar(","));
                foreach (var o in a)
                {
                    if (File.Exists(o))
                    {
                        this.Attachments.Add(o);
                    }
                    else
                    {
                        log.Debug($"Can't add attachment {o} - file was not found");
                    }
                }
            }
        }