带有特殊字符的SMTP密码 - utf8 - java邮件 - 编码问题?

时间:2017-09-05 15:21:58

标签: java encoding smtp javamail smtp-auth

我正在尝试通过java.mail SMTP发送电子邮件。我有2个使用相同提供商和设置的邮件帐户:

  1. 使用没有特殊字符的短密码
  2. 使用带有特殊字符的长密码(*> /%!)
  3. 第一个有效。第二个是535 Authentication credentials invalid

    我正在使用java.mail 1.5。

    这是我的代码:

    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("foo@example.org", "$§$&/&%§$!><>");
        }
    });
    
    Transport transport = session.getTransport("smtps");
    transport.connect("smtp.1und1.de", username, password);
    
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("foo@example.org"));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("bar@example.org"));
    message.setSubject("subject");
    message.setText("text");
    
    transport.sendMessage(message, message.getAllRecipients());
    

    我是否需要以某种方式对密码进行编码?

    提前致谢。

    编辑:

    我现在尝试通过telnet登录。同样的问题。所以这与Java无关,但它可能是一些常见的SMTP问题。

    在Mac和iPhone上使用Apple Mail使用电子邮件帐户无任何问题。

1 个答案:

答案 0 :(得分:0)

如果您有非ASCII特殊字符,则需要使用JavaMail 1.6并设置mail.mime.allowutf8会话属性。并且服务器需要支持SMTPUTF8扩展。

相关问题