Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mercadolivre.apimlv2.shared.mail;

import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

@Component
@Primary
@Profile("dev")
public class FakeMailer implements Mailer {
@Override
public void sendText(String to, String subject, String body) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.mercadolivre.apimlv2.shared.mail;

import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import com.sendgrid.helpers.mail.objects.Personalization;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Profile("!dev")
public class SendgridMailer implements Mailer {

private final SendGrid sendGrid;

public SendgridMailer(@Value("${sendgrid.api.key}") String apiKey) {
this.sendGrid = new SendGrid(apiKey);
}

@Override
public void sendText(String to, String subject, String body) {
Email from = new Email("no-reply@mercadolivre.com");
Email toEmail = new Email(to);
Content content = new Content("text/plain", body);
Mail mail = new Mail(from, subject, toEmail, content);

Personalization personalization = new Personalization();
personalization.addTo(toEmail);
mail.addPersonalization(personalization);

Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
sendGrid.api(request);
} catch (IOException ex) {
throw new RuntimeException("Failed to send email", ex);
}
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ spring:
accept-case-insensitive-enums: true
jwt:
secret: jdk02kdadnjndiawd
expTime: 86400
expTime: 86400

sendgrid:
api:
key: YOUR_SENDGRID_API_KEY