{"id":6198,"date":"2014-04-13T22:38:57","date_gmt":"2014-04-13T22:38:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/getting-error-while-sending-mail-in-java-collection-of-common-programming-errors\/"},"modified":"2014-04-13T22:38:57","modified_gmt":"2014-04-13T22:38:57","slug":"getting-error-while-sending-mail-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/getting-error-while-sending-mail-in-java-collection-of-common-programming-errors\/","title":{"rendered":"Getting error while sending mail in java-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4e0a256596ade906623407eef14a923d?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBhaskar Sharma<\/p>\n<p>I was trying to send mail using java. The code i was using worked when I used it with GMail&#8217;s SMTP server (&#8220;smtp.gmail.com&#8221;). But when I changed the code (smtp_host_name, smtp_port properties and authentication UserID\/Pwd) to send email through the SMTP server of my ISP provider, this code failed.<\/p>\n<p>Here is my code-<\/p>\n<pre><code>String host = \"smtp server host of my isp provider\";\n\n    Properties props = System.getProperties();\n    props.put(\"mail.smtp.host\", host);\n    props.put(\"mail.debug\", \"true\");\n    props.put(\"mail.transport.protocol.\", \"smtp\");\n    props.put(\"mail.smtp.auth\", \"true\");\n    props.put(\"mail.smtp.port\", \"25\");\n    props.put(\"mail.smtp.starttls.enable\",\"true\"); \n\n    Session mailSession = Session.getDefaultInstance(props, null);\n    mailSession.setDebug(sessionDebug);\n\n    Message msg = new MimeMessage(mailSession);\n    msg.setFrom(new InternetAddress(from));\n    InternetAddress[] address = {new InternetAddress(to)};\n    msg.setRecipients(Message.RecipientType.TO, address);\n    msg.setSubject(subject);\n    msg.setContent(messageText, \"text\/html\");\n\n    Transport transport = mailSession.getTransport(\"smtp\");\n    transport.connect(host, user, pass);\n<\/code><\/pre>\n<p>And i am getting this error-<\/p>\n<pre><code> javax.mail.MessagingException: Exception reading response; \n nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?\n<\/code><\/pre>\n<p>The problem is i am able to send mails from the same code from my system workspace. But after i upload the war file to host, i get the above error. I think i disabled SSL but still getting this error. Can someone please help. Its urgent&#8230;<\/p>\n<p>I followed the steps given below but still i am getting the same error. Here is my code now-<\/p>\n<pre><code> String host = \"mail.myweb.com\", user = emailUser, pass = emailPasswd;\n\n    String to = emailID;\n    String from = emailFrom;\n    String subject = subj;\n    String messageText = message;\n    boolean sessionDebug = true;\n\n    Properties props = System.getProperties();\n    props.put(\"mail.smtp.host\", host);\n    props.put(\"mail.debug\", \"true\");\n    props.put(\"mail.transport.protocol\", \"smtp\");\n    props.put(\"mail.smtp.auth\", \"true\");\n    props.put(\"mail.smtp.port\", \"25\");\n\n    Session mailSession = Session.getDefaultInstance(props, null);\n    mailSession.setDebug(sessionDebug);\n\n    Message msg = new MimeMessage(mailSession);\n    msg.setFrom(new InternetAddress(from));\n    InternetAddress[] address = {new InternetAddress(to)};\n    msg.setRecipients(Message.RecipientType.TO, address);\n    msg.setSubject(subject);\n    msg.setContent(messageText, \"text\/html\");\n\n    Transport transport = mailSession.getTransport(\"smtp\");\n    transport.connect(host, user, pass);\n\n    try {\n        transport.sendMessage(msg, msg.getAllRecipients());\n    }\n    catch (Exception e) {\n        System.out.println(\"Error\" + e.getMessage());\n    }\n    transport.close();\n}\n<\/code><\/pre>\n<p>Here is the full stacktrace<\/p>\n<pre><code> javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?\ncom.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)\ncom.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)\ncom.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)\njavax.mail.Service.connect(Service.java:288)\njavax.mail.Service.connect(Service.java:169)\ncommon.sendMail.(sendMail.java:35)\nfrontend.reply.sendfeedback(reply.java:74)\nsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\nsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\nsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\njava.lang.reflect.Method.invoke(Method.java:597)\ncom.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)\ncom.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)\ncom.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)\ncom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)\norg.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)\ncom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)\ncom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)\ncom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)\ncom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\ncom.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)\ncom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)\norg.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)\norg.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)\norg.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)\norg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\norg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\norg.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)\norg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\norg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\norg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)\norg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)\norg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)\norg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)\norg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)\norg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)\norg.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)\norg.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)\norg.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)\norg.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)\norg.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)\norg.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)\njava.lang.Thread.run(Thread.java:662)\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7afeb4d1993dc25c6028646840e4868a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nErnest Friedman-Hill<\/p>\n<p>This line<\/p>\n<pre><code>props.put(\"mail.smtp.starttls.enable\",\"true\"); \n<\/code><\/pre>\n<p>is enabling SSL on this connection; make it false instead , or just take it out.<\/p>\n<p>Also the dot at the end of this property name is suspicious, although probably innocuous:<\/p>\n<pre><code>props.put(\"mail.transport.protocol.\", \"smtp\");\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b19e60e87fe8aa11b33e9c70797bf821?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmathi<\/p>\n<p>Hi try to add following properties for non ssl smtp connection<\/p>\n<pre><code>    props.put(\"mail.smtp.starttls.enable\",\"false\");\n    props.put(\"mail.smtp.socketFactory.port\", \"25\");\n    props.put(\"mail.smtp.socketFactory.class\", \"\");\n    props.put(\"mail.smtp.socketFactory.fallback\", \"false\");\n<\/code><\/pre>\n<p>for example the <strong>ssl<\/strong> connection properties are<\/p>\n<pre><code>    Properties props = new Properties();\n    props.put(\"mail.smtp.username\", \"email@gmail.com\");\n    props.put(\"mail.smtp.password\", \"password\");\n    props.put(\"mail.smtp.protocol\", \"smtp\");\n    props.put(\"mail.smtp.host\", \"smtp.gmail.com\");\n    props.put(\"mail.smtp.port\", \"587\");\n    props.put(\"mail.smtp.starttls.enable\",\"true\");\n    props.put(\"mail.smtp.auth\", \"true\");\n    props.put(\"mail.smtp.socketFactory.port\", \"587\");\n    props.put(\"mail.smtp.socketFactory.class\", \"javax.net.ssl.SSLSocketFactory\");\n    props.put(\"mail.smtp.socketFactory.fallback\", \"false\");\n<\/code><\/pre>\n<p><strong>Non ssl<\/strong> properties are<\/p>\n<pre><code>    Properties props = new Properties();\n    props.put(\"mail.smtp.user\", \"email@mydomain.com\");\n    props.put(\"mail.smtp.password\", \"password\");\n    props.put(\"mail.smtp.protocol\", \"smtp\");\n    props.put(\"mail.smtp.host\", \"smtp.mydomain.com\");\n    props.put(\"mail.smtp.port\", \"25\");\n    props.put(\"mail.smtp.starttls.enable\",\"false\");\n    props.put(\"mail.smtp.auth\", \"true\");\n    props.put(\"mail.smtp.socketFactory.port\", \"25\");\n    props.put(\"mail.smtp.socketFactory.class\", \"\");\n    props.put(\"mail.smtp.socketFactory.fallback\", \"false\");\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ec5bcacd68dec3dca11f814919f0bfc1?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRavinder<\/p>\n<p>You need to get a <code>Transport<\/code> object with <code>smtps<\/code> protocol. Use Java Mail API version 1.4.1 or later. I am not sure if this was supported in earlier versions.<\/p>\n<p>Each of <code>smtp.jar<\/code> and <code>imap.jar<\/code> contains a resource file named <code>javamail.providers<\/code> with following entries:<\/p>\n<pre><code># JavaMail SMTP provider Sun Microsystems, Inc\nprotocol=smtp; type=transport; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc;\nprotocol=smtps; type=transport; class=com.sun.mail.smtp.SMTPSSLTransport; vendor=Sun Microsystems, Inc;  \n\n# JavaMail IMAP provider Sun Microsystems, Inc\nprotocol=imap; type=store; class=com.sun.mail.imap.IMAPStore; vendor=Sun Microsystems, Inc;\nprotocol=imaps; type=store; class=com.sun.mail.imap.IMAPSSLStore; vendor=Sun Microsystems, Inc;\n<\/code><\/pre>\n<p>These resources are loaded when you create a mail session object. <code>DEBUG<\/code> trace is as follows:<\/p>\n<pre><code>DEBUG: JavaMail version 1.4.5\nDEBUG: URL jar:file:\/E:\/Ravi\/work\/eclipse-workspace\/temp\/WebContent\/WEB-INF\/lib\/smtp.jar!\/META-INF\/javamail.providers\nDEBUG: successfully loaded resource: jar:file:\/E:\/Ravi\/work\/eclipse-workspace\/temp\/WebContent\/WEB-INF\/lib\/smtp.jar!\/META-INF\/javamail.providers\nDEBUG: Tables of loaded providers\nDEBUG: 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 Microsy stems, Inc]}\nDEBUG: 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 Microsy stems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]}\n<\/code><\/pre>\n<p>We need to get an instance of <code>com.sun.mail.smtp.SMTPSSLTransport<\/code> to use <code>smtps<\/code> protocol.<\/p>\n<pre><code>Transport transport = mailSession.getTransport(\"smtps\");\n<\/code><\/pre>\n<p>As the resource has a provider defined for this protocol, <code>Transport<\/code> object is generated without any exception.<\/p>\n<pre><code>DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]\n<\/code><\/pre>\n<p>If all your inputs are correct you would get a success. <code>DEBUG<\/code> on a sample execution is shown below:<\/p>\n<pre><code>DEBUG SMTP: trying to connect to host \"smtp.gmail.com\", port 465, isSSL true\n220 mx.google.com ESMTP d2sm12188996pbw.39\nDEBUG SMTP: connected to host \"smtp.gmail.com\", port: 465\n\nEHLO sreekarreddy\n250-mx.google.com at your service, [117.195.223.189]\n250-SIZE 35882577\n250-8BITMIME\n250-AUTH LOGIN PLAIN XOAUTH\n250 ENHANCEDSTATUSCODES\nDEBUG SMTP: Found extension \"SIZE\", arg \"35882577\"\nDEBUG SMTP: Found extension \"8BITMIME\", arg \"\"\nDEBUG SMTP: Found extension \"AUTH\", arg \"LOGIN PLAIN XOAUTH\"\nDEBUG SMTP: Found extension \"ENHANCEDSTATUSCODES\", arg \"\"\nDEBUG SMTP: Attempt to authenticate\nDEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM \nDEBUG SMTP: AUTH LOGIN command trace suppressed\nDEBUG SMTP: AUTH LOGIN succeeded\nDEBUG SMTP: use8bit false\nMAIL FROM:\n250 2.1.0 OK d2sm12188996pbw.39\nRCPT TO:\n250 2.1.5 OK d2sm12188996pbw.39\nDEBUG SMTP: Verified Addresses\nDEBUG SMTP:   rrarepally@gmail.com\nDATA\n354  Go ahead d2sm12188996pbw.39\nFrom: rrarepally@gmail.com\nTo: rrarepally@gmail.com\nMessage-ID: \nSubject: test email over smtps\nMIME-Version: 1.0\nContent-Type: text\/html; charset=us-ascii\nContent-Transfer-Encoding: 7bit\n\nhello world\n.\n250 2.0.0 OK 1335124739 d2sm12188996pbw.39\nQUIT\n221 2.0.0 closing connection d2sm12188996pbw.39\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Bhaskar Sharma I was trying to send mail using java. The code i was using worked when I used it with GMail&#8217;s SMTP server (&#8220;smtp.gmail.com&#8221;). But when I changed the code (smtp_host_name, smtp_port properties and authentication UserID\/Pwd) to send email through the SMTP server of my ISP provider, this code failed. Here is my code- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6198","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6198"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6198\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}