JAVA Mail settings for SMTP Print

  • 252

The mail server most likely resides on the same server that hosts your site so you will need to use 'localhost' as your SMTP server or instead of localhost use the server hostname. All our inexpensive Java hosting plans have a mail server running on localhost. Session session = Session.getInstance( properties,new SMTPAuthenticator(properties)); The properties object (Properties class) should have similar properties as the example below. For more detailed information, please refer to javamail docs: https://javaee.github.io/javamail/

  mail.transport.protocol=smtp
  mail.smtp.starttls.enable=false
  mail.smtp.host=localhost
  mail.smtp.auth=true
  mail.smtp.user=username@domain.com
  mail.smtp.password=password
  mail.smtp.from=username@domain.com
The SMPTAuthenticator is as follows:
private class SMTPAuthenticator extends javax.mail.Authenticator {
  private String user;
  private String pass;
SMTPAuthenticator (Properties properties){
  this.user = properties.getProperty("username@yourdomain.com");
  this.pass = properties.getProperty("password");
  }
public PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(this.user,this.pass);
  }
  
  }
  

Connecting remotely using Spring Boot

spring:
  mail:
    host: jpcloud.server.hostname
    port: 587
    username: user@yourdomain.com
    password: email_password
    properties.mail.smtp.auth: true
    properties.mail.smtp.starttls.enable: true 

Troubleshooting

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, your envelope sender has been denied (#5.7.1)
    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:2064)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1286)

The above error is likely due to missing 'from' email address.


Was this answer helpful?

« Back