<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot 發送電子郵件教程 > 原文: [http://zetcode.com/springboot/email/](http://zetcode.com/springboot/email/) Spring Boot 發送電子郵件教程展示了如何在 Spring Boot 應用中發送電子郵件。 我們使用 Mailtrap 服務。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## Spring Boot 電子郵件示例 在下面的示例中,我們創建一個將電子郵件發送到 Mailtrap 帳戶的應用。 如果沒有帳戶,我們需要注冊一個帳戶。 注冊過程非常簡單快捷。 有一個免費層,每月可發送 500 封電子郵件。 > **注意**:Gmail 不是測試應用的理想選擇。 我們應該使用諸如 Mailtrap 或 Mailgun 之類的在線服務,或者使用由網絡托管公司提供的 SMTP 服務器。 該應用具有 Web 界面來發送電子郵件。 此外,可以通過測試發送電子郵件。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ └───service │ │ EmailService.java │ └───resources │ │ application.properties │ ├───static │ │ index.html │ └───templates │ emailsent.ftl └───test └───java └───com └───zetcode SendEmailApplicationTest.java ``` 這是 Spring Boot 應用的項目結構。 `pom.xml` ```java <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springbootemailex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </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> ``` 我們在`pom.xml`中具有項目依賴項。 對于電子郵件,我們需要聲明`spring-boot-starter-mail`。 `resources/application.properties` ```java spring.main.banner-mode=off spring.mail.protocol=smtp spring.mail.host=smtp.mailtrap.io spring.mail.port=2525 spring.mail.username=0128491df14b6d spring.mail.password=7b6d7ha59a1f08 spring.mail.properties.mail.smtp.auth = true spring.mail.properties.mail.smtp.starttls.enable = true ``` 我們為 Mailtrap 配置電子郵件設置。 這些詳細信息在我們的 Mailtrap 帳戶中提供。 `com/zetcode/MyController.java` ```java package com.zetcode.controller; import com.zetcode.service.EmailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MyController { @Autowired private EmailService emailService; @GetMapping(value = "/sendmail") public String sendmail() { emailService.sendMail("kate@example.com", "Test Subject", "Test mail"); return "emailsent"; } } ``` 控制器包含一個發送電子郵件的映射。 `com/zetcode/service/EmailService.java` ```java package com.zetcode.service; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { private JavaMailSender javaMailSender; public EmailService(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public void sendMail(String toEmail, String subject, String message) { var mailMessage = new SimpleMailMessage(); mailMessage.setTo(toEmail); mailMessage.setSubject(subject); mailMessage.setText(message); mailMessage.setFrom("johndoe@example.com"); javaMailSender.send(mailMessage); } } ``` 電子郵件服務使用`JavaMailSender`和`SimpleMailMessage`發送簡單的電子郵件。 `resources/static/index.html` ```java <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <a href="sendmail">Send mail</a> </body> </html> ``` `index.html`文件是主頁。 它包含一個用于發送電子郵件的錨。 `resources/templates/emailsent.ftl` ```java <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Email sent</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <p> Email was sent </p> </body> </html> ``` 該模板包含一條簡單的消息,該消息在成功發送電子郵件后顯示。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`是設置 Spring Boot 應用的入口。 `com/zetcode/SendEmailApplicationTest.java` ```java package com.zetcode; import com.zetcode.service.EmailService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SendEmailApplicationTest { @Autowired private EmailService emailService; @Test public void testEmail() { emailService.sendMail("frank23@example.com", "Test subject", "Test mail"); } } ``` 這是發送電子郵件的測試。 ```java $ mvn spring-boot:run ``` 應用運行后,我們可以導航到`localhost:8080`。 在本教程中,我們展示了如何在 Spring Boot 中發送電子郵件。 您可能也對相關教程感興趣: [Spring Boot Freemaker 教程](/springboot/freemaker/), [Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看