无法将命令发送到SMTP主机

我无法发送邮件。 我尝试过多种方式。 但我仍然得到例外。 我在这里粘贴stacktrace和我的代码。 先谢谢你。 我正在发送来自rest ful api的测试邮件。 这是我的邮件代码。

@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();
    }
}

这是stackTrace。

SEVERE:Servlet.service()for servlet [dispatcher]与path [/ HomZservice]上下文引发异常[Request processing failed; 嵌套异常是java.lang.RuntimeException:javax.mail.MessagingException:无法发送
命令到SMTP主机; 嵌套异常是:javax.net.ssl.SSLHandshakeException:远程主机在执行期间关闭连接
握手]与根本原因java.io.EOFException:SSL对等关闭不正确地在sun.security.ssl.InputRecord.read(未知源)在sun.security.ssl.SSLSocketImpl.readRecord(未知源)在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 com.un.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 )在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 (未知源)在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)上的org.springframework.web.bind处的java.lang.reflect.Method.invoke(Unknown Source)。在org.springframework.web.servlet.mvc.annotation上的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)上的annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)。 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)位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)处的javax.servlet.http.HttpServlet.service(HttpServlet.java:731)。 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)在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)上的org.apache.catalina(org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) .core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.Aut org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)at henticatorBase.invoke(AuthenticatorBase.java:505) .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.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)

链接地址: http://www.djcxy.com/p/27889.html

上一篇: Can't send command to SMTP host

下一篇: Hibernate Spring Object Wrapper not passing values