Read Messages from Inbox

Standard

You will need the GetBind() method in order to use this code

READ MESSAGES THAT ARE NOT READ

CREATE THE ITEM VIEW

The 20 is the number of messages to be retrived

ItemView view = new ItemView(20, 0);
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
FindItemsResults<Item> findResults =
   GetBinding().FindItems(WellKnownFolderName.Inbox, sf, view);

ENUMERATE ITEMS

foreach (Item item in findResults.Items)
{

}

GET THE MESSAGE PROPERTIES

You will have to grab the ITEM UniqueId to read properties for that specific item

Item mess = Item.Bind(GetBinding(), item.Id.UniqueId);
EmailMessage message = mess as EmailMessage;

COMPLETE CODE SAMPLE THAT RETURN A DATATABLE

/// <summary>
/// Gets the inbox received messages.
/// </summary>
/// <param name="pageSize">Size of the page.</param>
/// <returns></returns>
public static DataTable GetInboxReceivedMessages(int pageSize)
{
    string domain = HttpContext.Current.Request.Url.Scheme +
                    Uri.SchemeDelimiter +
                    HttpContext.Current.Request.Url.Host +
                    (HttpContext.Current.Request.Url.IsDefaultPort
                    ? ""
                    : ":" + HttpContext.Current.Request.Url.Port);

    //string dominioCorrente = domain.Substring(0, domain.Length -1);

    ItemView view = new ItemView(pageSize, 0);
    SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead,
        false);
    FindItemsResults<Item> findResults =
       GetBinding().FindItems(WellKnownFolderName.Inbox, sf, view);

    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(string));
    dt.Columns.Add("Assunto", typeof(string));
    dt.Columns.Add("Recebido em", typeof(string));
    dt.Columns.Add("Url", typeof(string));
    dt.Columns.Add("Lida", typeof(string));
    dt.Columns.Add("EnviadaPor", typeof(string));
    dt.Columns.Add("EnviadaPorMail", typeof(string));

    foreach (Item item in findResults.Items)
    {
        Item mess = Item.Bind(GetBinding(), item.Id.UniqueId);
        EmailMessage message = mess as EmailMessage;

        string assunto;
        if (item.Subject.Length > 65)
        {
            assunto = GetFixedLengthString(item.Subject, 65) + "...";
        }
        else
        {
            assunto = item.Subject;
        }

        dt.Rows.Add(item.Id.UniqueId,
                    assunto,
                    item.DateTimeReceived.ToShortDateString(),
                    domain + "/Paginas/DetalhedoCorreio.aspx?ChatId=" +
                        item.Id.UniqueId,
                    message.IsRead ? "1" : "0",
                    message.From.Name,
                    message.From.Address);
    }
    return dt;
}

FIND MESSAGE BY UNIQUE ID

FIND IN A SPECIFIC FOLDER

You must know in which folder is the message, or you can build a method that search in all folders changing the WellKnownFolderName

ItemView view = new ItemView(100, 0);
SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.Id,
  "xxxxxxxxxxxxxxxxx");
FindItemsResults<Item> findResults =
  GetBinding().FindItems(WellKnownFolderName.Inbox, sf, view);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s