GmailEメール送信機能の実装

:::note info
個人的に作っているウェブサイトからメールを送信する機能を作るようになって記録します。
:::

1. build.gradle 作成
    ```gradle
    dependencies {
         implementation 'org.springframework.boot:spring-boot-starter-mail'
    }
    ```

2. 以下のURLからアプリパスワードを作成
https://accounts.google.com/v3/signin/challenge/pwd?continue=https://myaccount.google.com/apppasswords&service=accountsettings
![스크린샷 2024-02-07 17.59.54.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3567846/c51cecfd-f1ba-3161-dbf1-4e5ddf7fe10e.png)

3. application.propertiesに以下の内容を作成
    ```properties
    AdminMail.id = Email
    AdminMail.password = 2の作成ボタンをクリックすると出てくるパスワード
    ```

4. Configを作成
```java
    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;
        }
    }
```
    
5. Javaコードを作成
    ```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("メール住所", "受信者に表示される名前"));
    
    }
    ```