邮件发送入门:
一直想着研究做一下 邮箱的发送业务,今天有时间在网上查了一下参考着试了一下,总体不是很难,理解一下业务逻辑和熟悉一下类就可以实现简单的功能了,当然复杂的还是要深入研究的,今天做的还是很顺利只是在一个地方卡住了,下面重点讲出来,记录一下:
先创建好spring boot项目
1:导入pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.email</groupId>
<artifactId>email</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>email</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2:配置properties文件(与yml功能一样)
spring.mail.port=465 #默认端口
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com #选择邮箱(qq或者163)
spring.mail.username=发送者邮箱地址
#切记此处需要开启pom3的时候的验证码不是邮箱的密码
spring.mail.password=#授权码
spring.mail.properties.mail.smtp.ssl.enable=true
mail.fromMail.addr=发送者邮箱地址
目前只是简单的实现功能,所以没有写太多,直接service接口实现然后测试完成:
service接口:
public interface EmailService {
void sendTextEmail(String to, String subject, String content) throws MessagingException, UnsupportedEncodingException;
void sendHtmlEmali(String to,String subject,String content);
void sendAttachmentsEmail(String to,String subject,String content,String filePath);
void sendStaticResourcesEmail(String to,String subject,String content,String rscPath,String rscId);
}
接口实现类:
import com.email.email.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Service;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
@Service
public class EmaliServiceImpl implements EmailService {
private static final Logger log= LoggerFactory.getLogger(EmaliServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String from;
/***
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendTextEmail( String to, String subject, String content) throws UnsupportedEncodingException, MessagingException {
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setFrom(String.valueOf(new InternetAddress(from,"xxxx集团-深圳分公司","UTF-8")));
mailMessage.setTo(to);
mailMessage.setSubject(subject);
mailMessage.setText(content);
InternetAddress[]{new InternetAddress(to)});
try {
mailSender.send(mailMessage);
log.info("[文本邮件发送成功,当前时间]-[{}]",new Date());
}catch (Exception e){
e.printStackTrace();
}
}
}
测试类:
@SpringBootTest
class EmailApplicationTests {
@Autowired
EmailService service;
@Test
public void test1() throws UnsupportedEncodingException, MessagingException {
service.sendTextEmail("接收者地址”,"测试","收到请回复,over!");
System.out.println("成功");
}
}
其中在基本能发送邮件以后发现太简陋,想着把标题能够改一下,显得高大上一点,在查阅一番后发现,主要是就是创建邮件的方式不同:
1:第一种方式
public void sendTextEmail( Session session,String to, String subject, String content) throws UnsupportedEncodingException, MessagingException {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from,"xxx集团-深圳分公司","UTF-8"));
}
2:第二种方式
public void sendTextEmail( Session session,String to, String subject, String content) throws UnsupportedEncodingException, MessagingException {
mailMessage.setFrom(String.valueOf(new InternetAddress(from,"xxxx集团-深圳分公司","UTF-8")));
mailMessage.setTo(to);
mailMessage.setSubject(subject);
mailMessage.setText(content);
}
本示例选择的是第二种方式进行实现
基本入门 完成了,分享一下,后续学习在进行分享。
配置信息太多记不住 参考:https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html