当前位置: 首页 > 知识库问答 >
问题:

如何修复“邮件服务器连接失败;嵌套异常为javax.Mail.MessaginException:无法将套接字转换为TLS;”使用Spring mvc?

双志强
2023-03-14

当我试图使用spring MVC和XML配置文件通过web应用程序发送消息时,发生了崩溃。

应用程序似乎可以工作,但当我点击按钮发送消息时,我遇到了以下嵌套异常:

邮件服务器连接失败;嵌套的异常是javax.mail。MessaginException:无法将套接字转换为TLS;嵌套的异常是:javax.net.ssl。SSLHandshakeException:sun.security.validator。ValidatorException:PKIX路径生成失败:sun.security.provider.certpath。SunCertPathBuilderException:无法找到请求目标的有效证书路径。失败的消息:javax.mail。MessaginException:无法将套接字转换为TLS;嵌套的异常是:javax.net.ssl。SSLHandshakeException:sun.security.validator。ValidatorException:PKIX路径生成失败:sun.security.provider.certpath。SunCertPathBuilderException:无法找到请求目标的有效证书路径

我使用tomcat 7和Eclipse来运行这个项目。

我更改了配置中的bean。文件并更改了Gmail的安全配置,但我仍然有同样的问题。

EmailController.java:

package com.jcg.spring.mvc.email;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmailController {

    static String emailToRecipient, emailSubject, emailMessage;
    static final String emailFromRecipient ="XXXXXXXXXXXXXX";

    static ModelAndView modelViewObj;

    @Autowired
    private JavaMailSender mailSenderObj;

    @RequestMapping(value = {"/", "emailForm"}, method = RequestMethod.GET)
    public ModelAndView showEmailForm(ModelMap model) {
        modelViewObj = new ModelAndView("emailForm");
        return  modelViewObj;       
    }

    // This Method Is Used To Prepare The Email Message And Send It To The Client
    @RequestMapping(value = "sendEmail", method = RequestMethod.POST)
    public ModelAndView sendEmailToClient(HttpServletRequest request, final @RequestParam CommonsMultipartFile attachFileObj) {

        // Reading Email Form Input Parameters      
        emailSubject = request.getParameter("subject");
        emailMessage = request.getParameter("message");
        emailToRecipient = request.getParameter("mailTo");

        // Logging The Email Form Parameters For Debugging Purpose
        System.out.println("\nReceipient?= " + emailToRecipient + ", Subject?= " + emailSubject + ", Message?= " + emailMessage + "\n");

        mailSenderObj.send(new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {

                MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");             
                mimeMsgHelperObj.setTo(emailToRecipient);
                mimeMsgHelperObj.setFrom(emailFromRecipient);               
                mimeMsgHelperObj.setText(emailMessage);
                mimeMsgHelperObj.setSubject(emailSubject);

            }
        });
        System.out.println("\nMessage Send Successfully.... Hurrey!\n");

        modelViewObj = new ModelAndView("success","messageObj","Thank You! Your Email Has Been Sent!");
        return  modelViewObj;   
    }
}

spring-servlet.xml:

  <!-- Spring Email Sender Bean Configuration -->

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="XXXXXXXXXXXXX" />
        <property name="password" value="XXXXXXXXXXXXXX" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.debug">true</prop>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <prop key="mail.smtp.socketFactory.port">465</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>

            </props>
        </property>
    </bean>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Spring Mvc Email Example</display-name>

    <!-- Spring Configuration - Processes Application Requests -->
    <servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>   

    <!-- Welcome File List -->
    <welcome-file-list>
        <welcome-file>emailForm.jsp</welcome-file>
    </welcome-file-list>
</web-app>

日志:

DEBUG: JavaMail version 1.5.5
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers

....

DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP l***********.22 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587


EHLO ********PC
250-smtp.gmail.com at your service, [**********]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS

你能帮我解决这个问题吗?

共有1个答案

洪念
2023-03-14

<代码>

更改为

<代码>

 类似资料:
  • 我用Javax发送邮件有问题。我们使用gmail大约4年了,用我们的Java软件发送邮件。现在我得到了以下错误: 以下是我的gmail设置: 我还尝试了其他邮件服务器(O365、Strato),但收到了相同的错误。

  • 我有另一个转换套接字异常。Stackoverflow将我和相关答案联系起来,我已经做了人们建议的所有事情。 这里有异常描述: 线程“main”javax中出现异常。邮政MessaginException:无法将套接字转换为TLS;嵌套的例外是:java。网SocketException:java。安全NoSuchAlgorithmException:在com上构造实现时出错(算法:Default,

  • 我想通过单击JFrame netbeans中的按钮将邮件从我的电子邮件地址发送到另一个电子邮件地址。这是代码, 但它给了我以下错误, javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:java.io.lOException:使用SSL套接字工厂类启动TLS中的异常null: host, portsmtp.gmail.com,587;异常:java.l

  • 我在使用Apache Commons Mail连接到SMTP服务器时遇到问题。我已经尝试了所有方法,但我唯一能在网上找到的是Java邮件。这是我的错误: 原因:javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorExc

  • 我正在使用JavaMail API发送一封包含文本文件的电子邮件。我写的这段代码上周运行良好,但是我今天登录并运行程序并在底部附加的图像中收到错误。我更新到最新版本的java 8,仍然收到此错误。(我必须使用java 8进行项目工作)我查看了此更新的java 8补丁说明,看到根CA证书被删除,我不知道该怎么想/做。我发布这个问题是因为我在其他大量有关此问题的问题中找不到任何解决方案。 我正在运行J

  • 在一个应用程序中,我使用java实现了邮件发送逻辑。我使用了smtp.gmail。com587端口,具有有效的gmail id和密码。在开发环境中,一切正常。但在生产环境中,我需要使用不同的邮件服务器,例如smtp.xyz。在端口25上输入有效的电子邮件id和密码。 当我继续使用以下代码启用SSL时: 我出错了