個人的に作っているウェブサイトからメールを送信する機能を作るようになって記録します。
-
build.gradle 作成
dependencies { implementation 'org.springframework.boot:spring-boot-starter-mail' }
-
以下のURLからアプリパスワードを作成
https://accounts.google.com/v3/signin/challenge/pwd?continue=https://myaccount.google.com/apppasswords&service=accountsettings
-
application.propertiesに以下の内容を作成
AdminMail.id = Email AdminMail.password = 2の作成ボタンをクリックすると出てくるパスワード
-
Configを作成
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration
@PropertySource("classpath:application.properties")
public class EmailConfig {
@Value("${mail.smtp.port}")
private int port;
@Value("${mail.smtp.socketFactory.port}")
private int socketPort;
@Value("${mail.smtp.auth}")
private boolean auth;
@Value("${mail.smtp.starttls.enable}")
private boolean starttls;
@Value("${mail.smtp.starttls.required}")
private boolean startlls_required;
@Value("${mail.smtp.socketFactory.fallback}")
private boolean fallback;
@Value("${AdminMail.id}")
private String id;
@Value("${AdminMail.password}")
private String password;
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.gmail.com");
javaMailSender.setUsername(id);
javaMailSender.setPassword(password);
javaMailSender.setPort(port);
javaMailSender.setJavaMailProperties(getMailProperties());
javaMailSender.setDefaultEncoding("UTF-8");
return javaMailSender;
}
private Properties getMailProperties() {
Properties pt = new Properties();
pt.put("mail.smtp.socketFactory.port", socketPort);
pt.put("mail.smtp.auth", auth);
pt.put("mail.smtp.starttls.enable", starttls);
pt.put("mail.smtp.starttls.required", startlls_required);
pt.put("mail.smtp.socketFactory.fallback", fallback);
pt.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
return pt;
}
}
- Javaコードを作成
import org.springframework.mail.javamail.JavaMailSender; private final JavaMailSender javaMailSender; @Autowired public (JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public String sendEmail() throws Exception { String to = ""; // 送る対象 MimeMessage message = javaMailSender.createMimeMessage(); message.addRecipients(RecipientType.TO, to); createEmail(message); try { // メール転送 javaMailSender.send(message); } catch (Exception e) { throw new IllegalArgumentException(); } return emailCodeForVerify; } private void createEmail(MimeMessage message) throws Exception { message.setSubject(Title"); // タイトル String msgText = ""; msgText += "<div style='margin:20px;'>"; msgText += "<h1>Hello</h1>"; msgText += "<br>"; msgText += "<p>This is content<p>"; msgText += "</div>"; message.setText(msgText, "utf-8", "html"); message.setFrom(new InternetAddress("メール住所", "受信者に表示される名前")); }