I want to send an email ussing an EJB but i the only thing i get in return in this exception:
java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect, no password specified?
This is how my EJB looks like:
@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);
}
}
}
I dont know why it keeps saying that the password is not specified? Does the SSLSocketFactory have something to do with it? Do i need to call the getPasswordAuthenticion() method somewhere, or calling the second method from my managed bean is all i need?
I don't know why your code isn't working, but you might want to take a look at this:
http://spitballer.blogspot.com/2010/02/sending-email-via-glassfish-v3.html
It shows an alternate method of configuring email in Java EE, which I think works much better. The connection details are configured at the container level, just like a container-managed database connection.
Read up on SPF and DKIM.
It's hard to know what's going wrong here, as your code is only 1/2 the needed information. The real issue is probably that your code doesn't fit your environment, and SPF and DKIM often is a reason that connecting to any public SMTP server and sending an email doesn't work.
At least after reading a little about SPF and DKIM, you'll be able to know if it is an issue in your case, or if it is not an issue in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With