<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # SpringBoot – 發送帶有附件的電子郵件 > 原文: [https://howtodoinjava.com/spring-boot2/send-email-with-attachment/](https://howtodoinjava.com/spring-boot2/send-email-with-attachment/) 了解如何借助[`JavaMailSender`](https://howtodoinjava.com/spring-core/send-email-with-spring-javamailsenderimpl-example/)在 spring boot 應用程序中發送電子郵件,以發送簡單電子郵件以及帶有附件的電子郵件。 ## 1\. Maven 在 Spring Boot 應用程序中,包括`spring-boot-starter-mail`依賴項。 `pom.xml` ```java <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> ``` ## 2\. SMTP 配置 使用 spring boot,我們可以在`application.properties`文件中配置 SMTP 設置。 #### 2.1. 郵箱 `application.properties` ```java debug=true spring.mail.host=smtp.gmail.com spring.mail.port=25 spring.mail.username=admin@gmail.com spring.mail.password=xxxxxx # Other properties spring.mail.properties.mail.debug=true spring.mail.properties.mail.transport.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000 # TLS , port 587 spring.mail.properties.mail.smtp.starttls.enable=true # SSL, post 465 #spring.mail.properties.mail.smtp.socketFactory.port = 465 #spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory ``` > 為了能夠使用兩因素身份驗證并關閉“允許不太安全的應用程序”選項,強烈建議使用應用程序密碼代替 Gmail 密碼。 > > 請參閱:[https://support.google.com/accounts/answer/185833](https://support.google.com/accounts/answer/185833) 如果您可以訪問下面的 SMTP 服務器,請在下面自由使用。 #### 2.2. Outlook 如果它是某些公司服務器,請從管理員那里查找此信息。 `Outlook Configuration` ```java spring.mail.host=smtp-mail.outlook.com spring.mail.port=587 spring.mail.username=outlookuserid@outlook.com spring.mail.password=xxxxxx spring.mail.properties.mail.protocol=smtp spring.mail.properties.mail.tls=true spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.ssl.trust=smtp-mail.outlook.com ``` #### 2.3. AWS SES 從您的 AWS 設置頁面中找到相關配置。[閱讀更多](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-email.html)。 `AWS SES Configuration` ```java spring.mail.host=email-smtp.us-east-1.amazonaws.com spring.mail.port=465 spring.mail.username=xxxxxxxxxxx spring.mail.password=xxxxxxxxxxx spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enavle=true spring.mail.properties.mail.protocol=smtps spring.mail.properties.mail.smtps.auth=true ``` ## 3\. 預先配置的電子郵件模板 我們可以創建電子郵件模板以供每次重用 – 我們必須定期或在任何特定事件或持續時間內發送電子郵件。 在給定的示例中,我們正在使用[`SimpleMailMessage`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mail/SimpleMailMessage.html)創建一個非常簡單的純文本電子郵件。 `EmailConfig.java` ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.SimpleMailMessage; @Configuration public class EmailConfig { @Bean public SimpleMailMessage emailTemplate() { SimpleMailMessage message = new SimpleMailMessage(); message.setTo("user@gmail.com"); message.setFrom("admin@gmail.com"); message.setSubject("Important email"); message.setText("FATAL - Application crash. Save your job !!"); return message; } } ``` ## 4\. 發送簡單的電子郵件 為了發送不同類型的電子郵件,我們創建了一個`EmailService`類,該類可以采用在發送電子郵件之前在電子郵件中設置所需的參數。 `EmailService.java` ```java @Service("emailService") public class EmailService { @Autowired private JavaMailSender mailSender; @Autowired private SimpleMailMessage preConfiguredMessage; /** * This method will send compose and send the message * */ public void sendMail(String to, String subject, String body) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(body); mailSender.send(message); } /** * This method will send a pre-configured message * */ public void sendPreConfiguredMail(String message) { SimpleMailMessage mailMessage = new SimpleMailMessage(preConfiguredMessage); mailMessage.setText(message); mailSender.send(mailMessage); } } ``` ## 5\. 發送帶有附件的電子郵件 使用[`MimeMessageHelper`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mail/javamail/MimeMessageHelper.html)發送多媒體電子郵件,該電子郵件用于配置[`MimeMessage`](https://docs.oracle.com/javaee/7/api/javax/mail/internet/MimeMessage.html)。 這些 mime 消息是富文本電子郵件。 `Email with attachment` ```java @Autowired private JavaMailSender mailSender; public void sendMailWithAttachment(String to, String subject, String body, String fileToAttach) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); mimeMessage.setFrom(new InternetAddress("admin@gmail.com")); mimeMessage.setSubject(subject); mimeMessage.setText(body); FileSystemResource file = new FileSystemResource(new File(fileToAttach)); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.addAttachment("logo.jpg", file); } }; try { mailSender.send(preparator); } catch (MailException ex) { // simply log it and go on... System.err.println(ex.getMessage()); } } ``` ## 6\. 發送帶有內嵌圖像的電子郵件 富文本格式包括介于文本內容之間的媒體內容。 為此,我們必須使用`MimeMessageHelper`的`addInline()`方法。 `Email with inline images` ```java @Autowired private JavaMailSender mailSender; public void sendMailWithInlineResources(String to, String subject, String fileToAttach) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); mimeMessage.setFrom(new InternetAddress("admin@gmail.com")); mimeMessage.setSubject(subject); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true); FileSystemResource res = new FileSystemResource(new File(fileToAttach)); helper.addInline("identifier1234", res); } }; try { mailSender.send(preparator); } catch (MailException ex) { // simply log it and go on... System.err.println(ex.getMessage()); } } ``` ## 7\. Spring Boot 發送電子郵件演示 要使用上述配置發送電子郵件,請更新`application.properties`文件中的`spring.mail.username`和`spring.mail.password`屬性。 現在執行`EmailService`的方法發送電子郵件。 `Application.java` ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private EmailService emailService; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) { emailService.sendMail("lokeshgupta1981@gmail.com", "Hi", "Ho ho ho"); emailService.sendPreConfiguredMail("Ho ho ho"); } } ``` 請給我發送有關**通過 SpringBoot**發送電子郵件的問題。 學習愉快! [下載源碼](https://github.com/lokeshgupta1981/SpringExamples/tree/master/email)
                  <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>

                              哎呀哎呀视频在线观看