Send Mail using SendGrid Smtp

SendGrid provides 2 ways of integration, 

  • SMTP Relay - Easiest 
  • Web API - Recommended, fastest and reliable

SendGrid provides a lot of helpful API, helper classes, and methods. but it takes a lot of code change and testing.

SendGrid provides an SMTP Relay which is the easiest way to integrate with SendGrid. it requires minimal code change.

To send mail, a service requires at least hostName, port, username, and password. SendGrid provides its own values which need to replace with existing values.

SendGrid SMTP Relay API keys

SendGrid requires hostName, port, username, and password to integrate with SMTP Relay.

Go to https://app.sendgrid.com/guide/integrate/langs/smtp#settings/api_keys

Enter Key Name and Generate Key.

Key       value
Server : smtp.sendgrid.net
Ports :
25, 587 (for unencrypted/TLS connections)
465 (for SSL connections)
Username : apikey
Password : YOUR_API_KEY
package org.wesome.sendgrid;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SendGridApplication {
    public static void main(String[] args) {
        SpringApplication.run(SendGridApplication.class, args);
    }
}
package org.wesome.sendgrid.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.wesome.sendgrid.model.Mail;
import org.wesome.sendgrid.service.SendMailService;

@RestController
public class SendGridController {

    @Autowired
    SendMailService service;

    @PostMapping("/send")
    public ResponseEntity<String> sendMail(@RequestBody Mail mail) {
        service.sendMail(mail);
        return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK);
    }
}
package org.wesome.sendgrid.model;

import lombok.Data;

@Data
public class Mail {
    private String recipient;
    private String subject;
    private String message;
}
package org.wesome.sendgrid.service;

import org.springframework.stereotype.Service;
import org.wesome.sendgrid.model.Mail;

@Service
public interface SendMailService {
    void sendMail(Mail mail);
}
package org.wesome.sendgrid.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.wesome.sendgrid.model.Mail;

@Service
public class SendMailServiceImpl implements SendMailService {
    @Autowired
    JavaMailSender javaMailSender;

    @Override
    public void sendMail(Mail mail) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(mail.getRecipient(), mail.getRecipient());
        msg.setSubject(mail.getSubject());
        msg.setText(mail.getMessage());
        msg.setFrom("[email protected]");
        msg.setReplyTo("[email protected]");
        javaMailSender.send(msg);
    }
}
spring.mail.host=smtp.sendgrid.net
spring.mail.port=587
spring.mail.username=apikey
spring.mail.password=SendGrid API Key
# Other smtp properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
# TLS port 587
spring.mail.properties.mail.smtp.starttls.enable=true
curl --location --request POST 'http://localhost:8080/send' \
--header 'Content-Type: application/json' \
--data-raw '{
    "recipient": "[email protected]",
    "subject": "test email1",
    "message": "learning sendgrid"
}'

 

follow us on