版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LDY1016/article/details/78365072
最近做的一个项目中有个邮件发送功能,发送邮件默认是25端口,但是公司用的服务器是阿里云的,会屏蔽25端口,所以部署到服务器后连接不上邮件服务器,需要使用SSL协议(使用465端口)。
需要注意的是Spring Boot 的版本问题,低版本只支持使用默认的25端口进行发送,或者使用SSL协议时的配置有所区别,我的Spring Boot 的版本信息最开始是1.3.0.RELEASE,在开发完成后测试的时候发现使用465端口死活连不上邮件服务器,只能通过25端口发送,对比了以前我做的项目,发现只有boot的版本不一样,于是我修改了一下版本信息,修改后如下所示:
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>1.4.7.RELEASE</version>
-
</parent>
代码实现细节如下
一、添加maven依赖
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-mail</artifactId>
-
</dependency>
二、添加配置信息
-
#邮件配置
-
spring.mail.host=邮件服务器地址
-
spring.mail.username=用户名
-
spring.mail.password=密码
-
spring.mail. default-encoding=UTF-8
-
#登录服务器是否需要认证
-
spring.mail.properties.mail.smtp.auth= true
-
#SSL证书Socket工厂
-
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
-
#使用SMTPS协议465端口
-
spring.mail.properties.mail.smtp.socketFactory.port= 465
-
#显示在收件人邮箱里面的发件人名称
-
spring.mail. self.username=用户名
三、Java代码
-
import java.io.File;
-
import java.util.List;
-
import javax.mail.MessagingException;
-
import javax.mail.internet.InternetAddress;
-
import javax.mail.internet.MimeMessage;
-
import javax.mail.internet.MimeMessage.RecipientType;
-
import org.apache.commons.lang3.StringUtils;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.mail.javamail.JavaMailSender;
-
import org.springframework.mail.javamail.MimeMessageHelper;
-
import org.springframework.stereotype.Service;
-
/**
-
* @类名: MailSender<br>
-
* @描述: 邮件发送<br>
-
*/
-
-
public class MailSendService {
-
-
JavaMailSender mailSender;
-
//发件人名称设置
-
-
private String sendUser;
-
public void sendEmail(String toMail, String title, String content) throws MessagingException {
-
final MimeMessage mimeMessage = mailSender.createMimeMessage();
-
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
-
message.setFrom(sendUser);
-
message.setTo(toMail);
-
message.setSubject(title);
-
message.setText(content);
-
mailSender.send(mimeMessage);
-
}
-
/**
-
* @方法名: sendSimpleMail<br>
-
* @描述: 发送普通文本格式的邮件<br>
-
* @param toMail 收件人,多个用英文格式逗号分隔
-
* @param replyTo 抄送人,多个用英文格式逗号分隔
-
* @param title 邮件主题
-
* @param content 邮件内容
-
* @param flies 附件
-
* @throws Exception
-
*/
-
public void sendSimpleMail(String toMail, String replyTo, String title, String content, List<File> files) throws Exception {
-
final MimeMessage mimeMessage = mailSender.createMimeMessage();
-
final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
-
helper.setFrom(sendUser);
-
helper.setSubject(title);
-
helper.setText(content);
-
//设置多个收件人
-
String[] toAddress = toMail.split( ",");
-
helper.setTo(toAddress);
-
//设置多个抄送
-
if(StringUtils.isNotBlank(replyTo)) {
-
//helper.setReplyTo(replyTo);
-
InternetAddress[] internetAddressCC = InternetAddress.parse(replyTo);
-
mimeMessage.setReplyTo(internetAddressCC);
-
}
-
//添加附件
-
if(null != files) {
-
for (File file : files) {
-
helper.addAttachment(file.getName(), file);
-
}
-
}
-
mailSender.send(mimeMessage);
-
}
-
/**
-
* @方法名: sendHtmlMail<br>
-
* @描述: 发送HTML格式的邮件<br>
-
* @param toMail 收件人,多个用英文格式逗号分隔
-
* @param replyTo 抄送人,多个用英文格式逗号分隔
-
* @param title 邮件主题
-
* @param htmlContent 邮件内容
-
* @param files 附件
-
* @throws Exception
-
*/
-
public void sendHtmlMail(String toMail, String replyTo, String title, String htmlContent, List<File> files) throws Exception {
-
final MimeMessage mimeMessage = mailSender.createMimeMessage();
-
final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
-
helper.setFrom(sendUser);
-
helper.setSubject(title);
-
helper.setText(htmlContent, true);
-
//设置多个收件人
-
String[] toAddress = toMail.split( ",");
-
helper.setTo(toAddress);
-
//设置多个抄送
-
if(StringUtils.isNotBlank(replyTo)) {
-
//helper.setReplyTo(replyTo);
-
InternetAddress[] internetAddressCC = InternetAddress.parse(replyTo);
-
mimeMessage.setRecipients(RecipientType.CC,internetAddressCC);
-
}
-
//添加附件
-
if(null != files) {
-
for (File file : files) {
-
helper.addAttachment(file.getName(), file);
-
}
-
}
-
mailSender.send(mimeMessage);
-
}
-
}原文链接: https://blog.csdn.net/LDY1016/article/details/78365072?utm_source=blogxgwz7