--- MailSessionFactory.java 18 Sep 2004 23:07:15 -0000 1.1 +++ MailSessionFactory.java 18 Sep 2004 23:30:25 -0000 1.2 @@ -21,6 +21,9 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; + +import javax.mail.Authenticator; +import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.naming.Name; import javax.naming.Context; @@ -98,7 +101,8 @@ // can read its default properties without throwing Security // exceptions return AccessController.doPrivileged( new PrivilegedAction() { - public Object run() { + public Object run() { + String password = null; // Create the JavaMail properties we will use Properties props = new Properties(); @@ -109,15 +113,37 @@ RefAddr attr = (RefAddr) attrs.nextElement(); if ("factory".equals(attr.getType())) continue; + if ("password".equals(attr.getType())) { + password = (String)attr.getContent(); + continue; + } props.put(attr.getType(), (String) attr.getContent()); } - + + Authenticator auth = null; + + // Check for authentication + if (password != null) { + String user = props.getProperty("mail.smtp.user"); + if (user == null) + user = props.getProperty("mail.user"); + if (user != null) { + final PasswordAuthentication pa = new PasswordAuthentication(user, password); + + auth = new Authenticator(){ + protected PasswordAuthentication getPasswordAuthentication() + { + return pa; + } + }; + } + } // Create and return the new Session object - Session session = Session.getInstance(props, null); + Session session = Session.getInstance(props, auth); return (session); - } - } ); + } + } ); }