原创

【Spring MVC】发送邮件JavaMailSender 集成配置

欢迎转载:攻城狮不是猫 求保留连接

文采不行不喜欢废话,之间贴代码(标注红色字体的为重点)

1、xml 配置

<!-- 注册邮件类 -->
<bean id="mailUtils" class="com.wengine.bgmanage.mail.MailUtils"></bean>

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="defaultEncoding" value="${mail.defaultEncoding}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<!--<property name="port" value="${mail.port}" />-->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.starttls.required">${mail.smtp.starttls.required}</prop>
<!--<prop key="mail.transport.protocol">${mail.transport.protocol}</prop>-->
<!--<prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class}</prop>
<prop key="mail.smtp.socketFactory.fallback">${mail.smtp.socketFactory.fallback}</prop>
<prop key="mail.smtp.socketFactory.port">${mail.smtp.socketFactory.port}</prop>-->
<!--<prop key="mail.smtp.port">465</prop>-->
</props>
</property>
</bean>

2、mail.properties 配置

mail.defaultEncoding=UTF-8
mail.host=smtp.163.com
mail.username=xxxx@163.com
mail.password=
mail.smtp.auth=true
mail.smtp.timeout=5000
mail.smtp.starttls.enable=true
mail.smtp.starttls.required=true
mail.port=-1
mail.to=865365737@qq.com

#下面先不配置
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.transport.protocol=smtp
mail.smtp.socketFactory.port=465

3、pom.xml 配置,spring的配置不多说,这里直接粘贴出重点类(不加肯定报错)

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

4、MailUtil 类 (还有可以发送附件的 插入图片的 我这边没用到就没写了) 了解其他方式点击 Springboot 之 JavaMailSender发送电子邮件


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtils {
@Autowired
JavaMailSender javaMailSender;

/**
* 获得配置文件属性
*/
@Value("${mail.username}")
private String mailFrom;
@Value("${mail.to}")
private String mailTo;

/**
* 发送简单邮件
* @param title
* @param text
*/
public void simpleMailMessage(String title, String text){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailFrom);//哪个账号发送一般就是自己配的
message.setTo(mailTo);//发到哪
message.setSubject(title);
message.setText(text);
javaMailSender.send(message);
}

/**
* html邮件
* @param title
* @param text
*/
public void htmlMailMessage(String title, String text){
MimeMessage message = null;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(mailTo);
helper.setSubject(title);
helper.setText(text, true);
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(message);
}

public String getMailFrom() {
return mailFrom;
}

public void setMailFrom(String mailFrom) {
this.mailFrom = mailFrom;
}

public String getMailTo() {
return mailTo;
}

public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}
}

5、调用样例 我的是table

String tabTitle = "<table style=\"width:900px;\" cellpadding=\"2\" cellspacing=\"0\" border=\"1\" bordercolor=\"#000000\"><tbody>\n" +
"<tr><td style=\"text-align: center; background-color: rgb(204, 204, 204);\"> 序号</td>\n" +
"<td style=\"text-align: center; background-color: rgb(204, 204, 204);\"> 姓名</td>\n" +
"<td style=\"text-align: center; background-color: rgb(204, 204, 204);\"> xxx</td>\n" +
"<td style=\"text-align: center; background-color: rgb(204, 204, 204);\">xxxx</td></tr>";

mailUtils.htmlMailMessage("标题", tabTitle + text + "</tbody></table>");

6、最后163有个奇葩异常 javaMail SMTPSendFailedException: 554 DT:SPM 163 smtp4


我一开始标题写的是“测试”,内容写的是“测试”,结果给我报这个异常,百思不得其解,后来百度找的原因如下:

邮件title和content之前写的是test和helloworld等,被网易认为邮件内容不合法。于是就把各项内容按正常的内容填写之后发送就正常使用了;  

正文到此结束
Loading...