2016-08-23 60 views
0

我正在製作一個將信息發送到我的電子郵件的程序,並且正在從給定的java代碼中引用它。但我得到身份驗證失敗的錯誤。任何人都有想法發送SMTP郵件,而不允許從Gmail和雅虎安全性較低?請幫助我。JavaMail錯誤:javax.mail.AuthenticationFailedException

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import java.util.*; 
import java.io.*; 

public class Emailer 
{ 

    private static final String SMTP_HOST_NAME = "smtp.live.com"; 
    private static final String SMTP_AUTH_USER = "[email protected]"; 
    private static final String SMTP_AUTH_PWD = "spidermonkey"; 

    private static final String emailMsgTxt  = "Online Order Confirmation Message. Also include the Tracking Number."; 
    private static final String emailSubjectTxt = "HELEEEELEQWEWEQ!!!"; 
    private static final String emailFromAddress = "[email protected]"; 

    // Add List of Email address to who email needs to be sent to 
    private static final String[] emailList = {"[email protected]"}; 

    public static void main(String emailBody) throws Exception 
    { 
    Emailer smtpMailSender = new Emailer(); 
    smtpMailSender.postMail(emailList, emailSubjectTxt, emailBody, emailFromAddress); 
    System.out.println("Sucessfully Sent mail to All Users"); 
    } 

    public void postMail(String recipients[ ], String subject, String message , String from) throws MessagingException { 

    boolean debug = false; 
    //Set the host smtp address 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", SMTP_HOST_NAME); 
    props.put("mail.smtp.auth", "true"); 

    Authenticator auth = new SMTPAuthenticator(); 
    Session session = Session.getDefaultInstance(props, auth); 

    session.setDebug(debug); 

    // create a message 
    Message msg = new MimeMessage(session); 

    // set the from and to address 
    InternetAddress addressFrom = new InternetAddress(from); 
    msg.setFrom(addressFrom); 

    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
    for (int i = 0; i < recipients.length; i++) 
    { 
     addressTo[i] = new InternetAddress(recipients[i]); 
    } 
    msg.setRecipients(Message.RecipientType.TO, addressTo); 


    // Setting the Subject and Content Type 
    msg.setSubject(subject); 
    msg.setContent(message, "text/plain"); 
    Transport.send(msg); 
} 


/** 
* SimpleAuthenticator is used to do simple authentication 
* when the SMTP server requires it. 
*/ 
private class SMTPAuthenticator extends javax.mail.Authenticator 
{ 
    private Session createSmtpSession() { 
      final Properties props = new Properties(); 
      props.setProperty("mail.smtp.host", "smtp.live.com"); 
      props.setProperty("mail.smtp.auth", "true"); 
      props.setProperty("mail.smtp.port", "" + 587); 
      props.setProperty("mail.smtp.starttls.enable", "true"); 
      // props.setProperty("mail.debug", "true"); 

      return Session.getDefaultInstance(props, new javax.mail.Authenticator() { 

      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "spidermonkey"); 
      } 
      }); 
     } 
} 

} 

但是,每當我運行此我得到以下錯誤信息:

Exception in thread "main" javax.mail.AuthenticationFailedException: failed to connect, no password specified? 
    at javax.mail.Service.connect(Service.java:329) 
    at javax.mail.Service.connect(Service.java:176) 
    at javax.mail.Service.connect(Service.java:125) 
    at javax.mail.Transport.send0(Transport.java:194) 
    at javax.mail.Transport.send(Transport.java:124) 
    at client.Emailer.postMail(Emailer.java:102) 
    at client.Emailer.main(Emailer.java:67) 
    at client.RemoteFileClient.main(RemoteFileClient.java:28) 
+1

異常顯示問題很清楚,不是嗎? – Rao

+0

如果您使用'smtp.live.com',那麼端口必須是'465'。 –

回答

1

您已經創建

private Session createSmtpSession() 

但你並沒有使用它。

這是您的解決方案。

public void postMail(String recipients[ ], String subject, String message , String from) 
throws MessagingException { 

Session session = null; 

Properties props = new Properties(); 

    props.setProperty("mail.smtp.host", SMTP_HOST_NAME); 
    props.setProperty("mail.smtp.auth", "true"); 
    props.setProperty("mail.smtp.port", "" + 587); 
    props.setProperty("mail.smtp.starttls.enable", "true"); 
    // props.setProperty("mail.debug", "true"); 

    session = Session.getInstance(props, new javax.mail.Authenticator() { 

     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(SMTP_AUTH_USER,SMTP_AUTH_PWD); 
     } 
    }); 

session.setDebug(debug); 

// create a message 
Message msg = new MimeMessage(session); 

// set the from and to address 
InternetAddress addressFrom = new InternetAddress(from); 
msg.setFrom(addressFrom); 

InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
for (int i = 0; i < recipients.length; i++) 
{ 
    addressTo[i] = new InternetAddress(recipients[i]); 
} 
msg.setRecipients(Message.RecipientType.TO, addressTo); 


// Setting the Subject and Content Type 
msg.setSubject(subject); 
msg.setContent(message, "text/plain"); 
Transport.send(msg); 
} 
+0

和[擺脫Authenticator](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)。 –

相關問題