【发布时间】:2018-07-27 20:21:39
【问题描述】:
我正在尝试在 Spring 中发送堆栈跟踪电子邮件。这是我目前所拥有的:
# application.properties
spring.sendgrid.api-key="SG.o1o9MNb_QfqpasdfasdfasdfpLX3Q"
在我的 ErrorController 中:
// Send Mail
Email from = new Email("david@no-reply.com");
String subject = "Exception " + message.toString();
Email to = new Email("tom@gmail.com");
Content content = new Content("text/plain", trace);
Mail mail = new Mail(from, subject, to, content);
Request r = new Request();
try {
SendGrid sendgrid = new SendGrid();
r.setMethod(Method.POST);
r.setEndpoint("mail/send");
r.setBody(mail.build());
Response response = sendgrid.api(request);
sendgrid.api(r);
} catch (IOException ex) {
}
但是,它似乎没有正确初始化 SendGrid 对象(使用来自 application.properties 的 API 密钥)。执行上述操作的正确方法是什么?
【问题讨论】:
-
Spring 会自动配置 SendGrid bean,你不应该自己创建它,而是作为 bean 注入。
-
@SergiiZhevzhyk 你能告诉我如何调用该方法吗?
标签: java spring spring-boot sendgrid