準備
依賴
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
按照之前我們瞭解的SpringBoot的自動裝配原理,試着搜一搜MailAutoConfiguration,看看有沒有這個類,結果還真有。不過他叫MailSenderAutoConfiguration
,那就接着看看MailProperties
唄!!
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {
/**
* Condition to trigger the creation of a {@link MailSender}. This kicks in if either
* the host or jndi name property is set.
*/
static class MailSenderCondition extends AnyNestedCondition {
MailSenderCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
static class HostProperty {
}
@ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name")
static class JndiNameProperty {
}
}
}
MailProperties
我們可以明確的看見,他的內部就是我們之前在java-web時寫的那些熟悉的配置。我們只需在application.yaml中修改就可以了。
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
//SMTP server host. For instance, `smtp.example.com`.
private String host;
// SMTP server port.
private Integer port;
// Login user of the SMTP server.
private String username;
// Login password of the SMTP server.
private String password;
// Protocol used by the SMTP server.
private String protocol = "smtp";
// Default MimeMessage encoding.
private Charset defaultEncoding = DEFAULT_CHARSET;
//Additional JavaMail Session properties.
private Map<String, String> properties = new HashMap<>();
// Session JNDI name. When set, takes precedence over other Session settings.
private String jndiName;
……
}
配置application.yaml
spring:
mail:
username: [email protected]
password: lncnwzwgwltohbih
host: smtp.qq.com
#開啓加密驗證
properties:
mail:
smtp:
ssl:
enable: true
測試代碼:
@SpringBootTest
public class MailTest {
@Autowired
JavaMailSenderImpl mailSender;
//簡單郵件
@Test
public void contextLoads(){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好啊!!");
mailMessage.setText("這是一個測試郵件,springboot郵件!!");
mailMessage.setTo("[email protected]");
mailMessage.setFrom("[email protected]");
mailSender.send(mailMessage);
}
//複雜郵件
@Test
public void contextLoads2() throws MessagingException {
MimeMessage mimeMessage=mailSender.createMimeMessage();
//組裝
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true,"utf-8");;
//正文
helper.setSubject("你好啊!!這是一個複雜測試郵件!!");
//解析html
helper.setText("<h3 style='color:pink'>這是一個複雜測試郵件!!</h3>",true);
//附件
helper.addAttachment("girl.jpg",new File("D:\\springboot-mail\\src\\main\\resources\\gril.jpg"));
helper.setTo("[email protected]");
helper.setFrom("[email protected]");
mailSender.send(mimeMessage);
}
}
測試結果1:
測試結果2:
用到的幾個類
JavaMailSenderImpl
製作{@link Java郵件發送器}接口的實現,
支持Java郵件{@linkMime消息MIME消息}和Spring{@鏈接簡單郵件消息簡單郵件消息}.
也可以用作普通的{@鏈接org.springframework.mail。 郵件發件人}實現,
允許將所有設置本地定義爲bean屬性。
或者,可以預先配置JavaMail{@link javax.mail.Session}指定,
可能從應用服務器的JNDI環境中提取。
此對象中的非默認屬性將始終覆蓋設置在Java郵件{@代碼會話}中。
注意,如果在本地覆蓋所有值,在設置預先配置的{@code Session}時沒有附加值。
SimpleMailMessage、MimeMessage
前者是簡單的郵件,後者是複雜郵件(包含html、附件等)。
MimeMessageHelper
這是一個幫助類, org.springframework.mail的簡單設置器。 簡單郵件消息,將數值直接應用於底層MIME消息。 允許定義由所有方法自動應用的整個消息的字符編碼提供對HTML文本內容、圖像等內聯元素和典型元素的支持郵件附件。 還支持伴隨郵件地址的個人姓名。 請注意,高級設置仍然可以直接應用於底層MIME消息對象!
他提供不同構造器給我們選擇使用。例如:是否解析html,編碼格式等。