通过Java发送邮件时出现MessagingException

时间:2018-08-15 09:28:26

标签: java email ssl smtp

尝试从Java发送邮件时遇到以下问题

  

java-javax.mail.MessagingException:无法将套接字转换为TLS。而不是“ props.put(“ mail.smtp.ssl.trust”,“ smtp.gmail.com”)“

我还添加了-props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

控制台中的错误:

javax.mail.MessagingException: Could not convert socket to TLS;     nested exception is:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1880)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at com.synoriq.fm.api.UtilityFacade.sendMail(UtilityFacade.java:274)
    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.directwebremoting.impl.ExecuteAjaxFilter.doFilter(ExecuteAjaxFilter.java:34)
    at org.directwebremoting.impl.DefaultRemoter$1.doFilter(DefaultRemoter.java:428) 
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:431)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:283)
    at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
    at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
    at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    .....

用于发送邮件的Java代码

           //Get the session object  
           Properties props = new Properties(); 
           props.put("mail.smtp.user",user); 
           props.put("mail.smtp.host", host); 
           props.put("mail.smtp.port", "587");
           props.put("mail.debug", "true"); 
           props.put("mail.smtp.auth", "true"); 
           props.put("mail.smtp.starttls.enable","true"); 
           props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
           javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,  
            new javax.mail.Authenticator() {  
              protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(user,password);  
              }  
            });  

            try {  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(user));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
             message.setSubject("javatpoint");  
             message.setText("This is simple program of sending email using JavaMail API");  
             Transport transport = session.getTransport("smtps"); 
             transport.send(message);  

0 个答案:

没有答案