Sample code for 30+ languages & platforms
Java

Send Email using office365 SMTP

Demonstrates how to send an email using Office365.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The mailman object is used for sending and receiving email.
        MailMan mailman = new MailMan();

        // Set the SMTP server to Office365's SMTP server.
        // Note: Please be sure to double-check the SMTP settings
        // with your provider or with the official Office365 documentation
        // provided by Microsoft.  The required settings could change...
        mailman.setSmtpHost("smtp.office365.com");
        mailman.setSmtpPort(587);
        mailman.setStartTLS(true);

        // Set the SMTP login/password
        mailman.setSmtpUsername("OFFICE365_USERNAME");
        mailman.setSmtpPassword("OFFICE365_PASSWORD");

        // Create a new email object
        Email email = new Email();

        email.setSubject("This is a test");
        email.setBody("This is a test");
        email.setFrom("My Name <my_office365_email@example.com>");
        email.addTo("Chilkat Admin", "admin@chilkatsoft.com");

        // Call SendEmail to connect to the SMTP server and send.
        // The connection (i.e. session) to the SMTP server remains
        // open so that subsequent SendEmail calls may use the
        // same connection.
        try {
            mailman.sendEmail(email);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            mailman.closeSmtpConnection();
        } catch (ChilkatException e) {
            System.out.println("Connection to SMTP server not closed cleanly.");
        }

        System.out.println("Mail Sent!");
    }
}