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("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();

But I got an error:

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

Stacktrace:

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)

root cause:

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)

When i Exlude Code SSL AND TSL

& DEBUG RESULT IS

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
muhammadanish
  • 437
  • 2
  • 6
  • 20
  • Why are you adding SSLSocketFactory in properties `props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");` when you are connecting to SMTP port 25? Port 25 is usually plain socket and not SSL enabled. – Bimalesh Jha Mar 22 '13 at 17:25
  • you mean to say i may disable props.put("mail.smtp.socketFactory.port", "587"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); – muhammadanish Mar 22 '13 at 17:27
  • if i disable i got following error: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?] with root cause javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? – muhammadanish Mar 22 '13 at 17:29
  • This is probably because your client code is trying to initiate a SSL handshake with SMTP server. But, server is not SSL enabled. Try simple telnet command on port 25 e.g. telnet mail.server 25 and see what happens. If this works and you see some msg means your mail server is not SSL enabled and you need to remove all SSL related attributes in the connection properties. – Bimalesh Jha Mar 22 '13 at 17:32
  • WHEN I TRY TO SEND WITH OUT SSL & TSL here is DEBUG CODE – muhammadanish Mar 23 '13 at 03:53

2 Answers2

0

As others have said, get rid of the socket factory stuff.

You'll find lots of other help in the JavaMail FAQ

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

Is your SMTP server setup to allow Anonymous connections? After you removed SSL, you have auth set to false, so your client will not be passing Login/Password to the SMTP server. That is fine, so long as your SMTP server allows anonymous connections.

Also, does your network/SMTP server support IP6? If your client has IP6 support, later versions of Java default to IP6, but many SMTP servers are configured on IP4. See this article here Sending email using JSP for directions on conguring your JVM to force IP4. This needs to be set on the JVM as it is instantiated.

Community
  • 1
  • 1
Damienknight
  • 1,876
  • 2
  • 18
  • 34