2013-03-22 227 views
0
Properties props = new Properties(); 
props.put("mail.smtp.host", "mail.xxx.net"); 
props.put("mail.smtp.port","25"); 
props.put("mail.smtp.ssl.enable", "false"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.debug", "true"); 
props.put ("mail.smtp.starttls.enable", "true"); 
props.put("mail.transport.protocol", "smtp"); 
props.put("mail.smtp.socketFactory.port", "587"); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.socketFactory.fallback", "false"); 
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
Authenticator auth = new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication(){ 
     return new PasswordAuthentication("[email protected]", "xxx"); 
    } 
}; 
Session s = Session.getInstance(props,auth); 
MimeMessage message = new MimeMessage(s); 
InternetAddress from = new InternetAddress("[email protected]"); 
message.setFrom(from); 
InternetAddress to = new InternetAddress("[email protected]"); 
message.addRecipient(Message.RecipientType.TO, to); 
message.setSubject("Test from JavaMail."); 
//message.setText("Hello from JavaMail!"); 
message.setContent("Hello from JavaMail!", "text/plain"); 
message.saveChanges(); 
Transport transport = s.getTransport("smtp"); 
transport.connect("mail.xxx.net","[email protected]","xxx"); 
transport.sendMessage(message,message.getAllRecipients()); 
transport.close(); 

但我得到了一個錯誤:錯誤在通過JSP發送電子郵件(SMTP EXIM)

The server encountered an internal error() that prevented it from fulfilling this request.

堆棧跟蹤:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
    nested exception is: 
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

根源:

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
    nested exception is: 
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840) 
    org.apache.jsp.index_jsp._jspService(index_jsp.java:87) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

當我排除代碼SSL和TSL

& DEBUG結果

DEBUG: JavaMail version 1.4.5 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "mail.xxx.net", port 25, isSSL false 


Mar 23, 2013 8:52:13 AM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [jsp] in context with path [/KKS] threw exception [javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.net, port: 25; 
nested exception is: 
java.net.ConnectException: Connection timed out: connect] with root cause 
java.net.ConnectException: Connection timed out: connect 
+0

爲什麼要在屬性'props.put(「mail.smtp.socketFactory.class」,「javax.net.ssl.SSLSocketFactory」)中添加SSLSocketFactory?'當您連接到SMTP端口25時?端口25通常是普通的套接字,不支持SSL。 – 2013-03-22 17:25:17

+0

你的意思是說我可以禁用 props.put(「mail.smtp.socketFactory.port」,「587」); props.put(「mail.smtp.socketFactory.class」,「javax.net.ssl.SSLSocketFactory」); props.put(「mail.smtp.socketFactory.fallback」,「false」); – Danish 2013-03-22 17:27:11

+0

如果我禁用我得到以下錯誤:javax.net.ssl.SSLException:無法識別的SSL消息,明文連接?]與根本原因 javax.net.ssl.SSLException:無法識別的SSL消息,明文連接? – Danish 2013-03-22 17:29:24

回答

0

正如其他人所說,擺脫套接字工廠的東西。

你會發現很多其他的幫助,在JavaMail FAQ

0

是您的SMTP服務器設置爲允許匿名連接?刪除SSL後,您將auth設置爲false,因此您的客戶端不會將登錄名/密碼傳遞到SMTP服務器。這很好,只要你的SMTP服務器允許匿名連接。

另外,您的網絡/ SMTP服務器是否支持IP6?如果您的客戶端支持IP6,則更高版本的Java默認爲IP6,但許多SMTP服務器都配置爲IP4。請參閱此文章Sending email using JSP瞭解如何讓您的JVM強​​制實施IP4。這需要在JVM實例化時設置。

相關問題