从 Spring Boot 2.1 发送电子邮件。3.RELEASE

sending email from SpringBoot 2.1.3.RELEASE

我想从 SpringBoot 2.1 发送邮件。3.RELEASE;我已经取消了那些属性:

spring.mail.host=smtp.gmail.com
spring.mail.username=nunito.calzada@gmail.com
spring.mail.password=aMdwd3cded2@
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.propertirs.mail.smtp.ssl.enable = true
and using org.springframework.mail.MailSender

我正在使用 org.springframework.mail.MailSender

发送电子邮件
    mailSender.send(mailMessage);

一切似乎都很好,我没有看到任何异常,任何错误,但我没有收到电子邮件,甚至在垃圾邮件中也没有

我也试过了

spring.mail.properties.mail.smtp.socketFactory.port = 587

结果相同

您的 属性 文件包含

spring.mail.properties.mail.smtp.socketFactory.port = 465

gmail 使用 TSL/SSL

Port for TLS/STARTTLS: 587
    Port for SSL: 465

我建议你使用

spring.mail.properties.mail.smtp.socketFactory.port = 587

而不是 465

for Spring Boot add the dependency

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

Once the dependency is in place, the next step is to specify the mail server properties in the application.properties file using the spring.mail.* namespace.

For example, the properties for Gmail SMTP Server can be specified as:

spring.mail.protocol=smtp
spring.mail.host: smtp.gmail.com
spring.mail.port: 465
spring.mail.username: <user name>
spring.mail.password: <password>
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: true
mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.test-connection=

代码:

@Autowired
private JavaMailSender 
private void setMailDetailsForSend( final String payload, final String email ) throws MessagingException
{

    final MimeMessage mail = mailSender.createMimeMessage();
    final MimeMessageHelper helper = new MimeMessageHelper( mail, true );
    helper.setTo( email );
    helper.setSubject( "Notification" );
    helper.setText( "text/html", payload );
    mailSender.send( mail );

}

Some SMTP servers require a TLS connection, so the property spring.mail.properties.mail.smtp.starttls.enable is used to enable a TLS-protected connection.

重要的是传入SendMail(String from,....的构造函数, 放: public void sendMail(字符串来自, 串到, 字符串主题, 字符串文本, 布尔 isHtmlContent) 抛出 MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage();

    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
    **mimeMessageHelper.setFrom(from);**

其中 from 必须是 发件人 的有效电子邮件地址,通常与 中定义的用户名相同]application.properties: ...spring.mail.username: