如何配置 spring lemon 通过 Sendgrid 发送邮件?

How to configure spring lemon to send emails via Sendgrid?

我通过 sendgrid 的 SMTP api 成功发送了电子邮件,没有更改我的 spring-lemon 代码。我只是通过在 cloudfoundry 上配置环境变量来完成的。

我现在想使用 sendgrid java library

然后我创建了以下 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);
}

如何让 spring-lemon 使用此 class 而不是 SmtpMailSender?

自定义实现 MailSender,并将其配置为 bean 应该会抑制 Spring Lemon 的 SmtpMailSender 配置,因为 Spring Lemon 的 MailConfiguration class 被注释为 @ConditionalOnMissingBean(MailSender.class)。 (请参阅蓝图书第 79 页 - 允许开发人员提供他们的实现

@ConditionalOnMissingBean 是臭名昭著的,但我已经测试过它在这种情况下可以正常工作。