0

I successfully sent emails via sendgrid's SMTP api, without change to my spring-lemon code. I did it just by configuring environment variables on cloudfoundry.

I would now like to use sendgrid java library

I then created the following class

    SendGrid sendgrid = new SendGrid('api_key');

SendGrid.Email email = new SendGrid.Email();
email.addTo("example@example.com");
email.setFrom("other@example.com");
email.setSubject("Hello World");
email.setText("My first email with SendGrid Java!");

try {
  SendGrid.Response response = sendgrid.send(email);
  System.out.println(response.getMessage());
}
catch (SendGridException e) {
  System.err.println(e);
}

How do I get spring-lemon to use this class instead of SmtpMailSender ?

mozexty
  • 44
  • 1
  • 6
  • The blueprint book says something about @ConditionalOnProperty but I don't know if it suits my particular case. – mozexty Jun 14 '16 at 10:47

1 Answers1

0

Having your custom implementation of MailSender, and configuring that as a bean should suppress Spring Lemon's SmtpMailSender configuration, because Spring Lemon's MailConfiguration class is annotated with @ConditionalOnMissingBean(MailSender.class). (Refer page 79 of the blueprint book - Allowing developers to provide their implementations)

@ConditionalOnMissingBean is notorious, but I had tested it to be working in this case.

Sanjay
  • 8,755
  • 7
  • 46
  • 62
  • I'll try that and let you know. – mozexty Jun 14 '16 at 12:18
  • Should it implement `org.springframework.mail.MailSender` or `com.naturalprogrammer.spring.lemon.mail.MailSender` ? – mozexty Jun 15 '16 at 11:05
  • com.naturalprogrammer.spring.lemon.mail.MailSender – Sanjay Jun 15 '16 at 11:15
  • It worked !. I've created a MailSender implementation, and declared it as a `@Bean` in a `@Configuration` class. But one thing odd is that it's also trying to run while running in development environment, locally. – mozexty Jun 16 '16 at 18:24
  • Yes, because now Spring Lemon has backed away. To use the `MockMailSender`, you can follow the same technique as used in Spring Lemon - see the `MailConfiguration` class and the blueprint book for details. – Sanjay Jun 17 '16 at 06:11