i want to save outlook data into sql database table..like i want to save my contacts into Outlook into sql database Contact table n mail data also...??????????????
The following C# code will Traverse through all the Mails in your InBox,read only unread Mails and get all the necessary Details.You can get all the Details from the mail like subject,sender etc and then u can store it in DB.
Microsoft.Office.Interop.Outlook.MAPIFolder objFolder;
//A Object of Type MailItem,it represents each mail in your MailBox.
Microsoft.Office.Interop.Outlook.MailItem objMail;
Microsoft.Office.Interop.Outlook.Application oApp = new ApplicationClass();
Microsoft.Office.Interop.Outlook.NameSpace oNameSpace = oApp.GetNamespace("MAPI");
Console.WriteLine("Accessing the INbox");
objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Console.WriteLine("Accessing the mails in InBox");
Microsoft.Office.Interop.Outlook.Items oItems;
oItems = objFolder.Items;
for (int i = 0; i < oItems.Count; i++)
{
if (i == oItems.Count - 1)
{
}
else
{
Console.WriteLine("MAil No:" + i);
objMail = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetNext();
Console.WriteLine("Subject:" + objMail.Subject);
if (objMail.UnRead.Equals(true))
{
Console.WriteLine("GEtting the Unread MAils");
objMail.BodyFormat = OlBodyFormat.olFormatHTML;
//Displaying the Mail Subject on the Console.
Console.WriteLine("Mail Subject:" + objMail.Subject);
}
}
}
Console.ReadLine();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
The contacts will be saved in the COntacts folder of your Mail,so u have to access that.
in the code given you have to change a few things like
replace
replace objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
with
objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
and
Replace
Microsoft.Office.Interop.Outlook.MailItem objMail;
with
Microsoft.Office.Interop.Outlook.ContactItem objContact;
pradeepBalon...
Member
21 Points
146 Posts
save outlook data into sql database table
Nov 18, 2009 12:08 PM|LINK
hello..
i want to save outlook data into sql database table..like i want to save my contacts into Outlook into sql database Contact table n mail data also...??????????????
how to connect outllok to sql server???????????
PL reply.............
anooj
Participant
1796 Points
389 Posts
Re: save outlook data into sql database table
Nov 18, 2009 12:38 PM|LINK
Hi,
It's possible through Microsoft.Office.Interop.Outlook Namespace;
i posted a similar thread long time back:-
Plz refer:-
http://forums.asp.net/t/1473336.aspx
The below code worked for me.
The following C# code will Traverse through all the Mails in your InBox,read only unread Mails and get all the necessary Details.You can get all the Details from the mail like subject,sender etc and then u can store it in DB.
Microsoft.Office.Interop.Outlook.MAPIFolder objFolder; //A Object of Type MailItem,it represents each mail in your MailBox. Microsoft.Office.Interop.Outlook.MailItem objMail; Microsoft.Office.Interop.Outlook.Application oApp = new ApplicationClass(); Microsoft.Office.Interop.Outlook.NameSpace oNameSpace = oApp.GetNamespace("MAPI"); Console.WriteLine("Accessing the INbox"); objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Console.WriteLine("Accessing the mails in InBox"); Microsoft.Office.Interop.Outlook.Items oItems; oItems = objFolder.Items; for (int i = 0; i < oItems.Count; i++) { if (i == oItems.Count - 1) { } else { Console.WriteLine("MAil No:" + i); objMail = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetNext(); Console.WriteLine("Subject:" + objMail.Subject); if (objMail.UnRead.Equals(true)) { Console.WriteLine("GEtting the Unread MAils"); objMail.BodyFormat = OlBodyFormat.olFormatHTML; //Displaying the Mail Subject on the Console. Console.WriteLine("Mail Subject:" + objMail.Subject); } } } Console.ReadLine(); } catch (System.Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } }pradeepBalon...
Member
21 Points
146 Posts
Re: save outlook data into sql database table
Nov 19, 2009 04:32 AM|LINK
hello
when i create a contact in outlook..How can i save it in sql database table contact...
anooj
Participant
1796 Points
389 Posts
Re: save outlook data into sql database table
Nov 19, 2009 04:48 AM|LINK
Hi,
The contacts will be saved in the COntacts folder of your Mail,so u have to access that.
in the code given you have to change a few things like
replace
replace objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); with objFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts); and Replace Microsoft.Office.Interop.Outlook.MailItem objMail; with Microsoft.Office.Interop.Outlook.ContactItem objContact;pradeepBalon...
Member
21 Points
146 Posts
Re: save outlook data into sql database table
Nov 19, 2009 05:04 AM|LINK
sorry sir.......i can,t understand..............how i will save data from contact folder in sql data base table....
plz give some exlanation....
anooj
Participant
1796 Points
389 Posts
Re: save outlook data into sql database table
Nov 19, 2009 05:32 AM|LINK
The code will only help you to get the data from the Contacts of your mail.
Then you have to use ADO.Net or LINQ to insert the data into Respective Tables(which is not shown in code).
I hope i make myself clear!!
pradeepBalon...
Member
21 Points
146 Posts
Re: save outlook data into sql database table
Nov 20, 2009 05:39 AM|LINK
Thanks alot
vilhelmi
Member
2 Points
2 Posts
Re: save outlook data into sql database table
Nov 01, 2012 09:06 AM|LINK
Hi,
Is it possible to get mails only from specific address (name@example.com) instead just inbox ?