Tuesday, April 04, 2006

Backing up Official Email

Backing up Official Email

I had a necessity to back up all my official emails to a personal mail box to take all the information that I earned with me always. Yes PST (personal folders) is definitely one way of storing them and take them with you. But you always need outlook with you to read them. If you are comfortable with Microsoft and PST you can quit reading this piece. I decided to transfer my emails to Gmail which gives me the storage space I need with the search utility on top with few extra features. Now comes the question, how do you forward all the emails to gmail account?

There is a good tool that is written by Mark Lyon called Gmail Loader (http://www.marklyon.org/gmail/) which is capable of reading all your emails in mBox format and upload them to the Gmail using the APIs exposed by gmail. But unfortunately the PSTs are in pst format and not in open mBox format. Yes, Mark also has faced this problem and he has found some tools which can convert the PSTs to mbox format (http://www.marklyon.org/gmail/gmailapps.htm) which can be later supplied to the Gmail loader. I also did the same, but I got a lot of errors in the conversion process missing lot of emails. Later when I tried to use the uploader I came to know that the Gmail Loader uses a port that is blocked in my company’s firewall. Then I started looking up some alternative ways. I had some little experience in writing simple Macros to do some stuff in Excel sheets using VBA (Visual Basic for Applications). So I started looking at macros for Outlook that can forward emails to my personal ID.

Finally I ended up in writing a Macro which will read all my emails in the personal folder and forward it to gmail. Following is the macro,

Transfer all emails in a folder inside a personal folder

'Sub routine to call the kernel sleep API

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Author : Pradhap Nirmal Natarajan

Sub ScanFolders()

'define the types of variables

Dim ns As NameSpace

Dim Item As Object

Dim i As Integer

Dim addr As String

'this module forwards all emails in a specific personal folder

Set ns = GetNamespace("MAPI")

'grab the personal folder you want to read

'in the code below cognizant is a personal folder inside which the folder Visa is present

Set projArchiveFolder = ns.Folders.Item("Cognizant").Folders.Item("Visa")

For Each Item In projArchiveFolder.Items

'dont forward read receipts for which class property is 46

If Not Item.Class = 46 Then

Set oOldMail = Item

'equivalent to the forward click in outlook

Set oMail = oOldMail.forward

'the recipient

addr = "prathapnirmal+cog.others.Visa@gmail.com"

oMail.Recipients.Add addr

oMail.Recipients.Item(1).Resolve

If oMail.Recipients.Item(1).Resolved Then

oMail.Send

Else

MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name

End If

'sleep for a specific time (use this if you want a time gap between each send)

'Sleep 15000

End If

Next Item

End Sub

Transfer all emails inside all Folders in a Personal Folder

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub ScanFolders()

Dim ns As NameSpace

Dim Item As Object

Dim i As Integer

Dim addr As String

Set ns = GetNamespace("MAPI")

i = 0

Set projArchiveFolder = ns.Folders.Item("Cognizant").Folders.Item("Projects Archive").Folders

For i = 1 To projArchiveFolder.Count

For Each Item In projArchiveFolder.Item(i).Items

'dont forward read receipts

If Not Item.Class = 46 Then

Set oOldMail = Item

Set oMail = oOldMail.forward

addr = "prathapnirmal+cognizant.projects." & projArchiveFolder.Item(i).Name & "@gmail.com"

oMail.Recipients.Add addr

oMail.Recipients.Item(1).Resolve

If oMail.Recipients.Item(1).Resolved Then

oMail.Send

Else

MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name

End If

'Sleep 5000

End If

Next Item

Next i

End Sub

If you look at the code above you will see that the personal ID is a little different. It says prathapnirmal+cog.visa@gmail.com It doesn’t mean that I have a ID like this exclusively for Visa. This is a feature that you can use in the email ids to classify emails. My wish is to move all these emails to a different folder in the mailbox to easily differentiate them as you write rules in outlook. Gmail provides a feature called labeling to do this. So create a filter to move emails to different labels based on the to address. Following is what I got,


The mails are routed to different labels based on to Address. Please write to me at prathapnirmal+questions@gmail.com in case of any questions. Some useful bookmarks at http://del.icio.us/pradhapnirmal/jobexit

3 comments:

Martin G. said...

Very nice idea! Thanks for sharing the code.

For people who are interested in implementing a Gmail-like label system in Outlook: http://web-times.blogspot.com/2006/09/part-iii-web-2.html

Kasthuri said...

Thanks, that was an useful info.

Anonymous said...

Good brief and this post helped me alot in my college assignement. Gratefulness you on your information.