Sunday, 15 January 2017

C#: How do I send an email from my owa site?

I recently had the requirement to send out multiple emails from my OWA account. Being a lazy person, I deceided that it would be better to automate the process instead than send it one at a time.

Here is the code I generated to quickly send a test email from the account. The OWA site is https://mail.myowa.com.au/owa.

using Microsoft.Exchange.WebServices.Data;
using System;

namespace Email
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService();
            service.Credentials = new WebCredentials("username", "password", "domain");
            service.Url = new Uri(@"https://mail.myowa.com.au/EWS/Exchange.asmx");

            EmailMessage message = new EmailMessage(service);

            // Add properties to the email message.
            message.Subject = "Test Subject";
            message.Body = new MessageBody()
            {
                BodyType = BodyType.HTML,
                Text = "<b>Test content in bold</b>"
            };
            message.ToRecipients.Add("mytargetaddress@domain.com");

            // Send the email message and save a copy.
            message.SendAndSaveCopy();
        }
    }
}​