SpringBooot + Spring mail 發送郵件demo
- 一、建立springboot項目 導入mail與boot整合依賴
- 二、配置檔案application.properties
- 三、代碼實作
- 四、定時發送郵件
一、建立springboot項目 導入mail與boot整合依賴
pom檔案引入
<!-- mail-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、配置檔案application.properties
server.port=8081
spring.mail.host=smtp.163.com
spring.mail.port=
#發件方賬戶
[email protected]
#密碼
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
三、代碼實作
1、目錄
2、MailService
/**
* @Author sgh
* Description:
*/
public interface MailService {
boolean send(String to, String title, String content);
boolean sendToSome(String[] to, String title, String content);
}
3、MailServiceImpl
import com.sgh.demofortest.mail.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* @Author SUGUANHAN
* Description:
*/
@Service
@Slf4j
public class MailConServiceImpl implements MailService {
private MailProperties mailProperties;
private JavaMailSender javaMailSender;
@Autowired
public MailConServiceImpl(MailProperties mailProperties, JavaMailSender javaMailSender) {
this.mailProperties = mailProperties;
this.javaMailSender = javaMailSender;
}
/**
* @Description : 單發郵件
* @author sgh
* @param to 郵件接受者郵箱
* @param title 郵件标題
* @param content 郵件内容
* @return boolean
*/
@Override
public boolean send(String to, String title, String content) {
log.info("send mail.. to {},title {},content {}",to,title,content);
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//郵件發送者
simpleMailMessage.setFrom(mailProperties.getUsername());
//郵件接受者
simpleMailMessage.setTo(to);
//郵件标題
simpleMailMessage.setSubject(title);
//郵件内容
simpleMailMessage.setText(content);
try {
log.info("begin to send mail...");
javaMailSender.send(simpleMailMessage);
} catch (MailException e) {
log.info("sorry! there is something wrong... {}",e.getMessage());
return false;
}
return true;
}
/**
* @Description : 群發郵件
* @author sgh
* @param to
* @param title
* @param content
* @return boolean
*/
@Override
public boolean sendToSome(String[] to, String title, String content) {
for (String receiver : to) {
try {
this.send(receiver,title,content);
} catch (Exception e) {
log.error("something wrong! {}",e.getMessage());
return false;
}
}
return true;
}
}
4、測試
import com.sgh.demofortest.mail.service.MailService;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemofortestApplicationTests {
@Autowired
private MailService mailService;
@Test
public void contextLoads() {
String to = "[email protected]";
String title = "a mail for java test";
String content = "外面的人聽着 被包圍了我們";
mailService.send(to,title,content);
}
@Test
public void sendToSomeTest(){
String [] receivers = {"[email protected]","[email protected]"};
mailService.sendToSome(receivers,"測試","測試一下而已");
}
}
四、定時發送郵件
休息一下