Spring Boot – SMTP를 통해 이메일을 보내는 방법

19591 단어 springspringboot

프로젝트 디렉토리





메이븐



이메일을 보내려면 spring-boot-starter-mail을 선언하고 JavaMail 종속성을 가져옵니다.
  • pom.xml

  • <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>spring-boot-send-email</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-send-email</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    


    프로젝트 종속성을 표시합니다.

    mvn dependency:tree
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------< com.example:spring-boot-send-email >-----------------
    [INFO] Building spring-boot-send-email 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ spring-boot-send-email ---
    [INFO] com.example:spring-boot-send-email:jar:0.0.1-SNAPSHOT
    [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.7.1:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.7.1:compile
    [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.1:compile
    [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.11:compile
    [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.11:compile
    [INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
    [INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
    [INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
    [INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
    [INFO] |  |  \- org.yaml:snakeyaml:jar:1.30:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.7.1:compile
    [INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:compile
    [INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:compile
    [INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:compile
    [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.3:compile
    [INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.3:compile
    [INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:compile
    [INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.1:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.64:compile
    [INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.64:compile
    [INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.64:compile
    [INFO] |  +- org.springframework:spring-web:jar:5.3.21:compile
    [INFO] |  |  \- org.springframework:spring-beans:jar:5.3.21:compile
    [INFO] |  \- org.springframework:spring-webmvc:jar:5.3.21:compile
    [INFO] |     +- org.springframework:spring-aop:jar:5.3.21:compile
    [INFO] |     +- org.springframework:spring-context:jar:5.3.21:compile
    [INFO] |     \- org.springframework:spring-expression:jar:5.3.21:compile
    [INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.7.1:runtime
    [INFO] |  +- org.springframework.boot:spring-boot:jar:2.7.1:compile
    [INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.1:compile
    [INFO] +- org.springframework.boot:spring-boot-configuration-processor:jar:2.7.1:compile
    [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.1:test
    [INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.7.1:test
    [INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.1:test
    [INFO] |  +- com.jayway.jsonpath:json-path:jar:2.7.0:test
    [INFO] |  |  +- net.minidev:json-smart:jar:2.4.8:test
    [INFO] |  |  |  \- net.minidev:accessors-smart:jar:2.4.8:test
    [INFO] |  |  |     \- org.ow2.asm:asm:jar:9.1:test
    [INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.36:compile
    [INFO] |  +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
    [INFO] |  |  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
    [INFO] |  +- org.assertj:assertj-core:jar:3.22.0:test
    [INFO] |  +- org.hamcrest:hamcrest:jar:2.2:test
    [INFO] |  +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test
    [INFO] |  |  +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
    [INFO] |  |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
    [INFO] |  |  |  +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
    [INFO] |  |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
    [INFO] |  |  +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
    [INFO] |  |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
    [INFO] |  |     \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
    [INFO] |  +- org.mockito:mockito-core:jar:4.5.1:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.12.11:test
    [INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.12.11:test
    [INFO] |  |  \- org.objenesis:objenesis:jar:3.2:test
    [INFO] |  +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test
    [INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
    [INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
    [INFO] |  +- org.springframework:spring-core:jar:5.3.21:compile
    [INFO] |  |  \- org.springframework:spring-jcl:jar:5.3.21:compile
    [INFO] |  +- org.springframework:spring-test:jar:5.3.21:test
    [INFO] |  \- org.xmlunit:xmlunit-core:jar:2.9.0:test
    [INFO] \- org.springframework.boot:spring-boot-starter-mail:jar:2.7.1:compile
    [INFO]    +- org.springframework:spring-context-support:jar:5.3.21:compile
    [INFO]    \- com.sun.mail:jakarta.mail:jar:1.6.7:compile
    [INFO]       \- com.sun.activation:jakarta.activation:jar:1.2.2:compile
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.153 s
    [INFO] Finished at: 2022-07-18T14:40:39+07:00
    [INFO] ------------------------------------------------------------------------
    


    지메일 SMTP



    이 예는 TLS(포트 587) 및 SSL(포트 465)로 테스트된 Gmail SMTP 서버를 사용하고 있습니다. 이것을 읽으십시오 Gmail SMTP
  • 응용 프로그램 속성


  • 메모



    Gmail SMTP의 경우 계정이 2단계 인증을 사용하는 경우 앱 비밀번호를 생성하세요.
  • 액세스 링크: https://support.google.com/accounts/answer/185833?p=InvalidSecondFactor

  • URL : https://myaccount.google.com/security



    새로 생성된 앱 비밀번호를 spring.mail.password에 넣습니다.


  • 이메일 보내기



    Spring은 JavaMail APIs 위에 JavaMailSender 인터페이스를 제공합니다.
  • 일반 텍스트 이메일 보내기

  • HTML 이메일과 첨부 파일을 보냅니다.


  • 데모



    Spring Boot를 시작하면 이메일이 전송됩니다.

    package com.example.sendemail;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.io.IOException;
    
    @SpringBootApplication
    public class SpringBootSendEmailApplication implements CommandLineRunner {
        @Autowired
        private JavaMailSender javaMailSender;
        public static void main(String[] args) {
            SpringApplication.run(SpringBootSendEmailApplication.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Sending Email...");
            sendEmail();
            System.out.println("Done");
        }
    
        void sendEmailWithAttachment() throws MessagingException, IOException {
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true);
            helper.setTo("[email protected]");
            helper.setSubject("Testing from Spring Boot");
            helper.setText("<h1>Check attachment for image!</h1>", true);
            helper.addAttachment("my_photo.png", new ClassPathResource("android.png"));
            javaMailSender.send(msg);
    
        }
        void sendEmail() {
            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setTo("[email protected]", "[email protected]");
            msg.setSubject("Testing from Spring Boot");
            msg.setText("Hello World \n Spring Boot Email");
            javaMailSender.send(msg);
        }
    
    }
    





    소스 코드



    https://github.com/java-cake/spring-boot/tree/main/spring-boot-send-email

    좋은 웹페이지 즐겨찾기