如何配置Spring Boot以通过SendGrid发送电子邮件?

8

目前,我已经配置了我的应用程序使用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有像这样的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);
  }
}

我还发现了以下类:SendGridAutoConfiguration 我还遇到了以下摘录:这里:

# 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集成。

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

1个回答

7

如果在类路径上,Spring-Boot会自动配置SendGrid

Maven依赖

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

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) {
            // ...
        }
    }
}

默认为 api.sendgrid.com (请参见 https://github.com/sendgrid/sendgrid-java/blob/master/src/main/java/com/sendgrid/SendGrid.java#L79) - 但如果需要,可以进行覆盖。 - fateddy
我应该在什么时候使用这个代理? - gstackoverflow
每当您无法直接与外界通信时,这取决于您的基础设施。如果您不需要它,请将其排除在外。 - fateddy
我有点搞糊涂了。你能把你的想法表达清楚吗? - gstackoverflow
每当你无法直接与外界进行通信时,这取决于你的基础设施。如果不需要它,就把它留出来吧。这正确吗? - gstackoverflow
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接