我想發送一封電子郵件尤斯的EJB,但我唯一的事情,我得到的回報在此異常:從Java無狀態EJB發送電子郵件(符合Java EE 6)
java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect, no password specified?
這是怎麼了我的EJB看起來像:
@Stateless(name = "ejbs/EmailServiceEJB")
public class EmailServiceEJB extends Authenticator implements IEmailServiceEJB {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]",
"xxxxxxx");
}
public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name) {
// OUR EMAIL SETTINGS
String host = "smtp.gmail.com";// Gmail
int port = 465;
String serviceUsername = "[email protected]";
String servicePassword = "xxxxxxx";// Our Gmail password
Properties props = new Properties();
props.put("mail.smtp.user", serviceUsername);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "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");
// Destination of the email
String to = destinationEmail;
String from = "[email protected]";
// Creating a javax.Session with the our properties
Session session = Session.getInstance(props);
try {
Message message = new MimeMessage(session);
// From: is our service
message.setFrom(new InternetAddress(from));
// To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Comfirm your account");
// Instead of simple text, a .html template should be added here!
message.setText("Welcome....... ");
Transport transport = session.getTransport("smtp");
transport.connect(host, port, serviceUsername, servicePassword);
Transport.send(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我不知道爲什麼它口口聲聲說沒有指定密碼是什麼嗎? SSLSocketFactory與它有關係嗎?我需要在某處調用getPasswordAuthenticion()方法,或者從我的託管bean調用第二個方法是我需要的嗎?
您是否在使用像GlassFish這樣的應用程序服務器? – musiKk
是Glassfish 3.0 – sfrj