【问题标题】:Sending email using Spring boot java使用 Spring Boot java 发送电子邮件
【发布时间】:2016-01-08 18:51:51
【问题描述】:

我正在尝试使用 spring mvc 发送带有 java 邮件的电子邮件。我也尝试使用 java config 而没有 xml 配置并尝试使用 smtp 而不是 gmail 服务器。请有一个很好的例子或有人可以提供一个例子。我遇到的每个示例都使用 xml 配置。感谢您的帮助

【问题讨论】:

  • 如果你使用的是spring boot,你可以使用spring boot的starter email。我在博客上写过here

标签: spring-boot jakarta-mail


【解决方案1】:

SendGrid 有一个非常好的 Java 库,他们有一个免费计划,您可以每月免费发送 12k 封电子邮件。只需向他们注册,生成您的 API 密钥,然后...

将以下内容放入您的 pom.xml:

<dependency>
    <groupId>com.sendgrid</groupId>
    <artifactId>sendgrid-java</artifactId>
    <version>2.2.2</version>
</dependency>

以下 Java 代码是您发送电子邮件所需的全部内容:

SendGrid sendgrid = new SendGrid("YOUR_API_KEY_HERE");
SendGrid.Email welcomeMail = new SendGrid.Email();
welcomeMail.addTo(emailAddress);
welcomeMail.addToName("User-san");
welcomeMail.setFrom("welcome@example.com");
welcomeMail.setSubject("Welcome to Example!");
welcomeMail.setText("Thank you for your interest in Example.com! It is still in Beta at the moment but there are a number of exciting features planned. Tell us what you'd like to see.");

try {
    SendGrid.Response response = sendgrid.send(welcomeMail);
    System.out.println(response.getMessage());
} catch (SendGridException sge) {
    sge.printStackTrace();
}

【讨论】:

  • 在这种情况下是通过 WEB API 发送电子邮件吗?我没有看到任何 SMTP 配置。
  • 是的,它正在使用 SendGrid API,因此它将通过他们的 SMTP 服务器进行 go8mg。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多