Send java mail with Office365 / EWS

This code works with Office 365.

o365-logo

package org.grassfield.mail;
import microsoft.exchange.webservices.data.*;
import java.net.*;
public class Office365Mail {
public static void main(String[] args) throws Exception {
String body = "";
System.setProperty("javax.net.ssl.keyStore","c:\\Program Files\\Java\\jre8\\lib\\security\\cacerts");
String emailAddress = "pandian@grassfield.org";
String password = "chuVuf5u";
String subject = "eGXchgMailTest";
ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("mail@grassfield.org",password,"grassfield.org");
service.setCredentials(credentials);
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
// service.setTraceEnabled(true);
EmailMessage message = new EmailMessage(service);
body = "this is a body message";
message.setSubject(subject);
message.setBody(MessageBody.getMessageBodyFromText(body));
message.setSubject(subject);
message.getToRecipients().add(emailAddress);
message.send();
}
}

Get the EWS mail jars and dependencies from respective websites.

 

Advertisements

One thought on “Send java mail with Office365 / EWS

  1. Pingback: How to send TLS gmail, EWS, outlook365 email with java mail API | JavaShine

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