Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

@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.

I googled this topic and found that java has sendGrid api like this:

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("[email protected]");
    email.setFrom("[email protected]");
    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:

# 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.

But I could not find full example of this integration.
Please share exmple with me?

like image 397
gstackoverflow Avatar asked Jan 25 '18 22:01

gstackoverflow


1 Answers

Spring-Boot autoconfigures SendGrid if it's on the classpath.

Maven Dependency

Include the library as a maven dependency (see https://github.com/sendgrid/sendgrid-java) eg. using gradle:

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

Spring Boot Sendgrid Configuration Properties

Configure the SendGrid Properties (see 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. See https://github.com/sendgrid/sendgrid-java for examples on how to use it.

class SendGridMailService {

    SendGrid sendGrid;

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

    void sendMail() {
        Email from = new Email("[email protected]");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("[email protected]");
        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) {
            // ...
        }
    }
}
like image 168
fateddy Avatar answered Nov 17 '22 11:11

fateddy