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;
}
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