问题描述
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]DEBUG SMTP: useEhlo true, useAuth trueDEBUG SMTP: trying to connect to host "smtp.126.com", port 25, isSSL falseDEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;代码如下JavaMailSenderImpl mailSend=new JavaMailSenderImpl(); mailSend.setDefaultEncoding("UTF-8");//编码 mailSend.setHost(getWebServicePz("shxt.mail.host"));//mail端口协议 mailSend.setPassword(getWebServicePz("shxt.mail.password"));//密码 mailSend.setUsername(getWebServicePz("shxt.mail.username"));//用户名@前面 Properties properties = new Properties(); properties.setProperty("mail.debug", "true");//是否显示调试信息(可选测试用) properties.setProperty("mail.transport.protocol", "smtp");//协议 properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.auth", "true");//设置用户验证 properties.setProperty("mail.smtp.timeout","250000");//超时时间 mailSend.setJavaMailProperties(properties); MimeMessage msg = mailSend.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(msg, false, "UTF-8"); message.setFrom(getWebServicePz("shxt.mail.from")); message.setSubject("上会通知"); //主题 message.setTo(toEmails); //发件人 String htmlTemplate=mailTzTemplate(hyxx,template,hys,yhryLst,ytList);//freemark网页模板 message.setText(htmlTemplate, true); // 如果发的不是html内容去掉true参数 mailSend.send(msg); 问题补充:AngelAndAngel 写道
解决方案
这个是为了安全 做了ssl安全限制的。这个对你发送邮件没什么影响吧,没影响就不用管的。
解决方案二:
真没出现过这样的情况。
解决方案三:
看我的做法:1,applicationContext-mail.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 --><bean id="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host"><value>smtp.126.com</value></property><property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop><prop key="mail.smtp.timeout">25000</prop></props></property><property name="username"><value>你的邮箱</value></property><property name="password"><value>邮箱密码</value></property></bean><bean id="emailUtil" class="org.frame.EmailUtil"><property name="mailSender" ref="mailSender"></property></bean></beans>2,EmailUtil代码如下:package org.frame;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;public class EmailUtil {private JavaMailSender mailSender;public JavaMailSender getMailSender() {return mailSender;}public void setMailSender(JavaMailSender mailSender) {this.mailSender = mailSender;}/** * 发送简单文本邮件(可含多个emails,按':'分割) * * @param emails * @param subject * @param text */public void sentEmails(String emails, String subject, String text) {// 获取JavaMailSender bean// SimpleMailMessage只能用来发送text格式的邮件SimpleMailMessage mail = new SimpleMailMessage();String email[] = emails.split(";");for (int i = 0; i < email.length; i++) {try {mail.setTo(email[i]);// 接受者mail.setFrom("发送者邮箱");mail.setSubject(subject);// 主题mail.setText(text);// 邮件内容mailSender.send(mail);} catch (Exception e) {e.printStackTrace();}}}}希望对你有用。