/// <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)) { return false; } AddAttachments(mailToForward, operation); mailToForward.Send(); return true; })); }
/// <summary> /// This method sends mail according to the parameter /// </summary> /// <param name="operation">parameters for MailSend</param> /// <returns>true if processed successfully, false otherwise</returns> private bool MailSend(MailSimOperationsMailSend operation) { int iterations = GetIterationCount(operation.Count); if (iterations < 1) { Log.Out(Log.Severity.Error, operation.OperationName, "Count is less than the minimum allowed value", iterations); return(false); } for (int count = 1; count <= iterations; count++) { try { // generates a new email IMailItem mail = olMailStore.NewMailItem(); mail.Subject = DateTime.Now.ToString() + " - "; mail.Subject += (string.IsNullOrEmpty(operation.Subject)) ? DefaultSubject : operation.Subject; Log.Out(Log.Severity.Info, operation.OperationName, "Subject: {0}", mail.Subject); mail.Body = BuildBody(operation.Body); Log.Out(Log.Severity.Info, operation.OperationName, "Body: {0}", mail.Body); if (!AddRecipients(mail, operation)) { return(false); } AddAttachments(mail, operation); mail.Send(); } catch (Exception ex) { Log.Out(Log.Severity.Error, operation.OperationName, "Exception encountered\n" + ex); return(false); } SleepOrStop(operation.OperationName, operation.Sleep); } return(true); }
/// <summary> /// This method replies email according to the parameters /// </summary> /// <param name="operation">parameters for MailReply</param> /// <returns>true if processed successfully, false otherwise</returns> private bool MailReply(MailSimOperationsMailReply operation) { var parsedOp = ParseOperation(operation, operation.Folder, operation.MailSubjectToReply); return(parsedOp.Iterate((indexToReply, mails) => { IMailItem mailToReply = mails[indexToReply].Reply(operation.ReplyAll); Log.Out(Log.Severity.Info, operation.OperationName, "Subject: {0}", mailToReply.Subject); mailToReply.Body = BuildBody(operation.ReplyBody) + mailToReply.Body; Log.Out(Log.Severity.Info, operation.OperationName, "Body: {0}", mailToReply.Body); // process the attachment AddAttachments(mailToReply, operation); mailToReply.Send(); return true; })); }
/// <summary> /// Test module, focusing on Outlook (OOM) wrapper classes (Mail*) /// Also serves as an example for Mail* classes usage /// </summary> /// <param name="args">Command line argument. It is expected that the first argument is always "/t"</param> public void Execute(string[] args) { IMailStore mailStore = null; try { // We will use mailbox with display name specified in arg[1]; // otherwise, we will get the default store mailStore = ProviderFactory.CreateMailStore(args.Length > 1 ? args[1] : null); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); return; } catch (Exception ex) { Console.WriteLine(ex); return; } string mailboxName = mailStore.DisplayName; // Display all top folders in the mailbox IMailFolder rootFolder = mailStore.RootFolder; var rootFolders = rootFolder.SubFolders; foreach (var mailFolder in rootFolders) { Console.WriteLine(mailFolder.FolderPath); } // Open Inbox and loop through it's top folders IMailFolder inbox = mailStore.GetDefaultFolder("olFolderInbox"); // Registering callback for Inbox "ItemAdd" event inbox.RegisterItemAddEventHandler(FolderEvent); Console.WriteLine("Inbox has {0} mail items", inbox.MailItemsCount); var inboxSubFolders = inbox.SubFolders; IMailFolder testFolder = null; const string testFolderName = "MailSim Test Folder"; Console.WriteLine("Exploring Inbox Folders"); foreach (IMailFolder mailFolder in inboxSubFolders) { if (testFolderName == mailFolder.Name) { testFolder = mailFolder; } Console.WriteLine(mailFolder.FolderPath); } if (null == testFolder) { testFolder = inbox.AddSubFolder(testFolderName); } // Adding folder under Test Folder string folderName = "Test Subfolder " + (testFolder.SubFoldersCount + 1).ToString() + " - " + DateTime.Now.TimeOfDay.ToString(); testFolder.AddSubFolder(folderName); // Deleting folder under Test Folder if (testFolder.SubFoldersCount > 2) { testFolder.SubFolders.First().Delete(); } // Deleting email in inbox if (inbox.MailItemsCount > 2) { var mailToDelete = inbox.MailItems.First(); Console.WriteLine("Deleting email {0}", mailToDelete.Subject); mailToDelete.Delete(); } // Moving email if (inbox.MailItemsCount > 2) { inbox.MailItems.First().Move(testFolder); } // Sending new email with message attachment to matching users Console.WriteLine("Sending new email to matching GAL users"); IMailItem newMail = mailStore.NewMailItem(); newMail.Subject = "Test Mail from MailSim to GAL users " + (inbox.MailItemsCount + 1).ToString() + " - " + DateTime.Now.TimeOfDay.ToString(); newMail.Body = "Test from MailSim to matching users"; if (inbox.MailItemsCount > 0) { var message = inbox.MailItems.First(); newMail.AddAttachment(message); } newMail.AddRecipient(mailboxName); var gal = mailStore.GetGlobalAddressList(); if (gal != null) { var users = gal.GetUsers("Mailsim"); foreach (string userAddress in users) { newMail.AddRecipient(userAddress); } } if (newMail.ValidateRecipients()) { newMail.Send(); Console.WriteLine("Mail to specified users sent!"); } else { Console.WriteLine("Incorrect recipient(s), mail not sent"); } // Sending new email with to DL members Console.WriteLine("Sending new email to DL users"); newMail = mailStore.NewMailItem(); newMail.Subject = "Test Mail from MailSim to DL members " + (inbox.MailItemsCount + 1).ToString() + " - " + DateTime.Now.TimeOfDay.ToString(); newMail.Body = "Test from MailSim to DL members"; newMail.AddRecipient(mailboxName); if (gal != null) { var members = gal.GetDLMembers("Mailsim Users"); if (members.Any() == false) { Console.WriteLine("ERROR: Distribution list not found or empty"); } else { foreach (string userAddress in members) { newMail.AddRecipient(userAddress); } } } if (newMail.ValidateRecipients()) { newMail.Send(); Console.WriteLine("Mail to distribution list members sent!"); } else { Console.WriteLine("Incorrect recipient(s), mail not sent"); } var inboxMailItems = inbox.MailItems; // Reply All if (inbox.MailItemsCount >= 1) { var replyMail = inboxMailItems.First().Reply(true); replyMail.Body = "Reply All by MailSim" + replyMail.Body; Console.WriteLine("Message Body:"); Console.WriteLine(replyMail.Body); replyMail.Send(); Console.WriteLine("Reply All mail sent!"); } // Forward if (inbox.MailItemsCount >= 2) { var forwardMail = inboxMailItems.Skip(1).First().Forward(); forwardMail.Body = "Forward by MailSim" + forwardMail.Body; Console.WriteLine("Message Body:"); Console.WriteLine(forwardMail.Body); forwardMail.AddRecipient(mailboxName); if (forwardMail.ValidateRecipients()) { forwardMail.Send(); Console.WriteLine("Forwarded the mail(s)"); } else { Console.WriteLine("Incorrect recipient(s), mail(s) cannot be forwarded"); } } Console.WriteLine("Press any key to exit"); Console.Read(); inbox.UnRegisterItemAddEventHandler(); }