As I said, there is nothing wrong with the code. If anything, just to do some testing, try to drop the authentication part to see if this works:
public void sendPlainTextEmail(String host, String port, final String userName, final String password, String toAddress, String subject, String message) throws AddressException, MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // *** BEGIN CHANGE properties.put("mail.smtp.user", userName); // creates a new session, no Authenticator (will connect() later) Session session = Session.getDefaultInstance(properties); // *** END CHANGE // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); // set plain text message msg.setText(message); // *** BEGIN CHANGE // sends the e-mail Transport t = session.getTransport("smtp"); t.connect(userName, password); t.sendMessage(msg, msg.getAllRecipients()); t.close(); // *** END CHANGE }
So that the code that I use every day sends dozens of letters from my application, and it works 100% guaranteed - while smtp.gmail.com:587 is available, of course.
source share