2016-09-22 45 views
0
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class ExchangeSend { 

    public static void main(String[] args) throws MessagingException { 

     String to = "[email protected]"; 
     String cc = "[email protected]"; 
     String msgSubject = "Test"; 

     String msgText = "<i>Greetings!</i><br>"; 
     msgText += "<b>Wish you a nice day!</b><br>"; 
     msgText += "<font color=red>Duke</font>"; 

     //String msgText = "Automated test message from Enlite Admin. Message Succeeded"; 

     ExchangeSend sendM = new ExchangeSend(); 
     sendM.sendMessage(to, cc, msgSubject, msgText); 
    } 

    boolean transactional = false; 

    public void sendMessage(String to, String cc, String msgSubject, String msgText) throws MessagingException { 
     String host = "mail.red.com"; 
     String username = "e.red"; 
     String password = "red1"; 
     String from = "[email protected]"; 
     String port = "25"; 

     Properties props = System.getProperties(); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", port); 
     props.put("mail.smtp.auth", "true"); 

     Transport transport = null; 

     try { 
      Session session = Session.getDefaultInstance(props, null); 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.setContent(message, "text/html; chartset=utf-8"); 

      InternetAddress to_address = new InternetAddress(to); 
      message.addRecipient(Message.RecipientType.TO, to_address); 

      InternetAddress cc_address = new InternetAddress(cc); 
      message.addRecipient(Message.RecipientType.CC, cc_address); 

      message.setSubject(msgSubject); 
      message.setText(msgText); 

      transport = session.getTransport("smtp"); 
      transport.connect(); 
      transport.sendMessage(message, message.getAllRecipients()); 
     } finally { 
      if (transport != null) { 
       try { 
        transport.close(); 
       } catch (MessagingException logOrIgnore) { 
       } 
      } 
     } 
    } 
} 

從上面我貼我能夠與SMTP假髮送文本信息,但不支持任何身份驗證機制當我打開SMTP認證true,我得到這個錯誤:異常線程「main」 javax.mail.AuthenticationFailedException:由服務器和客戶端

Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:829) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730) at javax.mail.Service.connect(Service.java:388)

+0

分析此問題:http://stackoverflow.com/questions/8615730/javamail-with-ms-exchange-no-authentication-mechansims-supported-by-both-server –

回答

0

你的服務器可能希望你在發送你的密碼之前建立一個安全的連接。將mail.smtp.ssl.enable設置爲true。如果這不起作用,請發送JavaMail debug output

此外,請參閱此列表common JavaMail mistakes

+0

比爾,錯誤是與mail.smtp .ssl.enable爲true。假如它工作正常 – manr

+0

好吧,所以如上所述,**發佈調試輸出**。 –