我正在尝试通过smtp发送邮件,但它显示错误消息。我正在使用主机号578.after执行程序,它显示无法将套接字转换为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:无法找到请求目标的有效认证路径
您的代码太难看了,请使用服务类来实现。不要在控制器中执行此操作。你发送电子邮件的方法很奇怪。这样做:
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>
我认为你不需要设置插座。至少在我的spring boot应用程序中(当我使用application.properties而不是XML时),我不需要设置它。
我一直在用代表我的office365帐号发送SMTP邮件,工作了大概一个星期,然后突然不工作了,不知道怎么变了。 当我在PHPMailer中启用high debug logging时,我看到: SMTP- 这篇文章似乎是最相关的: 不接受来自服务器的身份验证:504 5.7.4无法识别的身份验证类型 以下是我交给PHPMailer的文字SMTP设置: 以及实际的PHP代码: 作为理智检查,我在中使
我使用的PHPMailer表格在这里找到。 从网站下载的示例是“接触-3”,在引导主题中使用PHPMailer通过gmail发送SMTP电子邮件。当我使用“接触-1”时,它完全适用于我的托管域电子邮件地址,但SMTP版本适用于gmail地址,联系人表单不会提交。 在下面的代码中,我更改了以下行以添加我的gmail地址和gmail密码: 任何关于使用给定信息进行此工作的帮助都将不胜感激-提前感谢!
我通过SMTP服务发送电子邮件时收到以下错误消息: 我的系统上有以下SMTP设置: 操作系统:Windows 7家庭高级版 IIS:IIS 7 如何解决这个问题? 谢谢。
我在ASP. Net Core中工作,并尝试从gmail使用smtp客户端发送电子邮件。有以下代码,但不起作用也看过以下帖子,但不起作用http://dotnetthoughts.net/how-to-send-emails-from-aspnet-core/ 它会跟踪错误 系统NotSupportedException:SMTP服务器不支持身份验证
我正在使用Azure mobile services后端,我可以通过SendGrid成功地发送电子邮件。但是,每次我尝试添加附件时,它都失败了。我从来没有收到过邮件。经过一点研究,我发现我所需要的只是一个虚拟路径。我修改了路径名,但它仍然不工作。 我想不出为什么会失败。 下面是我的代码: