Send a Message with Recipients and Attachments


You can use in the bellow code the GetBinding() Method in order to pass as ExchangeService

Attachments are the Full Path to file in HDD

Body is HTML or Plain Text

Recipients are valid E-Mails

CODE – Return true if sent

/// <summary>
/// Envia Mensagem.
/// </summary>
/// <param name="service">The service.</param>
/// <param name="subject">The subject.</param>
/// <param name="body">The body.</param>
/// <param name="mailTo">The mail to.</param>
/// <param name="mailCc"></param>
/// <param name="mailBcc"></param>
/// <param name="attachments"></param>
public static bool SendNewMessage(ExchangeService service, 
                  string subject, string body, 
                  List<string> mailTo, 
                  List<string> mailCc, 
                  List<string> mailBcc, 
                  List<string> attachments)
{
    // Create an e-mail message and identify the Exchange service.
    EmailMessage message = new EmailMessage(service)
    {
        /* Subject*/
        Subject = subject, /* Body*/
        Body = new MessageBody(body)
    };

    // Recipients
    foreach (string recipientes in mailTo)
    {
        message.ToRecipients.Add(recipientes);
    }

    // Recipients
    foreach (string recipientes in mailCc)
    {
        message.CcRecipients.Add(recipientes);
    }

    // Recipients
    foreach (string recipientes in mailBcc)
    {
        message.BccRecipients.Add(recipientes);
    }

    // Recipients
    foreach (string att in attachments)
    {
        message.Attachments.AddFileAttachment(att);
    }
    // Send the mail. This makes a trip to the EWS server.
    message.SendAndSaveCopy();
    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();
}