Search code examples
javaspringjakarta-mailtransport

Transport not work for sending smtp email in java


I am using :

transport.connect(getHost(), getPort(), getUsername(), getPassword()); 

to send email, but it always gives me the following exception:

class com.sun.mail.smtp.SMTPAddressFailedException: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

But actually I have provided the username and password above, and the username and password is right as I tested in thunderbird, it can send email well.

So what's my problem ? Please point me the right direction. Thanks


Solution

  • When creating the javax.mail.Session, be sure the given properties contain:

    props.put("mail.smtp.auth", "true");
    

    http://www.oracle.com/technetwork/java/javamail/faq/index.html#smtpauth