private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange( Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime) { string filter = "[Start] >= '" + startTime.ToString("g") + "' AND [End] <= '" + endTime.ToString("g") + "'"; //Response.Write(filter); try { Microsoft.Office.Interop.Outlook.Items calItems = folder.Items; calItems.IncludeRecurrences = true; calItems.Sort("[Start]", Type.Missing); Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter); if (restrictItems.Count > 0) { return(restrictItems); } else { return(null); } } catch { return(null); } }
private void appointmentsInRange() { Microsoft.Office.Interop.Outlook.Application a = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder; DateTime start = DateTime.Now; DateTime end = start.AddDays(1); Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); if (rangeAppts != null) { int size = 0; foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts) { size++; } ss.SpeakAsync("You have " + size + " items on your calendar today"); foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts) { setLabel(appt.Subject + " starts at " + appt.Start.ToString()); ss.SpeakAsync(appt.Subject + " starts at " + appt.Start.ToString()); opt += appt.Subject + " " + appt.Start.ToString() + "#"; } } else { ss.SpeakAsync("Sorry i could not find anything! Try again later"); } }
private Microsoft.Office.Interop.Outlook.Folder GetMatchingFolder(Microsoft.Office.Interop.Outlook.Folder folder, string personName) { if (folder.Name != personName && _destinationFolder == null) { foreach (Microsoft.Office.Interop.Outlook.Folder subFolder in folder.Folders) { if (subFolder.Name != personName && _destinationFolder == null) { _destinationFolder = GetMatchingFolder(subFolder, personName); } else { if (_destinationFolder == null) { _destinationFolder = subFolder; } } } } else { _destinationFolder = folder; } return(_destinationFolder); }
static void IterateMessages(Microsoft.Office.Interop.Outlook.Folder folder, string basePath) { var fi = folder.Items.Restrict("[Unread] = true"); if (fi != null) { foreach (Object item in fi) { Microsoft.Office.Interop.Outlook.MailItem mi = (Microsoft.Office.Interop.Outlook.MailItem)item; var attachments = mi.Attachments; if (attachments.Count != 0) { // Create a directory to store the attachment if (!Directory.Exists(basePath + folder.FolderPath)) { Directory.CreateDirectory(basePath + folder.FolderPath); } for (int i = 1; i <= mi.Attachments.Count; i++) { var fn = mi.Attachments[i].FileName.ToLower(); //check wither any of the strings in the extensionsArray are contained within the filename // if (extensionsArray.Any(fn.Contains)) // { // Create a further sub-folder for the sender if (!Directory.Exists(basePath + folder.FolderPath + @"\" + mi.Sender.Address)) { Directory.CreateDirectory(basePath + folder.FolderPath + @"\" + mi.Sender.Address); } //totalfilesize = totalfilesize + mi.Attachments[i].Size; if (!File.Exists(basePath + folder.FolderPath + @"\" + mi.Sender.Address + @"\" + mi.Attachments[i].FileName)) { Console.WriteLine("Saving " + mi.Attachments[i].FileName); mi.Attachments[i].SaveAsFile(basePath + folder.FolderPath + @"\" + mi.Sender.Address + @"\" + mi.Attachments[i].FileName); mi.Body = mi.Body + "Anhange nach " + basePath + folder.FolderPath + @"\" + mi.Sender.Address + @"\" + mi.Attachments[i].FileName + " gespeichert."; //mi.Attachments[i].Delete(); } else { Console.WriteLine("Already saved " + mi.Attachments[i].FileName); } // } //mi.Attachments[i].SaveAsFile(pathToSaveFile); //mi.Attachments[i].Delete(); Console.WriteLine("Subject: " + mi.Subject + " Attachment: " + mi.Attachments[i].FileName); } } } } }
private void GetMailFolders(Microsoft.Office.Interop.Outlook.Folder objInpFolder, TreeNode objInpNode) { try { foreach (Microsoft.Office.Interop.Outlook.Folder objFolder in objInpFolder.Folders) { if (objFolder.Name.ToUpper() == "SENT ITEMS" || objFolder.Name.ToUpper() == "OUTBOX") { continue; } if (objFolder.Folders.Count > 0) { TreeNode objNode = new TreeNode() { Tag = objFolder.EntryID, Text = objFolder.Name }; if (this.settings.AutoArchiveFolders.Contains(objFolder.EntryID)) { objNode.Checked = true; } objInpNode.Nodes.Add(objNode); GetMailFolders(objFolder, objNode); } else { TreeNode objNode = new TreeNode() { Tag = objFolder.EntryID, Text = objFolder.Name }; if (this.settings.AutoArchiveFolders.Contains(objFolder.EntryID)) { objNode.Checked = true; } objInpNode.Nodes.Add(objNode); } } } catch (Exception ex) { string strLog; strLog = "------------------" + System.DateTime.Now.ToString() + "-----------------\n"; strLog += "GetMailFolders method General Exception:" + "\n"; strLog += "Message:" + ex.Message + "\n"; strLog += "Source:" + ex.Source + "\n"; strLog += "StackTrace:" + ex.StackTrace + "\n"; strLog += "Data:" + ex.Data.ToString() + "\n"; strLog += "HResult:" + ex.HResult.ToString() + "\n"; strLog += "-------------------------------------------------------------------------" + "\n"; clsSuiteCRMHelper.WriteLog(strLog); ex.Data.Clear(); } }
private void DemoAppointmentsInRange() { Application a = new Application(); Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder; DateTime start = DateTime.Now; DateTime end = start.AddDays(5); Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); if (rangeAppts != null) { foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts) { Response.Write("Subject: " + appt.Subject + " " + " Start: " + appt.Start.ToString() + " " + "End:" + appt.End.ToString() + "<br/>"); } } }
static void EnumerateFolders(Microsoft.Office.Interop.Outlook.Folder folder, string pathToSaveFile) { Microsoft.Office.Interop.Outlook.Folders childFolders = folder.Folders; if (childFolders.Count > 0) { foreach (Microsoft.Office.Interop.Outlook.Folder childFolder in childFolders) { // We only want Inbox folders - ignore Contacts and others if (childFolder.FolderPath.Contains("Inbox")) { MessageBox.Show(childFolder.FolderPath); // Call EnumerateFolders using childFolder, to see if there are any sub-folders within this one EnumerateFolders(childFolder, pathToSaveFile); } } } MessageBox.Show("Checking in " + folder.FolderPath); IterateMessages(folder, pathToSaveFile); }
public void RefreshFolderList(ListBox listBox, Microsoft.Office.Interop.Outlook.Folder folder = null) { if (folder == null) { List <Microsoft.Office.Interop.Outlook.Folder> folders = new List <Microsoft.Office.Interop.Outlook.Folder>(); foreach (Microsoft.Office.Interop.Outlook.Folder f in outlook.Session.Folders) { folders.Add(f); } listBox.Items.Clear(); Application.DoEvents(); foreach (Microsoft.Office.Interop.Outlook.Folder f in folders.OrderBy(f => f.FolderPath)) { listBox.Items.Add(f.FolderPath); Application.DoEvents(); RefreshFolderList(listBox, f); } } else { List <Microsoft.Office.Interop.Outlook.Folder> folders = new List <Microsoft.Office.Interop.Outlook.Folder>(); foreach (Microsoft.Office.Interop.Outlook.Folder f in folder.Folders) { folders.Add(f); } foreach (Microsoft.Office.Interop.Outlook.Folder f in folders.OrderBy(f => f.FolderPath)) { listBox.Items.Add(f.FolderPath); Application.DoEvents(); RefreshFolderList(listBox, f); } } }
public void FileMessage_OnClick(Microsoft.Office.Core.IRibbonControl control) { var app = new Microsoft.Office.Interop.Outlook.Application(); var explorer = app.ActiveExplorer(); Microsoft.Office.Interop.Outlook.Selection selections = explorer.Selection; Microsoft.Office.Interop.Outlook.Folder inbox = app.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) as Microsoft.Office.Interop.Outlook.Folder; foreach (var selection in selections) { var personName = string.Empty; if (selection is Microsoft.Office.Interop.Outlook.MailItem) { personName = ((Microsoft.Office.Interop.Outlook.MailItem)selection).SenderName; _destinationFolder = GetMatchingFolder(inbox, personName); MoveMailItem((Microsoft.Office.Interop.Outlook.MailItem)selection, personName); } else if (selection is Microsoft.Office.Interop.Outlook.MeetingItem) { personName = ((Microsoft.Office.Interop.Outlook.MeetingItem)selection).SenderName; _destinationFolder = GetMatchingFolder(inbox, personName); MoveMeetingItem((Microsoft.Office.Interop.Outlook.MeetingItem)selection, personName); } else { MessageBox.Show("Unknown message type, can't move it."); break; } _destinationFolder = null; } _destinationFolder = null; }
static void EnumerateFoldersInDefaultStore(string pathToSaveFile) { Microsoft.Office.Interop.Outlook.Application Application = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.Folder root = Application.Session.DefaultStore.GetRootFolder() as Microsoft.Office.Interop.Outlook.Folder; EnumerateFolders(root, pathToSaveFile); }
private void ExportFolder( string[] selectedFolders, Microsoft.Office.Interop.Outlook.Folder outlookFolder, string exportFolderMsg, string exportFolderHtml, TextBox status, Button cancelButton) { string outlookFolderPath = outlookFolder.FolderPath; status.AppendText(string.Format("Scanning folder {0}...\r\n", outlookFolderPath)); Application.DoEvents(); // If the folder is selected, get the list of mails and export them if (selectedFolders.FirstOrDefault(f => outlookFolderPath.StartsWith(f)) != null) { if (!string.IsNullOrWhiteSpace(exportFolderMsg) && !Directory.Exists(exportFolderMsg)) { Directory.CreateDirectory(exportFolderMsg); } if (!string.IsNullOrWhiteSpace(exportFolderHtml) && !Directory.Exists(exportFolderHtml)) { Directory.CreateDirectory(exportFolderHtml); } string lastExportedMail = ""; try { foreach (object item in outlookFolder.Items) { try { if (item is Microsoft.Office.Interop.Outlook.MailItem) { Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)item; string sentOn = mail.SentOn.ToString("yyyyMMdd_HHmmss"); string sender = mail.SenderName; string subject = mail.Subject == null ? "" : mail.Subject.Length > maxSubjectLength?mail.Subject.Substring(0, maxSubjectLength) : mail.Subject; string fullName = string.Format("{0} - {1} - {2}", sentOn, sender, subject); fullName = fullName.Replace("\\", "_"); fullName = fullName.Replace("/", "_"); fullName = fullName.Replace(":", "_"); fullName = fullName.Replace("*", "_"); fullName = fullName.Replace("?", "_"); fullName = fullName.Replace("<", "_"); fullName = fullName.Replace(">", "_"); fullName = fullName.Replace("|", "_"); fullName = fullName.Replace("\"", "_"); if (!string.IsNullOrWhiteSpace(exportFolderMsg)) { string exportFilenameMsg = string.Format("{0}\\{1}.msg", exportFolderMsg, fullName); if (!File.Exists(exportFilenameMsg)) { status.AppendText(string.Format(" Saving Msg {0}...\r\n", fullName)); Application.DoEvents(); try { mail.SaveAs(exportFilenameMsg, olMsg); } catch (Exception ex) { status.AppendText(string.Format("ERROR Saving Msg {0}\r\n", fullName)); status.AppendText(string.Format(" {0}\r\n", ex.Message)); Application.DoEvents(); } Application.DoEvents(); } } if (!string.IsNullOrWhiteSpace(exportFolderHtml)) { string exportFilenameHtml = string.Format("{0}\\{1}.txt", exportFolderHtml, fullName); if (!File.Exists(exportFilenameHtml)) { status.AppendText(string.Format(" Saving Txt {0}...\r\n", fullName)); Application.DoEvents(); if (mail.Attachments.Count > 0) { string attachmentsFolder = string.Format("{0}\\.attachments\\{1}", exportFolderHtml, sentOn); if (!Directory.Exists(attachmentsFolder)) { Directory.CreateDirectory(attachmentsFolder); } for (int a = 1; a <= mail.Attachments.Count; a++) { string attachmentFilename; try { attachmentFilename = mail.Attachments[a].FileName; } catch { try { attachmentFilename = string.Format("{0} - {1}", a, mail.Attachments[a].DisplayName); } catch { attachmentFilename = string.Format("{0} - unknown type", a); } } try { attachmentFilename = string.Format("{0}\\{1}", attachmentsFolder, attachmentFilename); if (!File.Exists(attachmentFilename)) { mail.Attachments[a].SaveAsFile(attachmentFilename); } } catch (Exception ex) { status.AppendText(" Error saving attachment "); status.AppendText(a.ToString()); status.AppendText(" "); status.AppendText(attachmentFilename); status.AppendText("\r\n"); throw ex; } } } mail.SaveAs(exportFilenameHtml, Microsoft.Office.Interop.Outlook.OlSaveAsType.olTXT); Application.DoEvents(); } else { //status.AppendText(" Already exists: "); //status.AppendText(exportFilenameHtml); //status.AppendText("\r\n"); } } mail = null; lastExportedMail = fullName; } } catch (Exception ex) { status.AppendText("ERROR exporting mail in "); status.AppendText(outlookFolder.FolderPath); status.AppendText("\r\n"); status.AppendText(ex.Message); status.AppendText("\r\n"); status.AppendText("Last exported mail: "); status.AppendText(lastExportedMail); status.AppendText("\r\n"); } if (!cancelButton.Enabled) { return; } Application.DoEvents(); } } catch (Exception ex) { status.AppendText("ERROR exporting folder "); status.AppendText(outlookFolder.FolderPath); status.AppendText("\r\n"); status.AppendText("Last exported mail: "); status.AppendText(lastExportedMail); status.AppendText("\r\n"); throw ex; } GC.Collect(); } // Now process and subfolders foreach (Microsoft.Office.Interop.Outlook.Folder f in outlookFolder.Folders) { ExportFolder( selectedFolders, f, !string.IsNullOrWhiteSpace(exportFolderMsg) ? string.Format("{0}\\{1}", exportFolderMsg, f.Name) : "", !string.IsNullOrWhiteSpace(exportFolderHtml) ? string.Format("{0}\\{1}", exportFolderHtml, f.Name) : "", status, cancelButton); } }