title: Getting error while sending mail in java
body: <p>I was trying to send mail using java. The code i was using worked when I used it with GMail's SMTP server ("smtp.gmail.com"). 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>
  
  <p>Here is my code-</p>
  
  <pre><code>String host = "smtp server host of my isp provider";
  
      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.debug", "true");
      props.put("mail.transport.protocol.", "smtp");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "25");
      props.put("mail.smtp.starttls.enable","true"); 
  
      Session mailSession = Session.getDefaultInstance(props, null);
      mailSession.setDebug(sessionDebug);
  
      Message msg = new MimeMessage(mailSession);
      msg.setFrom(new InternetAddress(from));
      InternetAddress[] address = {new InternetAddress(to)};
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setContent(messageText, "text/html");
  
      Transport transport = mailSession.getTransport("smtp");
      transport.connect(host, user, pass);
  </code></pre>
  
  <p>And i am getting this error-</p>
  
  <pre><code> javax.mail.MessagingException: Exception reading response; 
   nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  </code></pre>
  
  <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...</p>
  
  <p>I followed the steps given below but still i am getting the same error.
  Here is my code now-</p>
  
  <pre><code> String host = "mail.myweb.com", user = emailUser, pass = emailPasswd;
  
      String to = emailID;
      String from = emailFrom;
      String subject = subj;
      String messageText = message;
      boolean sessionDebug = true;
  
      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.debug", "true");
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "25");
  
      Session mailSession = Session.getDefaultInstance(props, null);
      mailSession.setDebug(sessionDebug);
  
      Message msg = new MimeMessage(mailSession);
      msg.setFrom(new InternetAddress(from));
      InternetAddress[] address = {new InternetAddress(to)};
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setContent(messageText, "text/html");
  
      Transport transport = mailSession.getTransport("smtp");
      transport.connect(host, user, pass);
  
      try {
          transport.sendMessage(msg, msg.getAllRecipients());
      }
      catch (Exception e) {
          System.out.println("Error" + e.getMessage());
      }
      transport.close();
  }
  </code></pre>
  
  <p>Here is the full stacktrace</p>
  
  <pre><code> javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
  com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
  com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
  javax.mail.Service.connect(Service.java:288)
  javax.mail.Service.connect(Service.java:169)
  common.sendMail.(sendMail.java:35)
  frontend.reply.sendfeedback(reply.java:74)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  java.lang.reflect.Method.invoke(Method.java:597)
  com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
  com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
  com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
  org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
  org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
  org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
  org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
  org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
  org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
  org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
  org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
  org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
  org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
  org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
  org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
  java.lang.Thread.run(Thread.java:662)
  </code></pre>