SpringBoot發送郵件(三)發送帶有附件的郵件

前言:使用發郵件這個功能不難,但是也有一些坑,下面我把開發郵件功能總結了一下分享給大家,同時爲了避免篇幅過長,導致大家看的不仔細或看一半不想看了,我將這個功能細分了一下,寫了好幾篇供大家各取所需。

Spring mail提供了 JavaMailSender接口實現郵件發送,其底層還是javamail,不過是進一步封裝變得更加易用了。下面通過實例看看如何在Spring Boot中使用 JavaMailSender 發送郵件。

一、引入maven依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>


二、開啓POP3/SMTP服務

       把這個寫在第二步,是因爲很多沒做過發送郵件的可能不知道需要開啓這個才能使用第三方程序來發郵件,並且生成的授權碼下面也要用到。

 三、配置application.properties文件

spring.mail.host=smtp.qq.com      --qq郵箱爲smtp.qq.com      163郵箱爲smtp.163.com
[email protected]     --自己定義的發送者賬號
spring.mail.password=123456     --這裏不是註冊時的密碼,而是上面生成的授權碼
spring.mail.properties.mail.smtp.auth=true   --開啓驗證 true爲開啓,false不開啓
spring.mail.properties.mail.smtp.starttls.enable=true    --加密通訊,true開啓,false不開啓
spring.mail.properties.mail.smtp.starttls.required=true    --是否必須通過使用加密通訊進行通訊,true開啓,false不開啓

 四、編寫service

爲了能真實使用,編寫了一個MailService ,我就不在junt上寫了

@Service
public class MailService {

	@Autowired
	private JavaMailSender mailSender;

     // 獲取application.properties中的值賦給from
	@Value("${spring.mail.username}")
	private String from;

	/**
	 * @param toUsers 收件人
	 * @param ccUsers 抄送人
	 * @param title 標題
	 * @param text 內容
	 * @param fileName 文件名稱
	 * @param filePath 文件路徑
	 * @return
	 */
	public boolean sendFileMail(String[] toUsers,String[] ccUsers, String title, String text,String filePath,String fileName) {
		boolean isSend = true;
		try {
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom(from);// 發件人
			helper.setTo(toUsers);// 收件人
			//helper.setCc(ccUsers);//抄送人,使用Cc還是Bcc大家根據具體場景自己選擇
			helper.setBcc(ccUsers);//祕密抄送(發件人,收件人,抄送人都不會看到抄送給誰了)
			helper.setSubject(title);// 標題
			helper.setText(text);// text:內容
			File file = new File(filePath);// 創建文件
			FileSystemResource resource = new FileSystemResource(file);
			helper.addAttachment(fileName, resource);//若要添加多個附件,需要創建多個file然後分別addAttachment
			mailSender.send(mimeMessage);// 發送郵件
		} catch (MessagingException e) {
			isSend = false;
			e.printStackTrace();
		}
		return isSend;
	}
}


 五、在controller中或service中調用

寫到這大家肯定都會寫了,下面我以controller爲例(具體使用場景肯定不是這樣的,我只是給大家舉個例子)

@Controller
public class UserController {
 
    @Autowired
    MailService mailService;
 
    @RequestMapping("sendEmail")
    public String sendMail(ModelMap modelMap) {
        String[] toUsers={"[email protected]","[email protected]"};
        String[] ccUsers = {"[email protected]","[email protected]"};
        String isSend=mailService.sendFileMail(toUsers, ccUsers,"測試郵件", "測試內容", "C:\\Users\\文檔\\test1.txt", "test0001");
         if(isSend){
            modelMap.put("isSend", "成功發送給[email protected]用戶");
         }else{
            modelMap.put("isSend", "發送失敗");
         }
        return "index";
    }
}


六、最後前臺調用sendEmail就行了,具體我就不寫了。

其他發送郵件功能參考:
SpringBoot發送郵件(一)只有文本的簡單郵件:https://blog.csdn.net/m0_37679452/article/details/84345616

SpringBoot發送郵件(二)發送包含圖片的郵件:https://blog.csdn.net/m0_37679452/article/details/84370881

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章