Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SMTP with STARTTLS and/or Authentication

843834Aug 24 2009 — edited Aug 24 2009
Hello,
I'm having trouble with an smtp sender.
I tried both setting authentication and/or starttls, but:
- authentication seem not to be issued if normal smtp is selected, even though I see the call on my Authenticator: the smtp server complains about no authentication.
- starttls seem not to be issued, as the SSL packages complains about a non ssl stream:
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
getPasswordAuthentication: ******** / ********
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "sendm.pec.sonicle.com", port 25, isSSL false
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
Her isSSL is false, because I want to use starttls instead of smtps, and the ssl stack complains...as if starttls was not issued.
Session debugging never show commands about starttls nor authentication.

As you can see from the code below, I need a separate Session with its own properties, because I may have more than one relay class running in the same VM, working with different smtp servers and different options.
Notice also that I run a Transport manually, call connect with no arguments, as they will all be found in properties and from the Authenticator.

The class is to be setup its public properties just after instantiation, then call initialize() to setup its session instance.
Then you can call its createMessage(InputStream data) to create a msg from, e.g., an eml file.
Or you can create the MimeMessage yourself, but beware to use the same session as the Relay on creation.
Finally run sendMessage(MimeMessage msg) to send it.

Where is my error?

Here is my code:

import java.io.InputStream;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
*
* @author gbulfon
*/
public class Relay extends Authenticator {

public String sender;
public String host;
public int port=25;
public String protocol="smtp";
public boolean ssl=false;
public String username;
public String password;
Properties props;
Session session;

public void initialize() {
System.out.println("relay "+host+" uses "+protocol);
props=(Properties)System.getProperties().clone();
props.setProperty("mail."+protocol+".host", host);
props.setProperty("mail."+protocol+".port", ""+port);
if (ssl && protocol.equals("smtp")) {
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
}
if (username!=null) {
System.out.println("relay "+host+" is authenticated as "+username);
props.setProperty("mail."+protocol+".auth", "true");
}
session=javax.mail.Session.getInstance(props,this);
session.setDebug(true);
}

public Session getSession() {
return session;
}

@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("getPasswordAuthentication: "+username+" / "+password);
return new PasswordAuthentication(username,password);
}

public void sendMessage(MimeMessage msg) throws MessagingException {
Transport transport=session.getTransport(protocol);
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}

public MimeMessage createMessage(InputStream data) throws AddressException, MessagingException {
MimeMessage msg=new MimeMessage(session, data);
return msg;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2009
Added on Aug 24 2009
2 comments
641 views