java - 如何配置 spring-boot 通过 sendGrid 发送邮件?

标签 java html spring email sendgrid

目前我将我的应用程序配置为通过 spring-mail 发送电子邮件,我的代码如下所示:

@Autowired
private JavaMailSender sender;
.....
//send email 
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,
                    MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                    StandardCharsets.UTF_8.name());
Template template = freemarkerConfig.getTemplate(templateFileName);
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);    
helper.setTo(to);
helper.setText(html, true);
helper.setSubject(subject);
helper.setFrom(from);
sender.send(message);

现在我的任务是使用 sendGrid 重写它。

我用谷歌搜索了这个主题,发现 java 有类似 this 的 sendGrid api :

import com.sendgrid.*;

public class SendGridExample {
  public static void main(String[] args) {
    SendGrid sendgrid = new SendGrid("SENDGRID_APIKEY");

    SendGrid.Email email = new SendGrid.Email();

    email.addTo("test@sendgrid.com");
    email.setFrom("you@youremail.com");
    email.setSubject("Sending with SendGrid is Fun");
    email.setHtml("and easy to do anywhere, even with Java");

    SendGrid.Response response = sendgrid.send(email);
  }
}

我还找到了以下类(class):SendGridAutoConfiguration 我还遇到了来自 there 的以下片段:

# SENDGRID (SendGridAutoConfiguration)
spring.sendgrid.api-key= # SendGrid api key (alternative to username/password).
spring.sendgrid.username= # SendGrid account username.
spring.sendgrid.password= # SendGrid account password.
spring.sendgrid.proxy.host= # SendGrid proxy host.
spring.sendgrid.proxy.port= # SendGrid proxy port.

看起来 spring boot 已经与 sendGrid 集成。

但我找不到这种集成的完整示例。
请与我分享示例?

最佳答案

Spring-Boot 自动配置 SendGrid 如果它在类路径上。

Maven 依赖

将库作为 Maven 依赖项包含在内(参见 https://github.com/sendgrid/sendgrid-java),例如。使用渐变:

compile 'com.sendgrid:sendgrid-java:4.1.2'

Spring Boot Sendgrid 配置属性

配置 SendGrid 属性(参见 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)

# SENDGRID (SendGridAutoConfiguration)
spring.sendgrid.api-key= # SendGrid api key (alternative to username/password).
spring.sendgrid.username= # SendGrid account username.
spring.sendgrid.password= # SendGrid account password.
spring.sendgrid.proxy.host= # SendGrid proxy host. (optional)
spring.sendgrid.proxy.port= # SendGrid proxy port. (optional)

用法

Spring Boot 自动创建 SendGrid Bean。 参见 https://github.com/sendgrid/sendgrid-java有关如何使用它的示例。

class SendGridMailService {

    SendGrid sendGrid;

    public SendGridMailService(SendGrid sendGrid) {
        this.sendGrid = sendGrid;
    }

    void sendMail() {
        Email from = new Email("test@example.com");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("test@example.com");
        Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
        Mail mail = new Mail(from, subject, to, content);

        Request request = new Request();
        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = this.sendGrid.api(request);
            sendGrid.api(request);

            // ...
        } catch (IOException ex) {
            // ...
        }
    }
}

关于java - 如何配置 spring-boot 通过 sendGrid 发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48453077/

相关文章:

javascript - 如何在只知道它的索引的情况下获得段落内单词的边界矩形?

spring - 如何从 java 代码配置 spring boot 服务器(初始化时)?

java - 为什么jaxb会生成这样的代码?

java - 基于自定义 Gradle Java 的插件不起作用

html - 在不使用 vh 的情况下获取带有 % viewport 的标题的最佳方法

php - 网页下拉菜单的默认值

database - 使用 JPA 时避免来自数据库的陈旧数据?

java - Reactjs/Java Spring Boot - RequestMethod.POST - 空值

java - 保留文件 IO 中的换行符和间距

java - spring java方法是否通过@Value字符串文字获取字段?