Flask接口签名sign原理与实例代码浅析
300
2023-03-16
Spring实现邮件发送功能
前言:以前都是直接用java自带的邮件工具发送邮件,现在Spring帮我们做了封装,提供了更好用更简单的发送邮件工具JavaMailSender,关于邮件服务器的设置就不在这里说了,直接去QQ邮箱设置下就好,下面看下主要的步骤:
步骤一、添加发送邮件相关Maven依赖
步骤二、添加相关Spring配置,创建一个spring-mail.xml(如果是Spring-Boot请无视),内容如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd ">
步骤三、创建具体的邮件发送工具类
/**
* 基于Spring的JavaMailSender的邮件发送工具类
* @author simon
*
*/
@Component
public class EmailSender {
@Autowired
private JavaMailSender javaMailSender;
private static EmailSender emailSender;
@PostConstruct
public void init() {
emailSender = this;
emailSender.javaMailSender= this.javaMailSender;
}
/**
* 发送简单的文本邮件
* @param sendTo 收件人组
* @param subject 主题
* @param content 文本内容
*/
JfaJHvAZ public static void sendSimpleMessage(String sendFrom, String[] sendTo, String subject, String textcontent) {
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(sendFrom);
mail.setTo(sendTo);
mail.setSubject(subject);
mail.setText(textcontent);
//发送
emailSender.javaMailSender.send(mail);
}
/**
* 发送HTML内容格式的邮件
* @param sendFrom
* @param sendTo 收件人组
* @param subject 主题
* @param htmlContent HTML内容
* @throws Exception
*/
public static void sendHtmlMessage(String sendFrom, String[] sendTo, String subject, String htmlContent) throws Exception {
MimeMessage mimeMessage = emailSender.javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setFrom(sendFrom);
mimeMessageHelper.setTo(sendTo);
mimeMessageHelper.setSubject(subject);
// true 表示启动HTML格式的邮件
mimeMessageHelper.setText(htmlContent, true);
// 发送邮件
emailSender.javaMailSender.send(mimeMessage);
}
}
步骤四、创建单元测试测试发送邮件
/**
* 邮件发送测试类
*
* @author simon
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class EmailSenderTest {
@Test
public void testSend() throws Exception {
String sendFrom = "1317492210@qq.com";
String[] sendTo = {"zhaosheng@hitencent.com"};
String subject = "Spring自带JavaMailSender发送的HTML邮件";
StringBuilder htmlContent = new StringBuilder()
.append("")
.append("
.append("
.append("Spring自带JavaMailSender发送的HTML邮件")
.append("
.append("")
.append("
.append("您好!陌生人
.append("")
.append("");
EmailSender.sendHtmlMessage(sendFrom, sendTo, subject, htmlContent.toString());
System.out.println("邮件发送成功.");
}
OK,到这里全部的步骤就结束了,是不是比Java Mail简单方便很多哉,赶紧自己动手试试吧!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~