使用Spring发送Email详细流程(QQ邮箱例子)
下载完后,创建gradle项目,以下是我实现完后的整体项目
接下来的是各个文件的代码内容,可直接复制
其中实现了发送邮件的三种方法
第三步、
@Configuration
@ComponentScan("spittr")
@PropertySource("classpath:mail.properties")
public class MailConfig {
@Bean
public JavaMailSenderImpl mailSender(Environment env) {
//spring自带了一个MailSender的实现接口
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//邮件服务器主机名
mailSender.setHost(env.getProperty("mailserver.host"));
//邮件服务器监听的端口号
mailSender.setPort(Integer.parseInt(env.getProperty("mailserver.port")));
//用户名称
mailSender.setUsername(env.getProperty("mailserver.username"));
//密码,这里不是邮箱的密码,而是开启服务器后,生成的授权码
mailSender.setPassword(env.getProperty("mailserver.password"));
//ssl加密,需要加,否则无法运行
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
mailSender.setJavaMailProperties(props );
return mailSender;
}
}
第四步、
public class Spitter {
//编号
private Long id;
//用户名称
private String username;
//用户密码
private String password;
//发件人的名称
private String fullName;
//收件人的邮箱
private String email;
//判断邮件是否带有附件
private boolean updateByEmail;
public Spitter(Long id, String username, String password, String fullName, String email, boolean updateByEmail) {
this.id = id;
this.username = username;
this.password = password;
this.fullName = fullName;
this.email = email;
this.updateByEmail = updateByEmail;
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getFullName() {
return fullName;
}
public String getEmail() {
return email;
}
public boolean isUpdateByEmail() {
return updateByEmail;
}
}
第五步、
public class Spittle {
//编号
private final Long id;
//邮件的内容
private final String text;
//发送的时间
private final Date postedTime;
//邮件信息的对象
private Spitter spitter;
public Spittle(Long id, Spitter spitter, String text, Date postedTime) {
this.id = id;
this.spitter = spitter;
this.text = text;
this.postedTime = postedTime;
}
public Long getId() {
return this.id;
}
public String getText() {
return this.text;
}
public Date getPostedTime() {
return this.postedTime;
}
public Spitter getSpitter() {
return spitter;
}
}
第六步、
public interface SpitterMailService {
public abstract void sendSimpleSpittleEmail(String to, Spittle spittle);
public abstract void sendSpittleEmailWithAttachment(String to, Spittle spittle) throws MessagingException;
public abstract void sendRichSpitterEmail(String to,Spittle spittle)throws MessagingException;
}
第七步、
@Component
public class SpitterMailServiceImpl implements SpitterMailService {
//邮件发送的核心类
private JavaMailSender mailSender;
private VelocityEngine velocityEngine;
//通过构造方法将JavaMailSender依赖注入进来
@Autowired
public SpitterMailServiceImpl(JavaMailSender mailSender,VelocityEngine velocityEngine) {
this.mailSender = mailSender;
this.velocityEngine = velocityEngine;
}
/* 创建一封简单的邮件 */
@Override
public void sendSimpleSpittleEmail(String to, Spittle spittle) {
//通过这个对象封装信息,相当于信封
SimpleMailMessage message = new SimpleMailMessage();
//获取spittle对象中的收件人名字
String spitterName = spittle.getSpitter().getFullName();
//发件人
message.setFrom("******@qq.com");
//收件人
message.setTo(to);
//邮件主题
message.setSubject("New spittle from " + spitterName);
//邮件内容
message.setText(spitterName + " says: " + spittle.getText());
//发送邮件
mailSender.send(message);
}
@Override
public void sendSpittleEmailWithAttachment(String to, Spittle spittle) throws MessagingException {
//通过邮件发送器的createMimeMessage方法构建可含附件的邮件
MimeMessage message = mailSender.createMimeMessage();
// 第一个参数是邮件的对象
// 第二个参数为true表示这个消息是multipart类型的消息(表示是带有附件的邮件)
// 第三个参数是防止中文乱码
MimeMessageHelper helper = new MimeMessageHelper(message,true,"utf-8");
//获取收件人名,用于后面发送邮件做主题
String spitterName = spittle.getSpitter().getFullName();
//寄件人
helper.setFrom("*******@qq.com");
//收件人
helper.setTo(to);
//邮件的主题内容
helper.setSubject("New spittle from " + spitterName);
//邮件的发送内容
helper.setText(spitterName + " says: " + spittle.getText());
//通过路径获取图片附件
ClassPathResource couponImage = new ClassPathResource("/collateral/coupon.jpg");
//第一个参数是图片的名称,到时候收件人收到的附件名
//第二个参数是图片附件的资源
helper.addAttachment("Coupon.jpg", couponImage);
//send方法是发送邮件的方法
mailSender.send(message);
}
@Override
public void sendRichSpitterEmail(String to,Spittle spittle)throws MessagingException {
//创建一封富文本邮件
MimeMessage message=mailSender.createMimeMessage();
//第二个参数表明传递进来的第一个参数是html文本
MimeMessageHelper helper=new MimeMessageHelper(message,true,"utf-8");
//发件人
helper.setFrom("********@qq.com");
//收件人
helper.setTo(to);
//发送的邮件主题
helper.setSubject("New spittle from " + spittle.getSpitter().getFullName());
//cid:spitterLogo,标记一个变量,后面为这个变量添加一张图片
//第二个参数表示传递的第一个参数是html
helper.setText("<html><body><img src='cid:spittleLogo'>"+"<h4>"+
spittle.getSpitter().getFullName()+"says...</h4>"+"<i>"+spittle.getText()+
"</i>"+"</body></html>",true);
//创建一个变量存放图片
ClassPathResource image=new ClassPathResource("/collateral/coupon.jpg");
//为消息添加嵌入式的图片
helper.addInline("spittleLogo", image);
//发送邮件
mailSender.send(message);
}
}
第八步、图片自行添加
第九步、这是最终的测试运行类,做到这里先别急,还没完
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=MailConfig.class)
public class SpitterMailServiceImplTest {
@Autowired
private SpitterMailService mailService;
@Test
public void sendSimpleSpittleEmail() throws Exception {
Spitter spitter = new Spitter(1L, "habuma", null, "coffie", "******@qq.com", true);
Spittle spittle = new Spittle(1L, spitter, "Hiya!", new Date());
//第一个参数是收件人,第二个参数是邮件
mailService.sendSimpleSpittleEmail(spitter.getEmail(),spittle);
mailService.sendSpittleEmailWithAttachment(spitter.getEmail(), spittle);
mailService.sendRichSpitterEmail(spitter.getEmail(),spittle);
}
}
第十步、这个文件是配置邮箱服务器的属性,要是觉得麻烦也可以直接写在第三步中相对应的值中,但是却没那么灵活
qq的服务器名
mailserver.host=smtp.qq.com
端口号
mailserver.port=465
你的邮箱号
mailserver.username=********@qq.com
第一步获取到的授权码
mailserver.password=adomojibzlirbdff到了这里就完成了,再提醒一句,如果不知道如何在gradle中导包,可去看我的其他一篇博客