Android UI控制元件系列:LinearLayout(線性佈局)
阿新 • • 發佈:2022-04-29
簡單幾步,實現在spring boot中傳送郵件: 1、引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、application.yml中配置郵件相關的引數:
spring: mail: host: smtp.exmail.qq.com username: 使用者名稱 password: 密碼 properties: mail: smtp: auth: true starttls: enable: true required: true
3、郵件service程式碼:
@Service public class MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String from; /** * 傳送純文字的簡單郵件 * @param to * @param subject * @param content */ public void sendSimpleMail(String to, String subject, String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { sender.send(message); logger.info("簡單郵件已經發送。"); } catch (Exception e) { logger.error("傳送簡單郵件時發生異常!", e); } } /** * 傳送html格式的郵件 * @param to * @param subject * @param content */ public void sendHtmlMail(String to, String subject, String content){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要建立一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); sender.send(message); logger.info("html郵件已經發送。"); } catch (MessagingException e) { logger.error("傳送html郵件時發生異常!", e); } } /** * 傳送帶附件的郵件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要建立一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); sender.send(message); logger.info("帶附件的郵件已經發送。"); } catch (MessagingException e) { logger.error("傳送帶附件的郵件時發生異常!", e); } } /** * 傳送嵌入靜態資源(一般是圖片)的郵件 * @param to * @param subject * @param content 郵件內容,需要包括一個靜態資源的id,比如:<img src="cid:rscId01" > * @param rscPath 靜態資源路徑和檔名 * @param rscId 靜態資源id */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要建立一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); sender.send(message); logger.info("嵌入靜態資源的郵件已經發送。"); } catch (MessagingException e) { logger.error("傳送嵌入靜態資源的郵件時發生異常!", e); } } }
4、單元測試程式碼:
public class MailTests extends BasicUtClass{ @Autowired private MailService mailService; private String to = "[email protected]"; @Test public void sendSimpleMail() { mailService.sendSimpleMail(to, "主題:簡單郵件", "測試郵件內容"); } @Autowired VelocityEngine velocityEngine; @Test public void sendHtmlMail() { Map<String, Object> model = new HashMap<String, Object>(); model.put("time", XDateUtils.nowToString()); model.put("message", "這是測試的內容。。。"); model.put("toUserName", "張三"); model.put("fromUserName", "老許"); String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "welcome.vm", "UTF-8", model); mailService.sendHtmlMail(to, "主題:html郵件", content); } @Test public void sendAttachmentsMail() { mailService.sendAttachmentsMail(to, "主題:帶附件的郵件", "有附件,請查收!", "C:\Users\Xu\Desktop\csdn\1.png"); } @Test public void sendInlineResourceMail() { String rscId = "rscId001"; mailService.sendInlineResourceMail(to, "主題:嵌入靜態資源的郵件", "<html><body>這是有嵌入靜態資源:<img src='cid:" + rscId + "' ></body></html>", "C:\Users\Xu\Desktop\csdn\1.png", rscId); } }