使用SMTP发送电子邮件时出错:javax.mail.NoSuchProviderException:无法找到协议的提供者:SMTP

时间:2018-07-20 13:51:54

标签: java smtp

我想使用mail.jarSSLTLS从应用程序发送电子邮件,我尝试使用此代码,但出现此错误:

javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp

         String host="smtp.gmail.com";           
          final String emailSrc="exemple-exemple@gmail.com";
          final String password="*********";
          String emailCible="exemple-exemple2@gmail.com";

           //Get the session object  
           Properties props = new Properties();  
           props.put("mail.smtp.host",host);  
           props.put("mail.smtp.auth", "true");  

           Session session = Session.getDefaultInstance(props,  
            new javax.mail.Authenticator() {  
              protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(emailSrc,password);  
              }  
            });  

           //Compose the message  
            try {  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(emailSrc));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(emailCible));  
             message.setSubject(sendMailRequest.getSubject());  
             message.setText(sendMailRequest.getText());  

            //send the message  
             Transport.send(message);  

             response.setComment("email envoyé");  

             } catch (MessagingException e) {
                 e.printStackTrace();
             }  

        }


        // Send Mail TLS        
        if(sendMailRequest.getModeConection().equals(config.getSendModeTls())){

        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", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailSrc, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(emailSrc));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emailCible));
            message.setSubject(sendMailRequest.getSubject());
            message.setText(sendMailRequest.getText());

            Transport.send(message);

            response.setComment("email envoyé");  

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        }


        // send mail SSL
        if(sendMailRequest.getModeConection().equals(config.getSendModeSsl())){
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");


        props.setProperty("mail.smtp.ssl.enable", "true");
        props.setProperty("mail.smtp.ssl.socketFactory.class",
                        "DummySSLSocketFactory");
        props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailSrc,password);
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(emailSrc));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(emailCible));
            message.setSubject(sendMailRequest.getSubject());
            message.setText(sendMailRequest.getText());

            Transport.send(message);

            response.setComment("Mail envoyé");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        }

org.apache.cxf.interceptor.Fault: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162) at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.createFault(AbstractJAXWSMethodInvoker.java:267) at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128) at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:232) at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:85) at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:74) at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.apache.cxf.interceptor.ServiceInvokerInterceptor$2.run(ServiceInvokerInterceptor.java:126) at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37) at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:131) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:262) at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:299) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:218)

0 个答案:

没有答案