无法向SMTP主机

时间:2016-02-26 15:34:09

标签: java email 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 [dispatcher]的Servlet.service()与路径的上下文       [/ HomZservice]抛出异常[请求处理失败;嵌套异常       是java.lang.RuntimeException:javax.mail.MessagingException:无法发送
      命令到SMTP主机;       嵌套异常是:       javax.net.ssl.SSLHandshakeException:在执行期间远程主机关闭连接       握手]有根本原因       java.io.EOFException:SSL对等关闭不正确       at sun.security.ssl.InputRecord.read(Unknown Source)       at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)       at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)       在sun.security.ssl.SSLSocketImpl.writeRecord(未知来源)       at sun.security.ssl.AppOutputStream.write(Unknown Source)       at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)       在java.io.BufferedOutputStream.flushBuffer(未知来源)       在java.io.BufferedOutputStream.flush(未知来源)       在com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418)       在com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)       在com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)       at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)       在javax.mail.Service.connect(Service.java:297)       在javax.mail.Service.connect(Service.java:156)       在javax.mail.Service.connect(Service.java:105)       在javax.mail.Transport.send0(Transport.java:168)       在javax.mail.Transport.send(Transport.java:98)       在
      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)       在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)       在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)       at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)       在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)       在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:650)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)       在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)       在java.lang.Thread.run(未知来源)

0 个答案:

没有答案