Spring整合JavaMail

1.添加jar包

#此处省略spring基础相关jar包描述,以下是发送邮件相关jar包
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

2.配置文件

1.mail.properties

mail.smtp.host=smtp.163.com
mail.username=邮箱帐号(例如:abc)
mail.password=邮箱密码(例如:123456)
mail.smtp.auth=true
mail.from=发送邮箱(例如:abc@163.com)

2.applicationContext-mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


	<context:property-placeholder location="classpath:mail.properties"/>
	
	<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="from" value="${mail.from}"></property>
	</bean>
	
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="${mail.smtp.host}"></property>
		<property name="username" value="${mail.username}"></property>
		<property name="password" value="${mail.password}"></property>
		<property name="defaultEncoding" value="UTF-8"></property>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
				<prop key="mail.debug">true</prop>
				<prop key="mail.smtp.timeout">0</prop>
			</props>
		</property>
	</bean>
</beans>

3.Java代码

import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailTest {

    @Test
    public void test() throws MessagingException {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    
        SimpleMailMessage message = (SimpleMailMessage) act.getBean("mailMessage");
        message.setSubject("约会");
        message.setText("2017年5月10日在西湖边见面");
        message.setTo("xxxx@163.com");
        
        JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
        sender.send(message);
        
    }
    
    @Test
    public void test2() throws MessagingException {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    
        JavaMailSender sender = (JavaMailSender) act.getBean("mailSender");
        MimeMessage message = sender.createMimeMessage();
        //使用工具类设置附件
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("aaa@163.com");
        helper.setTo("bbb@163.com");
        helper.setSubject("来自百度的主题");
        helper.setText("<html><head></head><body><h2>你好</h2>"
                + "<a href='http://www.baidu.com'>百度</a>"
                + "<img src=cid:image></body></html>",true);
        
        //带图片
        FileSystemResource jpg = new FileSystemResource(new File("d:\test.jpg"));
        helper.addInline("image", jpg);//此处的image必须与html代码段中的<img src=cid:image>一致
        
        //带附件
        FileSystemResource attach = new FileSystemResource(new File("d:\intro.csv"));
        helper.addInline("intro.csv", attach);
        
        sender.send(message);
        
    }
}
原文地址:https://www.cnblogs.com/moonlightL/p/7271325.html