Delete a Message or a List of Messages

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

This method will delete a message based in a Message Unique ID and move it to deleted folders, you can change it for your needs

/// <summary>
/// Deletes the message.
/// </summary>
/// <param name="messId">The mess id.</param>
public static bool DeleteMessageByID(string messId)

{
   
Item mess = Item.Bind(GetBinding(), messId);
   
EmailMessage message = mess as EmailMessage;
   
if (message == null) return false;
   
message.Delete(DeleteMode.MoveToDeletedItems);
   
return true;
}
 

This method will delete a List of messages based in a Message Unique ID that you pass in the List<String> and move it to deleted folders, you can change it for your needs

/// <summary>
/// Deletes the message list.
/// </summary>
/// <param name="messId">The mess id.</param>
public static bool DeleteListOfMessagesByID<string> messId)

{
   
foreach (EmailMessage message in
       
messId.Select(messIdList => Item.Bind(GetBinding(), 
           
messIdList)).Select(mess => mess as EmailMessage))

   
{
       
if (message == null) return false;
       
message.Delete(DeleteMode.MoveToDeletedItems);
   
}
   
return true;
}

 

I you think this helped you, please take 10 second’s and respond to the question in the side bar, Thanks.

Reply and Forward with full information

When you use message.Reply() or message.Forward() you are unable to add extra information, like recipients, attachments etc.., in order to workaround you must use the CraeteReply() and CreateForward() methods.

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

REPLY

/// Replies the message. 
/// <param name="messId">The mess id.</param>
/// <param name="body">The body.</param>
/// <param name="replyAll">if set to <c>true</c> [reply all].</param>
/// <returns></returns>
private bool ReplyMessageCreate(string messId, string body, bool replyAll)

{
   
ExchangeService service = GetBinding();

    try
   
{
       
Item mess = Item.Bind(service, messId);
       
EmailMessage message = mess as EmailMessage;
       
if (message != null)
       
{
           
ResponseMessage reply = message.CreateReply(replyAll);
           
reply.BodyPrefix = body;

            EmailMessage replyMessage = reply.Save(WellKnownFolderName.Drafts);
           
foreach (string item in YourRecipients)
           
{
               
replyMessage.ToRecipients.Add(item);
           
}

            foreach (string item in YourRecipients)
           
{
               
replyMessage.CcRecipients.Add(item);
           
}

            foreach (string item in YourRecipients)
           
{
               
replyMessage.BccRecipients.Add(item);
           
}

            replyMessage.Attachments.AddFileAttachment(c:\att.txt);
        
           
replyMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
           
replyMessage.SendAndSaveCopy();
       
}

        return true;
   
}

    catch (Exception)
   
{
       
return false;
   
}
}

FORWARD

// <summary>
/// Replies the message. 
/// <param name="messId">The mess id.</param>
/// <param name="body">The body.</param>        
/// <returns></returns>
private bool ForwardMessageCreate(string messId, string body)

{
   
ExchangeService service = Bind.GetBinding(Bind._ImpersonateUser);

    try
   
{
       
Item mess = Item.Bind(service, messId);
       
EmailMessage message = mess as EmailMessage;
       
if (message != null)
       
{
           
ResponseMessage forward = message.CreateForward();
           
forward.BodyPrefix = body;

            EmailMessage forwardMessage = forward.Save(WellKnownFolderName.Drafts);
           
foreach (string item in YourRecipients)
           
{
               
forwardMessage.ToRecipients.Add(item);
           
}

            foreach (string item in YourRecipients)
           
{
               
forwardMessage.CcRecipients.Add(item);
           
}

            foreach (string item in YourRecipients)
           
{
               
forwardMessage.BccRecipients.Add(item);
           
}

            forwardMessage.Attachments.AddFileAttachment(c:\att.txt);

            forwardMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
           
forwardMessage.SendAndSaveCopy();
       
}

        return true;
   
}

    catch (Exception)
   
{

        return false;
   
}
}

Get Message by Unique ID

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

FIND IN A SPECIFIC FOLDER OR ENUMERATE

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

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

Read message body with InLine Attachment’s and render it

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

I am catching the Inline attachments and converting to Base64, should be fine for you

using Microsoft.Exchange.WebServices.Autodiscover;
using Microsoft.Exchange.WebServices.Data;
using BodyType = Microsoft.Exchange.WebServices.Data.BodyType;/// <summary>

/// Les the menssagem por id.
/// </summary>
/// <param name="messId">The mess id.</param>
/// <returns></returns>
public static string LeMenssagemPorId(string messId)
{
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
{
    RequestedBodyType = BodyType.HTML
};
Item mess = Item.Bind(GetBinding(), messId, psPropSet);
mess.Body.BodyType = BodyType.HTML;

string sHTMLCOntent = mess.Body.Text;

FileAttachment[] attachments = null;

if (mess.Attachments.Count != 0)
{
    attachments = new FileAttachment[mess.Attachments.Count];
    for (int i = 0; i < mess.Attachments.Count; i++)
    {
        try
        {
            if (mess.Attachments[i].IsInline)
            {
                string sType = mess.Attachments[i].ContentType.ToLower();
                if (sType.Contains("image"))
                {
                    attachments[i] = (FileAttachment)mess.Attachments[i];
                    attachments[i].Load();
                    string sId = attachments[i].ContentId;
                    sType = sType.Replace("image/", "");
                    string oldString = "cid:" + sId;
                    string imagem = 
                        Convert.ToBase64String(attachments[i].Content);
                    sHTMLCOntent = sHTMLCOntent.Replace(oldString, 
                        "data:image/" + sType + ";base64," + imagem);
                }
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("Erro: " + ex.Message);
        }
    }
}

How to reply a message with Outlook Live

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

 /// <summary>
/// Replies the message.
/// </summary>
/// <param name="messId">The mess id.</param>
/// <param name="body">The body.</param>
/// <param name="replyAll">if set to <c>true</c> [reply all].</param>
/// <returns></returns>
public static bool ReplyMessage(string messId, string body, bool replyAll)
{
	Item mess = Item.Bind(GetBinding(), messId);
	EmailMessage message = mess as EmailMessage;
	if (message != null)
	{
		MessageBody messageBody = new MessageBody(BodyType.HTML, body);
		message.Reply(messageBody, replyAll);
		return true;
	}
	return false;
}

Get the Message Date Received

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

/// <summary>
/// Gets the message date received.
/// </summary>
/// <param name="messId">The mess id.</param>
/// <returns></returns>
public static string GetMessageDateReceived(string messId)
{
    Item mess = Item.Bind(GetBinding(), messId);
    return mess.DateTimeReceived.ToShortDateString();
}

Get Attachments from a Message

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

GET ATTACHMENT’S COLLECTION

// Bind to an existing MyMessage item, requesting its 
//Id property plus its attachments collection.
EmailMessage myMessage = EmailMessage.Bind(GetBinding(),
    new ItemId(“Your message unique id”),
        new PropertySet(BasePropertySet.IdOnly,
            ItemSchema.Attachments));

ITERATE THROUGH ATTACHMENT’S COLLECTION

// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in myMessage.Attachments)
{
}

GET THE FILE OR ITEM ATTACHMENT INTO MEMORY

if (attachment is FileAttachment)
{
    //If it's file attachment
    FileAttachment fileAttachment = attachment as FileAttachment;
    //Load the file attachment into memory
    fileAttachment.Load();
}
else // Attachment is an item attachment.
{
    //If it's item attachment 
    ItemAttachment itemAttachment = attachment as ItemAttachment;
    //Load the item attachment into memory
    itemAttachment.Load();
}

Count Read Messages in a Folder using the WellKnownFolderName

In order to use this code you will need to get the GetBinding() method

CODE

/// <summary>
/// Obters the mensages lidas pasta.
/// </summary>
/// <param name="folder">The folder.</param>
/// <param name="pageSize">Size of the page.</param>
/// <returns></returns>
public static int ObterMensagesLidasPasta(WellKnownFolderName folder, int pageSize)
{
    ItemView view = new ItemView(pageSize, 0);
    SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true);

    FindItemsResults<Item> findResults = GetBinding().FindItems(folder, sf, view);
    return findResults.Items.Count;
}

Delete a Message and Move it to Deleted Items

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

CODE – Return True if deleted

/// <summary>
/// Deletes the message.
/// </summary>
/// <param name="messId">The mess id.</param>
public static bool DeleteMessage(string messId)
{
    Item mess = Item.Bind(GetBinding(), messId);
    EmailMessage message = mess as EmailMessage;
    if (message == null) return false;
    message.Delete(DeleteMode.MoveToDeletedItems);
    return true;
}

Get the Message TO Recipients

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

CODE – Return a List for each recipient

/// <summary>
/// Gets the message TO recepients.
/// </summary>
/// <param name="messId">The mess id.</param>
/// <returns></returns>
public static List<string> GetMessageToRecepients(string messId)
{
    Item mess = Item.Bind(GetBinding(), messId);
    EmailMessage message = mess as EmailMessage;
    return message.ToRecipients.Select(to => to.Address).ToList();
}