2014-12-07 174 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>