mattegol
asked on
How to receive e-mails with asp.net c#
Hello,
I want to pull emails from outlook to an asp.net c# application, process them and send it to a folder or a email-address.
How can this be done? I have no problem with sending an e-mail directly from c#.
I want to pull emails from outlook to an asp.net c# application, process them and send it to a folder or a email-address.
How can this be done? I have no problem with sending an e-mail directly from c#.
The following demostrates how to retreive data from items within an Outlook folder (called "MySubFolderName" under the Inbox folder) using .NET:
First add a reference to the Outlook COM object your project:
In VS.NET right click on References and choose Add Reference.
Select the COM tab
Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select.
Click OK.
Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.O utlook. ... - see definition of variable called 'item' below).
Here's the code:
code Ref: http://geekswithblogs.net/TimH/archive/2006/05/26/79720.aspx
First add a reference to the Outlook COM object your project:
In VS.NET right click on References and choose Add Reference.
Select the COM tab
Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select.
Click OK.
Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.O
Here's the code:
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null,null,false, false);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
for(int i=1;i<=subFolder.Items.Count;i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
Console.WriteLine("Item: {0}", i.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}" item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
Console.WriteLine("Categories: {0}", item.Categories);
Console.WriteLine("Body: {0}", item.Body);
Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}
code Ref: http://geekswithblogs.net/TimH/archive/2006/05/26/79720.aspx
ASKER
Will this "listen" to my mailbox an pull the emails automaticly, or do I have to use a button or something to pull them out?
Thanks
Thanks
Sure, you have initiate some event to do so, alternatively you have to write some window service to manage it
If you want to do on click event then put all the logic in that event. If you want to implement on some form/page loading then add in onload. But if you want to run this as process means in background silently as a service then you have to implement windows service.
Thanks,
Thanks,
ASKER
Ok, I need to implement a windows service to run in the background pulling these emails when they land in my inbox 24/7.
I'll give it a try.
Thanks
I'll give it a try.
Thanks
ok best of luck for your project
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
last post will help you to right windows services
Please go throght following links hope they will help you:
http://www.codeproject.com/KB/IP/despop3client.aspx
http://www.devx.com/tips/Tip/29291
http://www.codeguru.com/cpp/i-n/network/messaging/article.php/c5417
http://support.microsoft.com/kb/310262
Thanks