我正在尝试通过smtp发送邮件,但它显示错误消息。 我使用的主机号是578.执行程序后它显示了 无法将套接字转换为TLS;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径。
EmailController.java
package com.jcg.spring.mvc.email;
import java.io.IOException;
import java.io.InputStream;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamSource;
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 = "srinivasaraojella@gmail.com";
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);
// Determine If There Is An File Upload. If Yes, Attach It To The Client Email
if ((attachFileObj != null) && (attachFileObj.getSize() > 0) && (!attachFileObj.equals(""))) {
System.out.println("\nAttachment Name?= " + attachFileObj.getOriginalFilename() + "\n");
mimeMsgHelperObj.addAttachment(attachFileObj.getOriginalFilename(), new InputStreamSource() {
public InputStream getInputStream() throws IOException {
return attachFileObj.getInputStream();
}
});
} else {
System.out.println("\nNo Attachment Is Selected By The User. Sending Text Email!\n");
}
}
});
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.jcg.spring.mvc.email" />
<!-- 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="srinivasaraojella@gmail.com" />
<property name="password" value="srinu877$@" />
<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>
<!-- Spring Email Attachment Configuration -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Maximum Upload Size In Bytes -->
<property name="maxUploadSize" value="20971520" />
<!-- Maximum Size Of File In Memory (In Bytes) -->
<property name="maxInMemorySize" value="1048576" />
</bean>
<!-- Resolves Views Selected For Rendering by @Controllers to *.jsp Resources in the /WEB-INF/ Folder -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Send Email Exception Resolver i.e. In Case Of Exception The Controller Will Navigate To 'error.jsp' & Will Display The Exception Message -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">error</prop>
</props>
</property>
</bean>
</beans>
这样的错误:抱歉,由于以下错误,电子邮件未发送!邮件服务器连接失败;嵌套异常是javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径。失败的消息:javax.mail.MessagingException:无法将套接字转换为TLS;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径
答案 0 :(得分:0)
您的代码非常难看,请使用服务类进行实现。不要在控制器中这样做。 而你的发送电子邮件方法非常奇怪。这样做是这样的:
private void send(String to, String from, String message, String subject, String attachmentName, InputStreamSource inputStreamSource) throws MessagingException {
MimeMessage mail = mailSenderObj.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
if (subject == null) {
subject = "";
}
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(message, true);
helper.addAttachment(attachmentName, inputStreamSource);
mailSenderObj.send(mail);
}
尝试将XML更改为:
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
我认为你不需要设置套接字。 至少在我的春季启动应用程序(当我使用application.properties而不是XML)时,我不需要设置它。