Visual Studio .NET - how Retreive Emails from a inbox using asp.net 2.0

Asked By amit j on 26-Dec-08 06:12 AM

Hi,

Is there any way that i can get the inbox from my outlook server to a web page ? Just like the way i can send a mail from my code, i want to display all the mails in a web page created by me? is it possible?

Thanks

re

Web Star replied to amit j on 26-Dec-08 06:16 AM

try this code

C_A P replied to amit j on 26-Dec-08 06:40 AM
Imports System.Reflection
'// Reference to Microsoft Outlook 11.0 Object Library
Imports Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()
' Create Outlook application.
Dim oApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application

' Get Mapi NameSpace.
Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.
Dim oInbox As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
Dim oItems As Microsoft.Office.Interop.Outlook.Items = oInbox.Items
Console.WriteLine("Total : " & oItems.Count)

' Get unread e-mail messages.
oItems = oItems.Restrict("[Unread] = true")
Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.
Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem
Dim i As Integer

For i = 1 To oItems.Count
oMsg = oItems.Item(i)

Console.WriteLine(i)
Console.WriteLine(oMsg.SenderName)
Console.WriteLine(oMsg.Subject)
Console.WriteLine(oMsg.ReceivedTime)
Console.WriteLine(oMsg.Body)
Console.WriteLine("---------------------------")
Console.WriteLine("press any key to continue")
Next

' Log off.
oNS.Logoff()

' Clean up.
oApp = Nothing
oNS = Nothing
oItems = Nothing
oMsg = Nothing
End Sub

End Module

try this link

C_A P replied to amit j on 26-Dec-08 06:40 AM

http://support.microsoft.com/default.aspx?scid=kb;en-us;313795

 

how to retreive Emails from a inbox using asp.net 2.0
mv ark replied to amit j on 26-Dec-08 09:30 PM
As long as you know the credentials of you SMTP & POP3 servers (which you need to configure Outlook), you can send & receive mails programmatically with C#

To recieve mails you will need to use POP3. There are no readymade classes available in .NET but you can use an open source like Indy.Sockets or a commercial component like aspNetPOP3.
Check these links for more info -
http://www.codeproject.com/KB/IP/POP3ClientByChadZHower.aspx
http://www.aspnetpop3.com/overview.aspx
http://www.codeproject.com/KB/IP/Pop3MailClient.aspx
http://aspalliance.com/240_POP3_Email.all

SharpWebMail is an open source ASP.NET webmail application written in C# -  http://anmar.eu.org/projects/sharpwebmail/

Once you know your SMTP & POP3 servers addresses, you can configure a readymade interface

Re :: Retrieve Emails from a Inbox using Asp.net 2.0
Shailendrasinh Parmar replied to amit j on 26-Dec-08 10:59 PM

Hello Amit,

This article describes how to use Microsoft Outlook 10.0 Object Library to retrieve unread messages from the Outlook Inbox in Visual Basic .NET.

http://support.microsoft.com/default.aspx?scid=kb;en-us;313795#top

Create Sample to Retrieve Unread Messages from the Inbox

loadTOCNode(2, 'summary');
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft Outlook 10.0 Object Library, and then click Select
    4. Click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    Imports System.Reflection
    Module Module1
        Sub Main()
           ' Create Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application()
            ' Get Mapi NameSpace.
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
            oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:
            ' Get Messages collection of Inbox.
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            Dim oItems As Outlook.Items = oInbox.Items
            Console.WriteLine("Total : " & oItems.Count)
            ' Get unread e-mail messages.
            oItems = oItems.Restrict("[Unread] = true")
            Console.WriteLine("Total Unread : " & oItems.Count)
            ' Loop each unread message.
            Dim oMsg As Outlook.MailItem
            Dim i As Integer
            For i = 1 To oItems.Count
                oMsg = oItems.Item(i)
                Console.WriteLine(i)
                Console.WriteLine(oMsg.SenderName)
                Console.WriteLine(oMsg.Subject)
                Console.WriteLine(oMsg.ReceivedTime)
                Console.WriteLine(oMsg.Body)
                Console.WriteLine("---------------------------")
            Next
            ' Log off.
            oNS.Logoff()
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oItems = Nothing
            oMsg = Nothing
        End Sub
    End Module
    					
  6. Modify the code where you see the TODO comments.
  7. Press F5 to build and to run the application.
  8. Verify that the unread messages are retrieved.

Hope this helps.

Reply
Binny ch replied to amit j on 28-Dec-08 06:31 AM

Tp diaplay the inbox:

public static void DisplayInbox(ApplicationClass o)
{
  // Get items in my inbox.
  NameSpace outlookNS = o.GetNamespace("MAPI");
  MAPIFolder inboxFolder
    = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
  // Print out some basic info.
  Console.WriteLine("You have {0} e-mails.",
    inboxFolder.Items.Count);
  Console.WriteLine();
  foreach(object obj in inboxFolder.Items)
  {
    MailItem item = obj as MailItem;
    if(item != null)
    {
      Console.WriteLine("-> Received: {0}",
        item.ReceivedTime.ToString());
      Console.WriteLine("-> Sender: {0}", item.SenderName);
      Console.WriteLine("-> Subject: {0}", item.Subject);
      Console.WriteLine();
    }
  }
}