Can't send command to SMTP host
I am not able to send mail. I have tried in multiple way. but still I am getting exception. I am pasting stacktrace and my code over here. Thank you in advance. I am sending test mail from rest ful api. This is my mail code.
@Controller
public class TestSendMail {
@RequestMapping(value = "Customer/mailcheck.htm")
@ResponseBody
public String testMail(HttpServletRequest request) throws Exception, JSONException {
String to = "shah201999@gmail.com";
JSONArray jsonarray = new JSONArray();
JSONObject jsonObject = new JSONObject();
final String from = "skent.qc@gmail.com";
final String password = "12345";//change accordingly
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Sent by ashish shah from live , this is sample for to check send " +
"email using JavaMailAPI ");
Transport.send(message);
jsonObject.put("Status", "Success");
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
jsonObject.put("Status", "Error" + e);
throw new RuntimeException(e);
}
jsonarray.put(jsonObject);
return jsonarray.toString();
}
}
Here is stackTrace.
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/HomZservice] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: javax.mail.MessagingException: Can't send
command to SMTP host; nested exception is: javax.net.ssl.SSLHandshakeException: Remote host closed connection during
handshake] with root cause java.io.EOFException: SSL peer shut down incorrectly at sun.security.ssl.InputRecord.read(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source) at sun.security.ssl.AppOutputStream.write(Unknown Source) at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408) at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384) at javax.mail.Service.connect(Service.java:297) at javax.mail.Service.connect(Service.java:156) at javax.mail.Service.connect(Service.java:105 ) at javax.mail.Transport.send0(Transport.java:168) at javax.mail.Transport.send(Transport.java:98) at
com.houzservice.admin.controller.TestSendMail.testMail(TestSendMail.java:78) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherSe rvlet.java:716) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.Aut henticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)
上一篇: @Pathvariable Map <String,String>给我HTTP状态500
下一篇: 无法将命令发送到SMTP主机