0

我正在使用 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。我错在哪里?请帮忙。

4

2 回答 2

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>
于 2014-12-10T10:38:45.183 回答
0

雅虎邮件服务器是“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);
于 2014-12-07T12:06:45.133 回答