Search code examples
c#asp.net-mvcexchangewebservices

How to get Email ConversationId by EWS after send email


I want to get the email's ConversationId after sending an email. I am using Microsoft exchange web services package for sending an email.

using Microsoft.Exchange.WebServices.Data;

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.UseDefaultCredentials = false;
service.Credentials = new WebCredentials('*****', '*****');
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl(emailExist.Email, RedirectionUrlValidationCallback);

EmailMessage emailMessage = new EmailMessage(service);
emailMessage.From = "[email protected]";
emailMessage.Subject = "testing email";
emailMessage.Body = new MessageBody(BodyType.HTML, "<b>Hello</b> World");

if (!string.IsNullOrEmpty(email.To))
{
  var toArr = email.To.Split(',');
  foreach (var toAddress in toArr)
  {
    emailMessage.ToRecipients.Add(toAddress.Trim());
  }
}

// Send message and save copy by default to sentItems folder
emailMessage.SendAndSaveCopy();

Solution

  • In EWS Sends are asynchronous so you won't get back the Id of the message you just send in the Send Method. If you save the Message as a draft first before sending it that should populate both the Message and CoversationId's eg

                EmailMessage em = new EmailMessage(service);
                em.Subject = "Test"
                em.Save();                
                em.Load();
                Console.WriteLine(em.ConversationId.UniqueId);