第一步:QQ郵箱開啟郵箱授權碼

切換到賬戶選項卡:

拉到下方 找到POP3/IMAP/SMTP服務,開啟POP3/SMTP服務:



oxdlgmtkqtnbbjdg
第二步:匯入依賴
創建SpringBoot專案,匯入Maven依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第三步:組態檔
spring:
mail:
host: smtp.qq.com #發送郵件服務器
username: 369950806@qq.com #發送郵件的郵箱地址
password: oxdlgmtkqtnbbjdg #客戶端授權碼,不是郵箱密碼,這個在qq郵箱設定里面自動生成的
properties.mail.smtp.port: 465 #埠號465或587
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
default-encoding: utf-8
第四步:介面
public interface EmailService {
/**
* 發送文本郵件
*
* @param from 發送人
* @param to 收件人
* @param cc 抄送人
* @param subject 主題
* @param content 內容
*/
void sendTextEmail(String from,String to,String cc, String subject, String content);
/**
* 發送HTML郵件
*
* @param from 發送人
* @param to 收件人
* @param subject 主題
* @param content 內容
*/
void sendMimeEmail(String from,String to, String subject, String content);
/**
* 發送帶附件的郵件
*
* @param from 發送人
* @param to 收件人
* @param subject 主題
* @param content 內容
* @param file 附件
*/
public void sendsAttachmentEmail(String from,String to, String subject, String content, String file);
}
第五步:實作類
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {
@Resource
private JavaMailSender sender;
@Override
public void sendTextEmail(String from,String to, String cc, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
//發件人
message.setFrom(from);
//收件人
message.setTo(to);
//抄送人
message.setCc(cc);
//郵件主題
message.setSubject(subject);
//郵件內容
message.setText(content);
//發送郵件
sender.send(message);
}
@Override
public void sendMimeEmail(String from,String to, String subject, String content) {
MimeMessage message = sender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
message.setSubject(subject);
helper.setText(content,true);
sender.send(message);
log.info("郵件已經發送!");
} catch (MessagingException e) {
log.error("發送郵件時發生例外:"+e);
}
}
@Override
public void sendsAttachmentEmail(String from,String to, String subject, String content, String file) {
MimeMessage message = sender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
//攜帶附件
FileSystemResource fsr = new FileSystemResource(file);
String fileName = file.substring(file.lastIndexOf("/"));
helper.addAttachment(fileName,fsr);
sender.send(message);
log.info("郵件加附件發送成功!");
} catch (MessagingException e) {
log.error("發送失敗:"+e);
}
}
}
第六步:測驗代碼
@SpringBootTest
class MailDemoApplicationTests {
@Resource
private EmailService emailService;
@Test
public void sendSimpleEmail(){
String content = "好好學習,天天向上";
emailService.sendTextEmail("369950806@qq.com","3433698914@qq.com","hcitlife@hotmail.com","哈哈",content);
}
@Test
public void sendMimeEmail(){
String content = "<a href='https://blog.csdn.net/lianghecai52171314/'>注冊成功,點擊鏈接激活</a>";
emailService.sendMimeEmail("369950806@qq.com","3433698914@qq.com","激活郵件",content);
}
@Test
public void sendsAttachmentEmail1(){
emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","帶附件的郵件","美女","E:/圖片/mm01.jpg");
}
@Test
public void sendsAttachmentEmail2(){
emailService.sendsAttachmentEmail("369950806@qq.com","3433698914@qq.com","帶附件的郵件","美女","E:/圖片/zbjbxf.mp3");
}
@Test
void fun(){
File file = new File("E:/圖片/mm01.jpg");
System.out.println(file.getName());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/295397.html
標籤:java
上一篇:熱榜!! 基于java springboot實作博客系統《建議收藏》
下一篇:Java基礎知識之什么是集合框架
