Using Java With Exchange Server to send emails

In this post I’m going to explain how to send mails through Exchange server using the javax.mail package library , which is a bit more tricky than using other mails servers such as gmail.

The authentication part is the most complicated part of all of this, and here is a really interesting tip, if you try it too many times, your account gets locked and you’d not even know till much later, this actually happened with me and I think i wasted about 2 hours going in circles till i figured out that my account was actually locked, i phoned the helpdesk, 10 minutes later my app was sending mails.

and here is the code:

package sendmail;

package sendmail;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.*;

/**
*
* @author user
*/
public class Mailmanx {

// new MailWithPasswordAuthentication().run();

String body,subject;
public void mail_alist(ArrayList lines,String asubject)
{
//String user, pass, subject, body;

subject=”Alert :\t”+asubject;
body=”START of message\n”;

for(int i=0; i<lines.size(); i++) {
body+=((String)lines.get(i))+"\n";
}
try {
Sendmail(body, subject);
} catch (MessagingException ex) {
Logger.getLogger(Mailmanx.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void Sendmail(String body, String subject) throws MessagingException ////this is used to send the emails
{

Message message = new MimeMessage(getSession());

////test bit
// message.addRecipient(RecipientType.TO, new InternetAddress("TO_mail@corporate.com"));///change to dist list
// message.addFrom(new InternetAddress[] { new InternetAddress("from_mail@corporate.com") });
///end test bit

message.addRecipient(RecipientType.TO, new InternetAddress("to_email@corporate.com"));///change to dist list
message.addFrom(new InternetAddress[] { new InternetAddress("from_mail@corporate.com") });

message.setSubject(subject);
message.setContent(body, "text/plain");

Transport.send(message);
}

private Session getSession() {
Authenticator authenticator = new Authenticator();

Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");

properties.setProperty("mail.smtp.host", "Mail.server.url");
properties.setProperty("mail.smtp.port", "25");

return Session.getInstance(properties, authenticator);
}

private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;

public Authenticator() {
String username = "corp-domain\\user";
String password = "pass";
authentication = new PasswordAuthentication(username, password);
}

protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}

Advertisements

3 thoughts on “Using Java With Exchange Server to send emails

  1. Thanks…
    it is very useful for me..
    but i had done some modification

    properties.setProperty(“mail.smtp.starttls.enable”,”true”);

    String username = “name@corp-domain”;

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