有时在开发中我们可能会需要对用户邮箱进行验证,这个时候就需要发送邮箱验证了,这里使用
springboot实现邮件发送。
1.先在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在完成依赖引入后,可在application.yml中添加配置属性内容。
spring:
mail:
host: smtp.qq.com
username: xxxxxx
password: xxxxxxx
port: 25
protocol: smtp
default-encoding: UTF-8
3.接下来就可以创建一个工具类来实现发送各种邮件。
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
@Component
@Log4j2
public class MailSendUtil {
@Autowired
JavaMailSender javaMailSenderImpl;
/**
* 发送简单html文本的邮箱验证码
* @param to
* @param code
*/
public boolean sendSimpleHtmlMail(String to,String code) {
MimeMessage message=javaMailSenderImpl.createMimeMessage();
try {
//设置发送人
message.setFrom(new InternetAddress(username));
//设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 设置邮件标题
message.setSubject("邮箱验证码");
message.setContent("您正在对相关操作进行验证,非自我操作请忽略,验证码为:" + code + " 验证码十分钟内有效,请及时处理!","text/html;charset=UTF-8");
message.setSentDate(new Date());
javaMailSenderImpl.send(message);
} catch (AddressException e) {
log.error(e.getMessage());
return false;
} catch (MessagingException e) {
log.error(e.getMessage());
return false;
}
return true;
}
/**
* 发送带图片的邮件
* @param to
* @param subject
* @param content
* @param picPath
* @param picId
* @return
*/
public boolean sendPicMail(String to,String subject,String content,String picPath,String picId){
MimeMessage message=javaMailSenderImpl.createMimeMessage();
MimeMessageHelper helper=null;
try {
helper=new MimeMessageHelper(message, true);
//设置发送人
message.setFrom(new InternetAddress(username));
//设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 设置邮件标题
message.setSubject(subject);
helper.setText(content, true);
helper.addInline(picId, new FileSystemResource(new File(picPath)));
// helper.setTo(new InternetAddress(to));
message.setSentDate(new Date());
javaMailSenderImpl.send(message);
} catch (AddressException e) {
log.error(e.getMessage());
return false;
} catch (MessagingException e) {
log.error(e.getMessage());
return false;
}
return true;
}
/**
* 发送带附件的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @return
*/
public boolean sendEnclosureMail(String to,String subject,String content,String filePath){
MimeMessage message=javaMailSenderImpl.createMimeMessage();
MimeMessageHelper helper=null;
try {
helper=new MimeMessageHelper(message, true);
//设置发送人
message.setFrom(new InternetAddress(username));
//设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 设置邮件标题
message.setSubject(subject);
FileSystemResource file = new FileSystemResource(new File(filePath));
helper.addAttachment(file.getFilename(), file);
message.setSentDate(new Date());
helper.setText(content, true);
javaMailSenderImpl.send(message);
} catch (AddressException e) {
log.error(e.getMessage());
return false;
} catch (MessagingException e) {
log.error(e.getMessage());
return false;
}
return true;
}
/**
* 发送带附件和图片的邮件
* @param to
* @param subject
* @param content
* @param filePath
* @return
*/
public boolean sendPicEnclosureMail(String to,String subject,String content,String filePath,String picPath,String picId){
MimeMessage message=javaMailSenderImpl.createMimeMessage();
MimeMessageHelper helper=null;
try {
helper=new MimeMessageHelper(message, true);
//设置发送人
message.setFrom(new InternetAddress(username));
//设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
// 设置邮件标题
message.setSubject(subject);
FileSystemResource file = new FileSystemResource(new File(filePath));
//添加图片
helper.addInline(picId, new FileSystemResource(new File(picPath)));
//添加附件
helper.addAttachment(file.getFilename(), file);
message.setSentDate(new Date());
helper.setText(content, true);
javaMailSenderImpl.send(message);
} catch (AddressException e) {
log.error(e.getMessage());
return false;
} catch (MessagingException e) {
log.error(e.getMessage());
return false;
}
return true;
}
}
springboot中使用JavaMailSender发送邮件可以说非常方便了。
4.最后可以通过简单的单元测试来发送邮件了。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.suntree.utils.MailSendUtil;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
MailSendUtil mailSendUtil;
@Test
public void sendMail(){
mailSendUtil.sendSimpleHtmlMail("153xxxxx@qq.com", "123456");
}
}
有兴趣的可以试一试。