Search code examples
javaencodingsmtpjakarta-mailsmtp-auth

SMTP Password with special characters - utf8 - java mail - encoding issue?


I'm trying to send an email via java.mail SMTP. I have 2 mail accounts using the same provider and settings:

  1. using a short password with no special characters
  2. using a long password with special characters (* > / % !)

The first one works. The second one says 535 Authentication credentials invalid.

I'm using java.mail 1.5.

Here's my code:

Properties props = new Properties();
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.1und1.de");
props.put("mail.smtp.port", "465");

Session session = Session.getInstance(props, new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("[email protected]", "$§$&/&%§$!><>");
    }
});

Transport transport = session.getTransport("smtps");
transport.connect("smtp.1und1.de", username, password);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("subject");
message.setText("text");

transport.sendMessage(message, message.getAllRecipients());

Do I need to encode the password somehow?

Thanks in advance.

EDIT:

I now tried to login via telnet. Same problem. So this isn't related to Java, but it may be some common SMTP issue.

Using the E-Mail account using Apple Mail on Mac and iPhone works without any issues.


Solution

  • If you have non-ASCII special characters, you'll need to use JavaMail 1.6 and set the mail.mime.allowutf8 Session property. And the server needs to support the SMTPUTF8 extension.