JAVA邮件身份验证失败

时间:2014-12-07 11:12:11

标签: java smtp javamail smtp-auth

我正在使用JAVAMail API发送电子邮件。但身份验证失败。

以下是示例输入

private String to = "junaidbinsarfraz@yahoo.com";
private String from = "juniad_rocku@yahoo.com";
private String username = "juniad_rocku";
private String password = "myactualpassword";
private String subject = "My Subject";
private String body = "Please see the attached file";

和代码是

private void emailFile(){

    String host = "relay.jangosmtp.net";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "25");

    // Get the Session object.
    Session session = Session.getInstance(props,
       new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(username, password);
          }
       });

    try {
       Message message = new MimeMessage(session);

       message.setFrom(new InternetAddress(from));

       message.setRecipients(Message.RecipientType.TO,
          InternetAddress.parse(to));

       message.setSubject(this.subject);

       BodyPart messageBodyPart = new MimeBodyPart();

       messageBodyPart.setText(this.body);

       Multipart multipart = new MimeMultipart();

       multipart.addBodyPart(messageBodyPart);

       for(String filePath : UniversityForm.allAttachedFilesPath) {
           if(filePath != null && !(filePath.equals("")))
                this.addAttachment(multipart, filePath);
       }

       message.setContent(multipart);

       Transport.send(message);

       System.out.println("Sent message successfully....");

    } catch (MessagingException e) {
       throw new RuntimeException(e);
    }
}

错误

java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid

我已经提供了良好的用户名,密码...我不知道为什么身份验证失败。我不知道host。我哪里弄错了?请帮忙。

2 个答案:

答案 0 :(得分:0)

Yahoo Mail Server是" smtp.mail.yahoo.com"端口号为465.您忘记添加" mail.password"属性。您需要添加以下附加属性:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtps");
props.put("mail.password", password);

答案 1 :(得分:0)

这是我对yahoo smtp的工作配置,希望它会有所帮助:

<property name="host" value="smtp.mail.yahoo.com"/>
        <property name="port" value="587 "/>
        <property name="username" value="z********e@yahoo.com"/>
        <property name="password" value="**********"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>