前言:以前都是直接用Java自带的邮件工具发送邮件,现在Spring帮我们做了封装,提供了更好用更简单的发送邮件工具JavaMailSender,关于邮件服务器的设置就不在这里说了,直接去QQ邮箱设置下就好,下面看下主要的步骤:
步骤一、添加发送邮件相关Maven依赖
<!-- Spring 邮件发送 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
步骤二、添加相关Spring配置,创建一个spring-mail.xml(如果是Spring-Boot请无视),内容如下:
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- Spring JavaMailSender --> <bean class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com"></property> <!-- 或许你可以用465端口,默认的25不适合 --> <property name="port" value="587"/> <property name="protocol" value="smtp"/> <property name="username" value="******@qq.com"></property> <!-- 这里的是你在QQ邮箱发送开通SMTP短信后,获取的授权码 --> <property name="password" value="******"></property> <property name="defaultEncoding" value="UTF-8"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean> </beans>
步骤三、创建具体的邮件发送工具类
/** * 基于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 文本内容 */ 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("<html>") .append("<head>") .append("<title>") .append("Spring自带JavaMailSender发送的HTML邮件") .append("</title>") .append("</head>") .append("<body>") .append("您好!陌生人<p/>") .append("</body>") .append("</html>"); EmailSender.sendHtmlMessage(sendFrom, sendTo, subject, htmlContent.toString()); System.out.println("邮件发送成功."); }
OK,到这里全部的步骤就结束了,是不是比Java Mail简单方便很多哉,赶紧自己动手试试吧!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。