直到使用Spring Boot发送电子邮件
目标
发送仅包含文本的电子邮件
由于在Google的两步验证中遇到了困难,我记录了下来。
环境
IDE STS
Spring Boot 1.3.1
使用spring-boot-starter-mail进行邮件发送
步骤

抱歉,需要更多的上下文才能为您提供准确的汉语翻译。请提供更多详细信息以便我能够帮助您。
有几种方法,但这次我们会使用spring-boot-starter-mail来设置pom文件的依赖关系。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
邮件设置
编辑 application.yml
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:file:/tmp/mvp_mitsumori01 #
username: sa
password:
jpa:
hibernate.ddl-auto: validate #
thymeleaf.cache: false
mail:
host: smtp.gmail.com
port: 587
username: <googleのメールアドレス>
password: <アプリパスワード>
properties.mail.smtp.auth: true
properties.mail.smtp.starttls.enable: true
数据源无关
在邮件方面,我会记录下本次使用Gmail的配置
問題是,<应用程序密码>是什么,而在Google的设置中稍后提到。
Java: Java 是一种广泛使用的计算机编程语言。
为了确认发送,当main函数被执行时同时发送邮件。
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
@SpringBootApplication
public class MailTest02Application {
public static void main(String[] args) {
try (ConfigurableApplicationContext ctx = SpringApplication.run(MailTest02Application.class, args)) {
ctx.getBean(MailTest02Application.class).sendMail();
}
}
@Autowired
private MailSender sender;
public void sendMail() {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom("test@mail.com");
msg.setTo("<メールアドレス>");
msg.setSubject("テストメール"); //タイトルの設定
msg.setText("Spring Boot より本文送信"); //本文の設定
this.sender.send(msg);
}
}
谷歌的设定

登入Google账号以进入账号管理。

使用Spring Boot时可以访问它。
这就是在application.yml中设置的<应用密码>。
当涉及到这个问题时,一旦可以学会如何附加文件等操作,我会整理一下。