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("signup@xxx.net", "xxx");
}
};
Session s = Session.getInstance(props,auth);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress("admin@xxx.net");
message.setFrom(from);
InternetAddress to = new InternetAddress("muhammadanish@gmail.com");
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","signup@xxx.net","xxx");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
但我收到了一个错误:
服务器遇到内部错误(),导致无法完成此请求。
堆栈跟踪:
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: 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 :(得分:0)
正如其他人所说,摆脱插座工厂的东西。
您可以在JavaMail FAQ
中找到许多其他帮助答案 1 :(得分:0)
您的SMTP服务器是否设置为允许匿名连接?删除SSL后,您将auth设置为false,因此您的客户端不会将Login / Password传递给SMTP服务器。这很好,只要您的SMTP服务器允许匿名连接。
此外,您的网络/ SMTP服务器是否支持IP6?如果您的客户端支持IP6,则Java的更高版本默认为IP6,但在IP4上配置了许多SMTP服务器。请参阅此文章Sending email using JSP以获取有关如何配置JVM以强制IP4的说明。这需要在实例化时在JVM上设置。