JavaMail sending problem and solution

From: Bobby Martin (
Date: 05/19/04

Date: 19 May 2004 12:16:09 -0700

I had chronic problems with sending email via smtp. I could sign in
via a mail client and send to a remote user just fine, but JavaMail
refused to send to any but local users, citing a 550 relaying problem.
 All of the references I could find on the web told me to talk to my
mail admin, but I'm he and I also knew that if my mail client could do
it, so could I.

This solution requires that you log into the mail server as a normal
email user - you will have to create an (or use an existing) account.
You turn on smtp authorization by setting the a property you pass in
to get a session: the property is mail.smtp.auth and you set it to
"true". Then you create an Authenticator that returns a
PasswordAuthentication with your username and password in it.

Here's an excerpt of working code:

    Authenticator authenticator = new Authenticator() {
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(FROM, PASSWORD);
    Session mailSession = Session.getInstance(getMailProperties(),

    Message email = new MimeMessage(mailSession);
    try {
      email.setFrom(new InternetAddress(FROM));
      email.setSentDate(new Date());

      retval = true;
    } catch (Exception e) {

My getMailProperties includes the following:

I just post this here for posterity so maybe someone else doesn't go
through the hell I did to figure this out :)