【Spring-Mail】
需要的pom依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.6.2</version> </dependency>
一、普通邮件发送
编写配置信息【app.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" 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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.zeal4j"/> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com" /> <property name="port" value="587" /> <property name="username" value="zeal4j" /> <property name="password" value="frcgbdjqjzptbegg" /> <property name="defaultEncoding" value="utf-8"/> </bean> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="zeal4j@qq.com" /> <property name="subject" value="spring-mail"/> </bean> </beans>
管理器类:
package cn.zeal4j.util.springmail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailMessage; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Component; /** * @author Administrator * @file IntelliJ IDEA Spring-Mail * @create 2020 09 22 12:38 */ @Component public class MailOrderManager implements OrderManager{ @Autowired private MailSender mailSender; @Autowired private MailMessage mailMessage; public void placeOrder() { mailMessage.setTo("zeal4j@qq.com"); mailMessage.setText("This message powered by Spring-Mail, 测试"); mailSender.send((SimpleMailMessage) mailMessage); } }
接口:
package cn.zeal4j.util.springmail; /** * @author Administrator * @file IntelliJ IDEA Spring-Mail * @create 2020 09 22 12:39 */ public interface OrderManager { void placeOrder(); }
测试:
@Test public void sendSpringMail() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml"); MailOrderManager mailOrderManager = applicationContext.getBean("mailOrderManager", MailOrderManager.class); mailOrderManager.placeOrder(); }
二、带附件邮件发送:
@Test public void sendSpringMailWithAttachment() throws Exception{ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml"); JavaMailSender mailSender = applicationContext.getBean("mailSender", JavaMailSender.class); MimeMessage mimeMessage = mailSender.createMimeMessage(); mimeMessage.setSubject("Spring-Mail 附件测试"); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); mimeMessageHelper.setTo("zeal4j@qq.com"); mimeMessageHelper.setFrom("zeal4j@qq.com"); mimeMessageHelper.setText("Spring-Mail 附件测试 ......"); File file = new File("D:\\picture\\collection\\bg.jpg"); mimeMessageHelper.addAttachment(file.getName(), file); mailSender.send(mimeMessage); }