2017-07-14 123 views
-1

如何解決這個問題,下面的代碼是發送電子郵件 - 我得到的錯誤如下,我想要java代碼簡單地發送電子郵件到和從我的Outlook帳戶就是它Java代碼電子郵件不工作要求輸入密碼

DEBUG:setDebug:JavaMail的版本1.4.3

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
javax.mail.AuthenticationFailedException: failed to connect, no password specified? 
    at javax.mail.Service.connect(Service.java:325) 
    at javax.mail.Service.connect(Service.java:172) 
    at javax.mail.Service.connect(Service.java:121) 
    at javax.mail.Transport.send0(Transport.java:190) 
    at javax.mail.Transport.send(Transport.java:120) 
    at JavaMailTest.main(JavaMailTest.java:45) 

failed to connect, no password specified?

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class JavaMailTest { 
    public static void main(String[] args) { 
     String host="host"; 
     final String user="[email protected]";//change accordingly 
     String to="[email protected]";//change accordingly 

     //Get the session object 
     Properties props = new Properties(); 
     props.put("mail.smtp.host",host); 
     props.put("mail.smtp.auth", "false"); 

     Session session=Session.getDefaultInstance(props, null); 
     session.setDebug(true); 

     //Compose the message 
     try { 
      MimeMessage message = new MimeMessage(session); 
      message.saveChanges(); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
      message.setSubject("Test mail"); 
      message.setText("This is test mail."); 

      //send the message 
      Transport.send(message); 

      System.out.println("message sent successfully..."); 
     } 
     catch (MessagingException e) {e.printStackTrace();} 

    } 
} 
+0

可能重複[發送郵件在javax.mail沒有身份驗證](https://stackoverflow.com/questions/19115732/send-mail-in-javax-mail-without-authentication) – Tavo

回答

0

驗證策略由SMTP服務器設置。大多數服務器不允許發送沒有身份驗證。您需要聯繫您的系統管理員來驗證這一點。

+0

究竟在哪裏這是一個錯過? – harveySp

+0

@harveySp我不明白你的評論是什麼意思。 – zuckermanori