KENDRA SMITH
KENDRA SMITH

Reputation: 105

Sending email using Spring boot java

I am trying to send email with java mail using spring mvc. I am also attempting to do it with java config and without xml configuration and attempting to use smtp and not gmail server. Please is there a good example or could someone provide an example. Every example i have come across uses xml configuration. Thanks for the help

Upvotes: 0

Views: 2487

Answers (1)

james_s_tayler
james_s_tayler

Reputation: 1943

SendGrid have a really nice Java library for this and they have a free plan where you can send 12k emails per month for free. Just sign up with them, generate your API key and then...

Put the following in your pom.xml:

<dependency>
    <groupId>com.sendgrid</groupId>
    <artifactId>sendgrid-java</artifactId>
    <version>2.2.2</version>
</dependency>

And the following Java code is all you need to be able to send an email:

SendGrid sendgrid = new SendGrid("YOUR_API_KEY_HERE");
SendGrid.Email welcomeMail = new SendGrid.Email();
welcomeMail.addTo(emailAddress);
welcomeMail.addToName("User-san");
welcomeMail.setFrom("[email protected]");
welcomeMail.setSubject("Welcome to Example!");
welcomeMail.setText("Thank you for your interest in Example.com! It is still in Beta at the moment but there are a number of exciting features planned. Tell us what you'd like to see.");

try {
    SendGrid.Response response = sendgrid.send(welcomeMail);
    System.out.println(response.getMessage());
} catch (SendGridException sge) {
    sge.printStackTrace();
}

Upvotes: 1

Related Questions