【问题标题】:Sending email using Java and Spring Boot使用 Java 和 Spring Boot 发送电子邮件
【发布时间】:2021-01-26 13:58:18
【问题描述】:

我是使用 java 发送电子邮件的新手。我有一个 Spring Boot 项目,我想从那里发送电子邮件。我已经尝试了互联网上的几个示例。给出我试过的两个例子的 URL:

https://www.mkyong.com/spring-boot/spring-boot-how-to-send-email-via-smtp/ 
https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

但没有一个工作。谁能告诉我在这些示例中我必须进行哪些自定义以及我必须更改哪些值才能使其正常工作?就像我将地址更改为我必须发送电子邮件的地址一样,用户名和密码是什么?他们的用户名、密码是谁的?

【问题讨论】:

  • 用户名和密码是您在邮件服务器上的帐户。找到 JavaMail FAQ 并从那里开始。如果您需要帮助,您需要告诉我们更多关于您的申请要求、您已经尝试过的内容以及失败的原因。

标签: spring-boot java-8 jakarta-mail


【解决方案1】:

您可以使用 Sendgrid API。您每天可以发送 100 封电子邮件而无需付费,完全免费。需要创建 Sendgrid 帐户并生成 API 密钥以发送电子邮件。

参考:

https://sendgrid.com/docs/for-developers/sending-email/v3-java-code-example/

【讨论】:

    【解决方案2】:

    Ogham 库。发送电子邮件的代码很容易编写,您甚至可以使用模板来编写电子邮件的内容。它比其他库更易于使用,因为您不需要处理技术问题(例如在 HTML 中内联图像和样式,它是自动完成的)。 您甚至可以使用 SMTP 服务器在本地测试您的代码,以在通过 SMTP 提供商发送电子邮件之前检查您的电子邮件结果。 可以使用 SMTP 协议或通过提供商 API(如 SendGrid)发送电子邮件。 它与 Spring Boot 兼容,并且更易于使用,因为您不需要实例化服务。

    package fr.sii.ogham.sample.springboot.email;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.RestController;
    
    import fr.sii.ogham.core.exception.MessagingException;
    import fr.sii.ogham.core.service.MessagingService;
    import fr.sii.ogham.email.message.Email;
    
    @SpringBootApplication
    @PropertySource("application-email-basic.properties")   // just needed to be able to run the sample
    public class BasicSample {
    
        public static void main(String[] args) throws MessagingException {
            SpringApplication.run(BasicSample.class, args);
        }
        
        @RestController
        public static class EmailController {
            // Messaging service is automatically created using Spring Boot features
            // The configuration can be set into application-email-basic.properties
            // The configuration files are stored into src/main/resources
            @Autowired
            MessagingService messagingService;
        
            @PostMapping(value="api/email/basic")
            @ResponseStatus(HttpStatus.CREATED)
            public void sendMail(@RequestParam("subject") String subject, @RequestParam("content") String content, @RequestParam("to") String to) throws MessagingException {
                // send the email using fluent API
                messagingService.send(new Email()
                                        .subject(subject)
                                        .body().string(content)
                                        .to(to));
            }
        }
    
    }
    

    还有很多其他featuressamples/spring samples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2017-08-03
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      相关资源
      最近更新 更多