在spring中能够实现邮件的发送,需要导入在有spring环境的情况下面需要导入两种包,我是maven工程,这里就发依赖了
<!--支持包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- 发送邮件的包 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
在spring中配置bean,引入mail的配置文件
<context:property-placeholder location="classpath:mail.properties"/>
配置文件:
mail.smtp.host=smtp.163.com //服务器主机名
mail.smtp.password=密码
mail.smtp.username=账号
mail.smtp.defaultEncoding=utf-8//编码字符
mail.smtp.auth=true//是否进行用户名密码校验
mail.smtp.timeout=20000//设置超时时间
在spring中配置邮件发送的bean
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}"/>
<property name="password" value="${mail.smtp.password}"/>
<property name="username" value="${mail.smtp.username}"/>
<property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
</props>
</property>
</bean>
然后在单元测试中实现邮件的发送的测试
package com.yyt.utils;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"classpath:spring-com.xml","classpath:spring-mybatis.xml"})
public class MailSendTest {
@Autowired
private JavaMailSender javaMailSender;//在spring中配置的邮件发送的信息
@Test
public void sendMail(){
SimpleMailMessage smm=new SimpleMailMessage();
smm.setFrom("发件人的邮件地址");//发送人的邮件
smm.setTo("收件人的邮件地址");//接收的邮箱的地址
smm.setSubject("使用了properties,哈哈");//邮件的标题
smm.setText("骄傲骄傲骄傲骄傲家居,你收到了没有");//邮件的内容
javaMailSender.send(smm);//发送邮件
System.out.println("发送成功");
}
@Test
public void sendMail01(){
/**
* 使用另一种的方式发送邮件,然后在邮件 使html格式生效
*/
MimeMessage mm=javaMailSender.createMimeMessage();
MimeMessageHelper smm=new MimeMessageHelper(mm);
try {
smm.setFrom("发件人的邮件地址");//发送人的邮件
smm.setTo("接收的邮箱的地址");//接收的邮箱的地址
smm.setSubject("使用了properties,哈哈,猜猜我是谁?");//邮件的标题
smm.setText("骄傲骄傲骄傲骄傲家居,你收到了没有<br><a href='http://www.baidu.com'>去百度吧!</a>",true);//邮件的内容 true 是代表的html格式生效
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mm);//发送邮件
System.out.println("发送成功");
}
@Test
public void sendMail02(){
/**
* 使用另一种的方式发送邮件,然后在邮件,发送一个照片
*/
MimeMessage mm=javaMailSender.createMimeMessage();
MimeMessageHelper smm=null;
try {
smm=new MimeMessageHelper(mm,true);
smm.setFrom("发送人的邮件");//发送人的邮件
smm.setTo("接收的邮箱的地址");//接收的邮箱的地址
smm.setSubject("你丫的吃了狗屎了,你还不发送成功");//邮件的标题 这里多此测试 有可能会导致报错,说你发送的邮件内容不合法,注意看报错码,然后在去找报错码的意思,我这里也是报错了的,我是在他们官网上面看到的
smm.setText("山东凯撒第三款<a href='http://www.baidu.com'>去百度吧!</a><img src='cid:identifier1234'>",true);//邮件的内容 true 是代表的html格式生效
File file=new File("D:/1.jpg");//发送邮件的图片
FileSystemResource resource=new FileSystemResource(file);
smm.addInline("identifier1234", resource);//这里指定一个id,在上面引用
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mm);//发送邮件
System.out.println("发送成功");
}
@Test
public void sendMail03(){
/**
* 使用另一种的方式发送邮件,然后在邮件,发送一个附件
*/
MimeMessage mm=javaMailSender.createMimeMessage();
MimeMessageHelper smm=null;
try {
smm=new MimeMessageHelper(mm,true);
smm.setFrom("发送人的邮件");//发送人的邮件
smm.setTo("接收的邮箱的地址");//接收的邮箱的地址
smm.setSubject("你丫的吃了狗屎了,你还不发送成功");//邮件的标题
smm.setText("山东凯撒第三款<a href='http://www.baidu.com'>去百度吧!</a>",true);//邮件的内容 true 是代表的html格式生效
File file=new File("D:/1.jpg");
FileSystemResource resource=new FileSystemResource(file);
smm.addAttachment("CoolImage.jpg", resource);//发送一个附件
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mm);//发送邮件
System.out.println("发送成功");
}
}
基本上以上的几种方式就能实现邮件的发送,根据自己的项目的实际需求来选择用哪种!