Display of unread emails in outlook through C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lassi
    New Member
    • Jul 2014
    • 4

    #1

    Display of unread emails in outlook through C#

    Im looking for the process in C# where I give email id and password of any outlook user and to display all unread emails from outlook.

    Example :

    Email id : xyz@abc.com
    Password : cnauhicu

    When I give this email id I need all unread email from that account xyz@abc.com

    Email id : rts@abc.com
    Password : ncauhscua

    When I give this email id I need all unread email from that account rts@abc.com

    Thanks in advance.
  • Praveen07
    New Member
    • Jul 2014
    • 2

    #2
    here you go .. please refer this..



    Regards,
    Praveen Nelge

    Comment

    • lassi
      New Member
      • Jul 2014
      • 4

      #3
      Im unable to make this. Could please check with this.

      I'm looking for the process in C# where I give email id and password of any outlook and to display all unread emails from Outlook.

      Example:

      Email id : xyz@example.com
      Password : xyzpassword

      When I give this email id I need all unread email from that account xyz@abc.com


      Email id : rts@example.com
      Password : rtspassword

      When I give this email id I need all unread email from that account rts@abc.com

      This is what I'm trying:

      I want to give username and password of the Outlook email at the runtime and I would like to access the unread mails of that emails. This works out when I login to my outlook account, but it should be able to access any account of Outlook based on user id and password.

      This code is reading only the first email from outlook but not the unread mail and its not taking user id and password at the runtime.


      Code:
          private void btnAccessEmail_Click(object sender, EventArgs e)
              {
                  Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
                  Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
                  
                  
                  Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
                 
                  //login
                  mapiNameSpace.Logon(null, null, false,false);
      
                  mapiNameSpace.Logon("my Emailid", "my emailid password", Missing.Value, true);
      
      
                  Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;
      
                  Console.WriteLine("Total : ", myItems.Count);
                  
                  Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                  
                  Console.Write(myInbox.Name);
      
                  Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;
      
                  Console.WriteLine("Total : ", inboxItems.Count);
                   
                  Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);
                  
                  mapiNameSpace.Logon("Profile", Missing.Value, false, true);
      
                           
                  
                  if (myInbox.Items.Count > 0)
                  {
      
                    
                     Microsoft.Office.Interop.Outlook.Items items = myInbox.Items;
                     inboxItems = inboxItems.Restrict("[UnRead] = true");
                     Console.WriteLine("Total Unread :", inboxItems.Count);
      
      
      
      
                     //Int32 cnt = 0;
      
      
                     //try
                     //{
                     //    foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
                     //    {
                     //        Console.WriteLine(mail.Subject);
                     //        cnt++;
                     //        if (mail.UnRead == true)
                     //        {
                     //        }
      
                     //    }
                     //}
      
                     //catch
                     //{
      
                     //}
      
                     //Console.WriteLine(cnt);
                     
      
                      // Grab the Subject
                      lblSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
      
      
                     // Grab the Body
                      txtBody.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body;
      
                      // Sender Name
                      lblSenderName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderName;
      
                      // Sender Email
                      lblSenderEmail.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).SenderEmailAddress;
      
                      // Creation date
                      lblCreationdate.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).CreationTime.ToString();
                  }
                  else
                  {
                      MessageBox.Show("There are no emails in your Inbox.");
                  }
              }
          }
      Last edited by Rabbit; Jul 9 '14, 04:58 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      Working...