none
Refer to latest emails in Inbox RRS feed

  • Question

  • Hi,

    How to adjust following codes, to take the latest Inbox Emails, per given Outlook account (to my Email server)?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Outlook;
    namespace OutlookEmailAccess
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    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 myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    if (myInbox.Items.Count > 0)
    {
    // Grab the Subject
    lblSubject.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Subject;
    //Grab the Attachment Name
    if (((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments.Count > 0)                
    {
    lblAttachmentName.Text = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Attachments[1].FileName;
    }
    else
    {
    lblAttachmentName.Text = "No Attachment";
    }
    // 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.");
    }
    }
    }
    }



    Many Thanks & Best Regards, Hua Min

    Wednesday, December 20, 2017 5:09 AM

Answers

  • You can't - Logon takes the name of an existing profile as shown in Control Panel | Mail | Show Profiles. If you pass an empty string, it uses the default profile.

    You cannot dynamically connect to an arbitrary server, you need to create a profile first, either manually or programmatically using Extended MAPI (C++ or Delphi only). If using Redemption (it wraps Extended MAPI) is an option, it can connect to any Exchange mailbox using RDOSession.LogonHostedExchangeMailbox. Or you can explicitly build a profile (not for the fainthearted) using ProfMan.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, December 22, 2017 3:21 AM

All replies


  • Items in a folder are not sorted in any particular way until you explicitly call Items.Sort. You also need to make sure you avoid multiple dot notation (myInbox.Items[1]) so that you sort and later access the same Items collection instead of the new instance returned every time you read the MAPIFolder.Items property.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, December 20, 2017 6:03 PM
  • Hi,

    Any example to refer to myInbox.Items[1]?


    Many Thanks & Best Regards, Hua Min

    Thursday, December 21, 2017 1:53 AM
  • set items = myInbox.Items
    items.Sort "[ReceivedTime]", false
    set oldestEmail  = items(1)
    Msgbox oldestEmail.Subject


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, December 21, 2017 3:48 AM
  • Sorry, do you have also the details to connect to Outlook Email server, using relevant account?



    Many Thanks & Best Regards, Hua Min

    Thursday, December 21, 2017 8:30 AM
  • Hello,

    I think you could use _NameSpace.Logon method

    Please visit How to: Get and Log On to an Instance of Outlook

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 21, 2017 9:53 AM
    Moderator
  • Don't you already do that in your code above?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, December 21, 2017 1:39 PM
  • Hello,

    I think you could use _NameSpace.Logon method

    Please visit How to: Get and Log On to an Instance of Outlook

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks to all.

    Celeste,

    Sorry, I have IP of current Email server, and how to use Namespace Logon to get into it?


    Many Thanks & Best Regards, Hua Min

    Friday, December 22, 2017 3:03 AM
  • You can't - Logon takes the name of an existing profile as shown in Control Panel | Mail | Show Profiles. If you pass an empty string, it uses the default profile.

    You cannot dynamically connect to an arbitrary server, you need to create a profile first, either manually or programmatically using Extended MAPI (C++ or Delphi only). If using Redemption (it wraps Extended MAPI) is an option, it can connect to any Exchange mailbox using RDOSession.LogonHostedExchangeMailbox. Or you can explicitly build a profile (not for the fainthearted) using ProfMan.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, December 22, 2017 3:21 AM
  • Hello

    Could you get latest mailitem for inbox? 

    i suggest you mark helpful post as answer to close this thread. Please post new threads if you have any new issues.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jackson_1990 Friday, December 22, 2017 11:08 AM
    • Unmarked as answer by Jackson_1990 Friday, December 22, 2017 1:08 PM
    Friday, December 22, 2017 6:25 AM
    Moderator
  • Hi,

    Thanks to all.


    Is CoCreateInstance the way to link to Email server or not?



    Many Thanks & Best Regards, Hua Min


    Friday, December 22, 2017 1:09 PM
  • It is not - CoCreateInstance is a function to create a particular COM object. It has absolutely nothing to do with any email servers.

    Is this a new question? If your original question has been answered, mark the appropriate reply as an answer and start a new thread.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, December 22, 2017 1:38 PM
  • It is not - CoCreateInstance is a function to create a particular COM object. It has absolutely nothing to do with any email servers.

    Is this a new question? If your original question has been answered, mark the appropriate reply as an answer and start a new thread.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Hi,

    Is there one example to access Outlook latest Inbox RTF EMAIL item using Outlook redemption?


    Many Thanks & Best Regards, Hua Min

    Wednesday, December 27, 2017 5:07 AM
  • Sorry to that I also want to have the clear example to refer to Outlook redemption.

    Many Thanks & Best Regards, Hua Min

    Wednesday, December 27, 2017 9:06 AM
  • Hi HuaMin,

    If your original issue related has been resolved, I would suggest you mark the helpful reply as answer.

    >> I also want to have the clear example to refer to Outlook redemption.

    For this new issue, since it is related with redemption which is not Microsoft Product, please contact Dmitry by link below for help.

    #http://www.dimastr.com/redemption/contact.htm

    Note agaion, it is recommened to discuss one issue in one thread.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 27, 2017 9:13 AM
  • Dmitry,

    Could you please show with more details to Outlook redemption?


    Many Thanks & Best Regards, Hua Min

    Thursday, December 28, 2017 4:41 AM
  • Tao,
    Is there any similar way (like Outlook Redemption), to achieve, within Visual studio?

    Many Thanks & Best Regards, Hua Min

    Tuesday, January 2, 2018 3:18 AM
  • Hi,

    Since your original issue related with refering to latest emails has been resolved, I would suggest you mark the solution as answer to close current thread.

    For the new requirement, I would suggest you post a new thread, and share us your detail requirement.

    If there is any unclear in my post, please feel free to let me know.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 2, 2018 5:42 AM