Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Visual Basic
Article

Backing up MS Outlook emails along with Meta-data

Rate me:
Please Sign up or sign in to vote.
3.22/5 (6 votes)
4 Jun 20062 min read 57K   758   39   14
Scans outlook inbox folder and put emails information in database. Also creates .msg file fore every message in Emails Folder. Finally it moves Email from inbox to inboxBackupFolder.

Sample screenshot Download sourcecode (804 kb)

Sample screenshot

Introduction

Outlook Logger is a console base application that scans your MS Outlook Inbox folder and saves the meta-data of your emails in MS Access database For Example: sender information, Email Subject, Attachments Count, Message sent and Receive time, Message Importance etc. Further it also saves the TO,CC,BCC information for each email in another table.

Application creates an outlook .msg and save i Emails directory. Emails are saved in sub-folders named as recieved date of emails.

Finally it moves Email from inbox to "Personal Folders\InBoxBackup" Folder.

Outlook Redemption has been used to get rid of Security Prompts in  outlook.

Not a tough thing to do!! So move on...

Note: Following Steps to run application
---------------------------------------
1. Register redemption.dll (provided with code) using regsvr32 on your Computer.
2. Create Personal Folders\InBoxBackup Folder in outlook

 

Database Side:

Outlook logger stores the meta-data of emails in 'OutLook_DB.mdb' database that is located in \bin folder. Database contains the following four tables.

1) 'Outlook_logger_tmp' : Temporary Storage of EntryID of all emails in inbox.
2) 'Outlook_logger_message': Store Emails Meta-data as described above.
3) 'Outlook_logger_Recipients': Stores the information of Emails Recipients, as To,Bcc and CC.
4) 'Outlook_logger_Attachments': Attachments data of emails

EntryID is the property of Outlook that uniquely identifies each email (in a folder). I have appended sender email address to apply more uniqueness for example:

0000000094991EDBACA62B41A7E88CDEC57A942824002000-sajid.mahmood@hotmail

Every Email has its own ID and tracked against it. Linked in tables and also the .msg file name.

Functionality
a. NEW EMAILS IDENTIFICATION
-------------------------------
 a) Delete All Recs from outlook_logger_tmp table.
 b) Get EntryID for each email from Inbox in 'Outlook_logger_tmp'.
 c) Compare EntryIDs in outlook_logger_tmp with 'Outlook_logger_message' table and identify New Emails (EntryIDs).
 d) Save new EntryIDs in an ArrayList.

b. Start Processing each Email one by one
----------------------------------------------
 a) Save Emails Meta-deta in outlook_logger_message table.
 a) Save To,CC,BCC to 'OUTLOOK_LOGGER_RECIPIENTS' table.
 b) Check if attachment then save the attachment metadata in 'OUTLOOK_LOGGER_ATTACHMENTS'.
 c) Save Email File
 d) Move Email in backup folder

Lets jump into source Code:

VB.NET
<FONT color=#0000ff></FONT>
VB.NET
<FONT color=#0000ff>

Sub</FONT><FONT size=2> Main()

</FONT><FONT color=#0000ff size=2>Try
</FONT><FONT size=2>
</FONT><FONT color=#008000 size=2>'Outlook Settings
</FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> outlook_Profile </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = "Default Outlook Profile"

oApp = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Outlook.Application()

oNS = oApp.GetNamespace("mapi")

oNS.Logon(outlook_Profile, , </FONT><FONT color=#0000ff size=2>False</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2>)

destinationFolder = CurDir() & "\Emails"

</FONT><FONT color=#008000 size=2>'You must first Create the following folder in MS Outlook manualy
</FONT><FONT size=2>
BackUpFolder = ConfigurationSettings.AppSettings.Get("BackUpFolder")

</FONT><FONT color=#008000 size=2>'Database Configurations
</FONT><FONT size=2>
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurDir() & "\OutLook_DB.mdb" & ";User Id=admin;Password=;"

dbCon = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> OleDbConnection(conString)

dbCon.Open()

</FONT><FONT color=#008000 size=2>'Actual Processing
</FONT><FONT size=2>
mainCode()

</FONT><FONT color=#0000ff size=2>Catch</FONT><FONT size=2> ex </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> System.Exception

Console.WriteLine(ex.Message)

</FONT><FONT color=#0000ff size=2>Catch</FONT><FONT size=2> olkExp </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> COMException

Console.WriteLine(olkExp.Message)

</FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Try
</FONT><FONT size=2>
</FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> dbCon.State = ConnectionState.Open </FONT><FONT color=#0000ff size=2>Then
</FONT><FONT size=2>
dbCon.Close()

</FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If
</FONT><FONT size=2>
oNS.Logoff()

</FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT>
 <FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> mainCode()</P><P></FONT><FONT color=#008000 size=2>'FIRST CHECK FOR DESTINATION FOLDER of Emails</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Not</FONT><FONT size=2> checkDirPath(destinationFolder) </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>Console.WriteLine("Destination directory " & destinationFolder & " - Not Accessable")</P><P></FONT><FONT color=#0000ff size=2>Exit</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Const</FONT><FONT size=2> olFolderInbox = 6</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> inboxFolder </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Outlook.MAPIFolder = oNS.GetDefaultFolder(olFolderInbox)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Redemption.SafeMailItem()</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> inboxItems </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Outlook.Items = inboxFolder.Items</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> rCurrentUser </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Redemption.SafeCurrentUser()</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> currentUserEmailAddr, insertQuery </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Get Current User Email Address</P></FONT><FONT size=2><P>currentUserEmailAddr = rCurrentUser.Address.ToLower</P><P>Console.WriteLine("Started Scanning Emails on : " & currentUserEmailAddr)</P><P></FONT><FONT color=#008000 size=2>'DELETE ALL MAILS EntryIDs DATA (OLD) FROM TEMP TABLE</P></FONT><FONT size=2><P>deleteTmpTable()</P><P></FONT><FONT color=#008000 size=2>'Store Inbox EntryIDs Emails in Tmp Table</P></FONT><FONT size=2><P>loadTmpTable(inboxItems)</P><P>Console.WriteLine("Total " & inboxItems.Count & " emails found in Inbox")</P><P></FONT><FONT color=#008000 size=2>'Store New Emails's EntryIDs </P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> mailIDs </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> ArrayList = getNewEntryIDs()</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> id </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Now Process Emails One by One</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>For</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Each</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>In</FONT><FONT size=2> mailIDs</P><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> inboxItems.Count <> 0 </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>sInboxItem.Item = inboxItems.GetFirst</P><P></FONT><FONT color=#0000ff size=2>Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Exit</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>For</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>While</FONT><FONT size=2> TypeName(sInboxItem.Item) <> "Nothing"</P><P>id = sInboxItem.EntryID & "-" & sInboxItem.SenderEmailAddress</P><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> id = mailID </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> msgRecvDate </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Date</FONT><FONT size=2> = </FONT><FONT color=#0000ff size=2>CDate</FONT><FONT size=2>(sInboxItem.ReceivedTime)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> FolderName </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = DatePart(DateInterval.Year, msgRecvDate) & "-" & DatePart(DateInterval.Month, msgRecvDate) & "-" & DatePart(DateInterval.Day, msgRecvDate)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> destSubFolder </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = destinationFolder & "\" & FolderName</P><P>MkFolder(destSubFolder)</P><P>logMessage(mailID, sInboxItem, currentUserEmailAddr, destSubFolder)</P><P></FONT><FONT color=#008000 size=2>'Save Recipients data</P></FONT><FONT size=2><P>saveRecipientsInfo(mailID, sInboxItem, "TO")</P><P>saveRecipientsInfo(mailID, sInboxItem, "CC")</P><P>saveRecipientsInfo(mailID, sInboxItem, "BCC")</P><P></FONT><FONT color=#008000 size=2>'Save Attachments Meta-Data</P></FONT><FONT size=2><P>saveAttachments(mailID, sInboxItem)</P><P></FONT><FONT color=#008000 size=2>'Save Message</P></FONT><FONT size=2><P>saveMessageFile(mailID, sInboxItem, destSubFolder)</P><P></FONT><FONT color=#008000 size=2>'Move Email to Backup Folder</P></FONT><FONT size=2><P>moveToBackup(sInboxItem)</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P>sInboxItem.Item = inboxItems.GetNext()</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>While</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Next</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT></P> 
VB
<P><FONT color=#0000ff size=2></FONT> </P><FONT color=#0000ff size=2><FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> logMessage(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByRef</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Redemption.SafeMailItem, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> currentUserEmailAddr </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> destSubFolder </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>)</P><P></FONT><FONT color=#008000 size=2>'Description</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'============</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Save the Message meta-data in the database</P></FONT><FONT size=2><P>Console.WriteLine("Processing : " & sInboxItem.Subject & " + " & sInboxItem.SenderEmailAddress)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> subject </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = Replace(sInboxItem.Subject, "'", "''")</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> senderName </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = Replace(sInboxItem.SenderName, "'", "''")</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> senderEmailAddr </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = Replace(sInboxItem.SenderEmailAddress, "'", "''")</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> msgRecvDate </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Date</FONT><FONT size=2> = </FONT><FONT color=#0000ff size=2>CDate</FONT><FONT size=2>(sInboxItem.ReceivedTime)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> abc </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Date</FONT><FONT size=2> = </FONT><FONT color=#0000ff size=2>CDate</FONT><FONT size=2>(sInboxItem.SentOn)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> insertQuery </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P>insertQuery = "insert into outlook_logger_message "</P><P>insertQuery += " (USER_EMAILADDR, MSG_ID, MSG_SENDER_EMAILADDR, MSG_SENDER_NAME, MSG_SUBJECT, "</P><P>insertQuery += " MSG_FILE_NAME, MSG_FILE_PATH, MSG_ATTACHMENT_COUNT,MSG_SEND_DATE, MSG_RECV_DATE, "</P><P>insertQuery += " MSG_SIZE, MSG_IMPORTANCE,LOG_DATE,MSG_READ_FLAG) "</P><P>insertQuery += " values "</P><P>insertQuery += " ('" & currentUserEmailAddr & " ','" & mailID & "','" & senderEmailAddr & "','" & senderName & "','" & subject & "' ,"</P><P>insertQuery += " '" & mailID & ".msg" & "','" & destSubFolder & "'," & sInboxItem.Attachments.Count & ", #" & </FONT><FONT color=#0000ff size=2>CDate</FONT><FONT size=2>(sInboxItem.SentOn) & "#,#" & </FONT><FONT color=#0000ff size=2>CDate</FONT><FONT size=2>(sInboxItem.ReceivedTime) & "#, "</P><P>insertQuery += sInboxItem.size & "," & sInboxItem.Importance & " ,#" & Now() & "#,'N')"</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> cmd </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> OleDbCommand(insertQuery, dbCon)</P><P>cmd.ExecuteNonQuery()</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</P> 
VB
</FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2></FONT> </P><FONT color=#0000ff size=2><P><FONT color=#008000 size=2>'Description</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'============</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Save the Recipients Information in the database along with EntryID/mailID
</FONT>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> saveRecipientsInfo(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByRef</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Redemption.SafeMailItem, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> recipientType </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>)</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> contactsLongStr </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> ArrayList()</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> insertQuery </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> recipientsLine </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> recipientType = "TO" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>recipientsLine = sInboxItem.To</P><P></FONT><FONT color=#0000ff size=2>ElseIf</FONT><FONT size=2> recipientType = "CC" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>recipientsLine = sInboxItem.CC</P><P></FONT><FONT color=#0000ff size=2>ElseIf</FONT><FONT size=2> recipientType = "BCC" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P>recipientsLine = sInboxItem.BCC</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> recipientsLine <> "" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> Contacts </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2> = Replace(recipientsLine, "'", "")</P><P>contactsLongStr = divideContacts(Contacts, 1000)</P><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> contactLine </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> cmd </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> OleDbCommand()</P><P>cmd.Connection = dbCon</P><P></FONT><FONT color=#0000ff size=2>For</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Each</FONT><FONT size=2> contactLine </FONT><FONT color=#0000ff size=2>In</FONT><FONT size=2> contactsLongStr</P><P>insertQuery = "insert into OUTLOOK_LOGGER_RECIPIENTS (" & _</P><P>"MSG_ID, RECIPIENTS_EMAILADDR, TOORCCORBCC " & _</P><P>" ) values ('" & _</P><P>mailID & "','" & contactLine & "','" & recipientType & "')"</P><P>cmd.CommandText = insertQuery</P><P>cmd.ExecuteNonQuery()</P><P></FONT><FONT color=#0000ff size=2>Next</P></FONT><FONT size=2><P>cmd.Dispose()</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub
</FONT></P> 
VB
<P><FONT color=#0000ff size=2> </P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> saveAttachments(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByRef</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Redemption.SafeMailItem)</P><P></FONT><FONT color=#008000 size=2>'Description</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'============</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Saves the attachments Meta-data</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> x </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> insertQuery </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>If</FONT><FONT size=2> sInboxItem.Attachments.Count > 0 </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> cmd </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> OleDbCommand()</P><P>cmd.Connection = dbCon</P><P></FONT><FONT color=#0000ff size=2>For</FONT><FONT size=2> x = 1 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> sInboxItem.Attachments.Count</P><P>insertQuery = "insert into OUTLOOK_LOGGER_ATTACHMENTS (" & _</P><P>"MSG_ID, ATTACHMENT_NAME, ATTACHMENT_SIZE " & _</P><P>" ) values ('" & _</P><P>mailID & "','" & sInboxItem.Attachments(x).FileName & "'," & sInboxItem.Attachments(x).Size & ")"</P><P>cmd.CommandText = insertQuery</P><P>cmd.ExecuteNonQuery()</P><P></FONT><FONT color=#0000ff size=2>Next</P></FONT><FONT size=2><P>cmd.Dispose()</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub
</FONT></P> 
VB
<P><FONT color=#0000ff size=2> </P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> saveMessageFile(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> mailID </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>, </FONT><FONT color=#0000ff size=2>ByRef</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Redemption.SafeMailItem, </FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> destSubFolder </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>)</P><P></FONT><FONT color=#008000 size=2>'Description</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'============</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'a) Stores the Message file (.msg) in the destination Folder</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Folder Name is Emails\{Received Date} </P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'For Example Example : Emails\2005-10-31</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'MSG File Path Example : Emails\2005-10-31\0000000094991EDBACA62B41A7E88CDEC57A942824002000-sajid.mahmood@millips.com.pk.msg</P></FONT><FONT size=2><P>sInboxItem.SaveAs(destSubFolder & "\" & mailID & ".msg")</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</P> 
VB
</FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> moveToBackup(</FONT><FONT color=#0000ff size=2>ByRef</FONT><FONT size=2> sInboxItem </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Redemption.SafeMailItem)</P><P></FONT><FONT color=#008000 size=2>'Description</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'============</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'Move email from inbox to Backup Folder, This helps in fast scanning in future.</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> bkfolder </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Outlook.MAPIFolder</P><P>bkfolder = GetFolder(BackUpFolder)</P><P>sInboxItem.move(bkfolder)</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT></P>

Conclusion

Main theme behind writing this article was to develop an understanding of working with MS Outlook in .Net and use of Outlook Redemption to get rid of security prompts in Outlook, when emails are accessed outside MS-Outook (with service pack installed).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
Author has Master's degree in Computer Science. Worked in VC++ earlier. Now working in .Net technologies.

Some Experience in Multimedia and E-Learning as well. Now a days he is developing software applications for Telecom and mobile sector.

Comments and Discussions

 
Questionsource code print corrupted Pin
hjgode15-Aug-14 19:53
hjgode15-Aug-14 19:53 
Generalhiiiiiiii Pin
sunny12nov19854-Feb-11 19:15
sunny12nov19854-Feb-11 19:15 
GeneralMy vote of 5 Pin
treesprite29-Jan-11 5:35
professionaltreesprite29-Jan-11 5:35 
GeneralThank you Pin
rtchandler252-Dec-09 9:41
rtchandler252-Dec-09 9:41 
GeneralC++ Pin
john563218-Jun-09 18:57
john563218-Jun-09 18:57 
GeneralThank you very much~! Pin
Ruifeng Zhang17-Dec-08 5:29
Ruifeng Zhang17-Dec-08 5:29 
GeneralExcellent Pin
profpaul22-Mar-07 14:23
profpaul22-Mar-07 14:23 
GeneralDatabase script Pin
Max Bogon31-May-06 9:55
Max Bogon31-May-06 9:55 
GeneralRe: Database script Pin
Ehsan Abbasi4-Jun-06 20:24
Ehsan Abbasi4-Jun-06 20:24 
GeneralRe: Database script Pin
Max Bogon5-Jun-06 4:15
Max Bogon5-Jun-06 4:15 
Generalwaste of space Pin
Jonathan [Darka]26-May-06 5:25
professionalJonathan [Darka]26-May-06 5:25 
GeneralRe: waste of space Pin
Ehsan Abbasi28-May-06 19:36
Ehsan Abbasi28-May-06 19:36 
GeneralRe: waste of space Pin
Arne Suki31-Aug-09 21:43
Arne Suki31-Aug-09 21:43 
GeneralRe: waste of space Pin
Jonathan [Darka]31-Aug-09 22:01
professionalJonathan [Darka]31-Aug-09 22:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.