簡體   English   中英

如何配置spring-boot以通過sendGrid發送電子郵件?

[英]How to configure spring-boot to send email via 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對其進行重寫。

我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);
  }
}

我也找到了以下課程: 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集成在一起。

但是我找不到這種集成的完整示例。
請與我分享示例嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM