JavaMailSender邮箱配置

Posted wanhua.wu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaMailSender邮箱配置相关的知识,希望对你有一定的参考价值。

 

163邮箱:

#####163邮箱########
spring.mail.host=smtp.163.com
spring.mail.username[email protected]163.com
#163邮箱密码
spring.mail.password=qS
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

 

gmail邮箱:

spring.mail.host=smtp.gmail.com
spring.mail.port=465
[email protected]
spring.mail.password=ndb
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.ssl.enable=true

  

例子:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String username;

    public void send(String email, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(username);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(body);
        javaMailSender.send(message);
    }

}

 

以上是关于JavaMailSender邮箱配置的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot使用JavaMailSender发送邮件

Spring Boot中使用JavaMailSender发送邮件

使用spring的JavaMailSender发送邮件

spring-boot实战12:Spring Boot中使用JavaMailSender发送邮件

Spring javamailsender 发送 SSL 电子邮件

Java 之 Spring Boot 发送邮箱(解决依赖找不到异常)