/// <summary> /// This method forwards emails according to the parameters /// </summary> /// <param name="operation">argument for MailForward</param> /// <returns>true if processed successfully, false otherwise</returns> private bool MailForward(MailSimOperationsMailForward operation) { var parsedOp = ParseOperation(operation, operation.Folder, operation.MailSubjectToForward); return parsedOp.Iterate((indexToForward, mails) => { IMailItem mailToForward = mails[indexToForward].Forward(); Log.Out(Log.Severity.Info, operation.OperationName, "Subject: {0}", mailToForward.Subject); mailToForward.Body = BuildBody(operation.ForwardBody) + mailToForward.Body; Log.Out(Log.Severity.Info, operation.OperationName, "Body: {0}", mailToForward.Body); if (!AddRecipients(mailToForward, operation/*, operation.Items[0] is MailSimOperationsMailForwardRandomAttachments*/)) { return false; } AddAttachments(mailToForward, operation); mailToForward.Send(); return true; }); }
/// <summary> /// This method forwards emails according to the parameters /// </summary> /// <param name="operation">argument for MailForward</param> /// <returns>true if processed successfully, false otherwise</returns> private bool MailForward(MailSimOperationsMailForward operation) { int iterations = GetIterationCount(operation.Count); bool random = false; try { // retrieves mails from Outlook var mails = GetMails(operation.OperationName, operation.Folder, operation.MailSubjectToForward).ToList(); if (mails.Any() == false) { Log.Out(Log.Severity.Error, operation.OperationName, "Skipping MailForward"); return false; } // randomly generates the number of emails to forward if (iterations == 0) { random = true; iterations = randomNum.Next(1, mails.Count + 1); Log.Out(Log.Severity.Info, operation.OperationName, "Randomly forwarding {0} emails", iterations); } // we need to make sure we are not forwarding more than what we have in the mailbox if (iterations > mails.Count) { Log.Out(Log.Severity.Warning, operation.OperationName, "Only {1} email(s) are in the folder, so the the number of emails to forward is adjusted from {0} to {1}", iterations, mails.Count); iterations = mails.Count; } for (int count = 1; count <= iterations; count++) { Log.Out(Log.Severity.Info, operation.OperationName, "Starting iteration {0}", count); List<string> recipients = GetRecipients(operation.OperationName, operation.RecipientType, operation.Recipients); if (recipients == null) { Log.Out(Log.Severity.Error, operation.OperationName, "Recipient is not specified, skipping operation"); return false; } List<string> attachments = GetAttachments(operation.OperationName, operation.Attachments); // just forward the email in order if random is not selected, // otherwise randomly pick the mail to forward int indexToForward = random ? randomNum.Next(0, mails.Count) : count - 1; IMailItem mailToForward = mails[indexToForward].Forward(); Log.Out(Log.Severity.Info, operation.OperationName, "Subject: {0}", mailToForward.Subject); mailToForward.Body = System.DateTime.Now.ToString() + " - " + ((string.IsNullOrEmpty(operation.ForwardBody)) ? DefaultBody : operation.ForwardBody) + mailToForward.Body; Log.Out(Log.Severity.Info, operation.OperationName, "Body: {0}", mailToForward.Body); // adds all recipients foreach (string recpt in recipients) { Log.Out(Log.Severity.Info, operation.OperationName, "Recipient: {0}", recpt); mailToForward.AddRecipient(recpt); } // processes the attachment foreach (string attmt in attachments) { Log.Out(Log.Severity.Info, operation.OperationName, "Attachment: {0}", attmt); mailToForward.AddAttachment(attmt); } mailToForward.Send(); SleepOrStop(operation.OperationName, operation.Sleep); Log.Out(Log.Severity.Info, operation.OperationName, "Finished iteration {0}", count); } } catch (Exception ex) { Log.Out(Log.Severity.Error, operation.OperationName, "Exception encountered\n" + ex); return false; } return true; }