简体   繁体   English

如何配置spring-boot以通过sendGrid发送电子邮件?

[英]How to configure spring-boot to send email via sendGrid?

At the moment I configured my application to send email via spring-mail and my code looks like this: 目前,我已将应用程序配置为通过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);

Now I have task to rewrite it using sendGrid. 现在,我有任务使用sendGrid对其进行重写。

I googled this topic and found that java has sendGrid api like this : 我GOOGLE了这个话题,发现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);
  }
}

Also I've found following class: SendGridAutoConfiguration Also I've met following snippet from there : 我也找到了以下课程: 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.

Looks like spring boot has integration with the sendGrid. 看来Spring Boot已与sendGrid集成在一起。

But I could not find full example of this integration. 但是我找不到这种集成的完整示例。
Please share exmple with me? 请与我分享示例吗?

Spring-Boot autoconfigures SendGrid if it's on the classpath. Spring-Boot如果在类路径上,则自动配置SendGrid

Maven Dependency Maven依赖

Include the library as a maven dependency (see https://github.com/sendgrid/sendgrid-java ) eg. 将库包含为Maven依赖项(请参见https://github.com/sendgrid/sendgrid-java ),例如。 using gradle: 使用gradle:

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

Spring Boot Sendgrid Configuration Properties Spring Boot Sendgrid配置属性

Configure the SendGrid Properties (see https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ) 配置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)

Usage 用法

Spring Boot creates the SendGrid Bean automatically. Spring Boot自动创建SendGrid Bean。 See https://github.com/sendgrid/sendgrid-java for examples on how to use it. 有关如何使用它的示例,请参见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) {
            // ...
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM