2014-12-07 153 views
0

我正在使用JAVAMail API发送邮件。但验证失败。JAVA邮件验证失败

这里是样本输入

private String to = "[email protected]"; 
private String from = "[email protected]"; 
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。我错在哪里?请帮忙。

回答

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); 
+0

感谢。但它给了我错误 - >'无法连接到SMTP主机:smtp.mail.yahoo.com,端口:465,响应:-1' – Junaid 2014-12-07 12:26:20

+1

没有mail.password属性。你可以设置它,但它什么都不做。有关[连接到Yahoo!的示例,请参阅JavaMail FAQ]。 Mail](http://www.oracle.com/technetwork/java/javamail/faq/index.html#yahoomail)以及[调试提示](http://www.oracle.com/technetwork/java/javamail /faq/index.html#condebug)如果你仍然得到这个错误。 – 2014-12-07 20:39:12

+0

尝试使用端口587.我可以通过我的工作配置雅虎smtp(xml配置,但它是同样的事情) – 2014-12-10 10:37:17

0

这里是雅虎SMTP我的工作配置,希望这将有助于:

<property name="host" value="smtp.mail.yahoo.com"/> 
     <property name="port" value="587 "/> 
     <property name="username" value="z********[email protected]"/> 
     <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>