.NET - Outlook mailboxen organiseren en opkuisen
At the beginning of the year I clean up and organise my Outlook mailboxes. I want to keep Outlook performing as fast as possible so I try to reduce the size of my inbox. Nowadays I use LINQPad, a few C# scripts and my OutlookProvider component to simplify this job. I have no idea if these scripts are useful for other developers but why not share them? So here they are.
Move mails
The following script will run through a list of emails and move them to other folders. Just specify the source folder, add your own query criteria and implement some rules and destination folders. This C# script should be run in LINQPad because it uses the Dump() method.
OutlookProvider outlookProvider = new OutlookProvider();
// ** CHOOSE FOLDER **
//var mails = outlookProvider.InboxItems;
//var mails = outlookProvider.SentMailItems;
var mails = outlookProvider.GetItems<MailItem>(@"\\Personal folders\OUT");
var mailsToMove = from mail in mails
let m = (mail as MailItem)
// ** CHANGE WHERE **
//where m.
select m;
mailsToMove.Select(m => new { m.SentOn.Date, m.Subject }).Dump("Mails which will be moved");
while (mailsToMove.Count() > 0)
{
var mail = mailsToMove.Last();
// ** CREATE RULES AND MOVE TO CHOOSEN FOLDERS **
if (mail.SentOn.Year == 2007)
{
var folder2007 = outlookProvider.Folders.FirstOrDefault(
f => ((Folder)f).FolderPath == @"\\Personal Archive\OUT\2007");
if (folder2007 != null)
{
mail.Move(folder2007);
}
}
else if (mail.SentOn.Year == 2008)
{
var folder2008 = outlookProvider.Folders.FirstOrDefault(
f => ((Folder)f).FolderPath == @"\\Personal Archive\OUT\2008");
if (folder2008 != null)
{
mail.Move(folder2008);
}
}
// Make sure to move all other mails to an new Folder!
// Otherwise this will be an infinitive loop.
else
{
var folderOthers = outlookProvider.Folders.FirstOrDefault(
f => ((Folder)f).FolderPath == @"\\Personal Archive\OUT\Other");
if (folderOthers != null)
{
mail.Move(folderOthers);
}
}
}
Delete mails
This small script will run through a list of emails and delete them.
OutlookProvider outlookProvider = new OutlookProvider();
// ** CHOOSE FOLDER **
//var mails = outlookProvider.InboxItems;
//var mails = outlookProvider.SentMailItems;
var mails = outlookProvider.GetItems<MailItem>(@"\\Personal folders\OUT");
var mailsToDelete = from mail in mails
let m = (mail as MailItem)
// ** CHANGE WHERE **
//where m.
select m;
mailsToDelete.Select(m => new { m.SentOn.Date, m.Subject })
.Dump("Mails which will be deleted");
while (mailsToDelete.Count() > 0)
{
mailsToDelete.Last().Delete();
}
Remove attachments
The following script can be used to remove large attachments from emails. Just specify the source folder and the minimum file size.
OutlookProvider outlookProvider = new OutlookProvider();
// ** CHOOSE FOLDER **
//var mails = outlookProvider.InboxItems;
//var mails = outlookProvider.SentMailItems;
var mails = outlookProvider.GetItems<MailItem>(@"\\Personal folders\OUT");
var mailsWithAttachments = from mail in mails
let m = (mail as MailItem)
let a = m.Attachments.OfType<Attachment>()
where
m.Attachments.Count > 0
// ** CHANGE SIZE **
&& m.Attachments.OfType<Attachment>().Sum(s => s.Size) > 1024 * 500
select m;
((mailsWithAttachments.Sum(m => m.Attachments.OfType<Attachment>().Sum(a => a.Size)) / 1024 / 1024) + "MB").
Dump("Total size of attachments which will be deleted");
foreach (var mail in mailsWithAttachments)
{
foreach (var att in mail.Attachments)
{
((att as Attachment).DisplayName + " - " + ((att as Attachment).Size / 1024) + "KB").Dump();
}
while (mail.Attachments.Count > 0)
{
mail.Attachments.OfType<Attachment>().Last().Delete();
}
mail.Save();
}
Compact and repair PST Files
Before executing these scripts make sure you have made a backup of your PST files in case something goes wrong!
After executing my scripts I always proceed with following 2 steps:
Compact PST files
- Open the Data Files >> Settings >> Personal Folders window.
- Choose Compact now for each PST file.
Repair PST files
- Start the SCANPST.EXE utility which can be found in the Program Files Microsoft Office folder.
- This Inbox Repair Tool will check your PST file for integrity and it will repair corrupt PST files.
I hope that you can keep your Outlook up and running smoothly and that you can take advantage of these C# scripts.