0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GmailEメール送信機能の実装

Last updated at Posted at 2024-02-07

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

  1. build.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

  3. application.propertiesに以下の内容を作成

    AdminMail.id = Email
    AdminMail.password = 2の作成ボタンをクリックすると出てくるパスワード
    
  4. 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;
        }
    }
  1. 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("メール住所", "受信者に表示される名前"));
    
    }
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?