Sending email from java stateless EJB (Java EE-6)

I want to send an email using EJB, but I'm the only thing I get in return for this exception:

java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect, no password specified? 

Here's what my EJB looks like:

 @Stateless(name = "ejbs/EmailServiceEJB") public class EmailServiceEJB extends Authenticator implements IEmailServiceEJB { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(" xxxxxxx@gmail.com ", "xxxxxxx"); } public void sendAccountActivationLinkToBuyer(String destinationEmail, String name) { // OUR EMAIL SETTINGS String host = "smtp.gmail.com";// Gmail int port = 465; String serviceUsername = " xxxxxxx@gmail.com "; 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 = " xxxxxxx@gmail.com "; // 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 do not know why he says that the password is not specified? Does SSLSocketFactory have something with this? Do I need to call the getPasswordAuthenticion () method somewhere, or is the second method call from my managed bean - is that all I need?

+4
source share
2 answers

I don't know why your code is not working, but you can take a look at this:

http://spitballer.blogspot.com/2010/02/sending-email-via-glassfish-v3.html

It shows an alternative method for setting up email in Java EE, which I think works a lot better. Connection details are configured at the container level, just like the database connection managed by the container.

+9
source

Read in SPF and DKIM.

It's hard to understand what is going wrong here, since your code is only 1/2 the necessary information. The real problem is probably that your code is not suitable for your environment, and SPF and DKIM are often the reason that connecting to any public SMTP server and sending email do not work.

At least by reading a little about SPF and DKIM, you can find out if this is a problem in your case, or if it is not a problem in your case.

+1
source

Source: https://habr.com/ru/post/1342851/


All Articles