如何在Quarkus中通过SendGrid WebAPI实现邮件发送?
Quarkus 集成 SendGrid WebAPI 实现方案
你需要的基于WebAPI的集成方案不需要依赖Quarkus官方邮件扩展,直接对接SendGrid官方Java SDK即可,具体实现步骤如下:
1. 引入SendGrid Java SDK依赖
Maven项目在pom.xml中添加以下依赖:
<dependency> <groupId>com.sendgrid</groupId> <artifactId>sendgrid-java</artifactId> <version>4.10.1</version> <!-- 可根据官方最新版本调整版本号 --> </dependency>
Gradle项目添加配置:
implementation 'com.sendgrid:sendgrid-java:4.10.1'
2. 新增基础配置
在application.properties中添加SendGrid配置项,不要硬编码密钥,生产环境可通过环境变量或者配置中心注入:
# SendGrid API密钥,在SendGrid后台生成 sendgrid.api-key=你的SendGrid API密钥 # 默认发件人邮箱,需要在SendGrid后台完成验证 sendgrid.from-email=noreply@yourdomain.com
3. 封装SendGrid客户端与发送逻辑
3.1 生产SendGrid单例客户端
利用Quarkus CDI机制生成全局复用的SendGrid客户端,避免重复创建实例:
import com.sendgrid.SendGrid; import org.eclipse.microprofile.config.inject.ConfigProperty; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Produces; @ApplicationScoped public class SendGridClientProducer { @ConfigProperty(name = "sendgrid.api-key") String apiKey; @Produces @ApplicationScoped public SendGrid buildSendGridClient() { return new SendGrid(apiKey); } }
3.2 封装邮件发送服务
import com.sendgrid.SendGrid; import com.sendgrid.Request; import com.sendgrid.Response; import com.sendgrid.helpers.mail.Mail; import com.sendgrid.helpers.mail.objects.Content; import com.sendgrid.helpers.mail.objects.Email; import org.eclipse.microprofile.config.inject.ConfigProperty; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.io.IOException; @ApplicationScoped public class SendGridMailService { @Inject SendGrid sendGridClient; @ConfigProperty(name = "sendgrid.from-email") String defaultSender; /** * 发送普通文本邮件 * @param receiver 收件人邮箱 * @param subject 邮件主题 * @param content 邮件内容 * @return 发送是否成功 */ public boolean sendTextMail(String receiver, String subject, String content) throws IOException { Email from = new Email(defaultSender); Email to = new Email(receiver); Content mailContent = new Content("text/plain", content); Mail mail = new Mail(from, subject, to, mailContent); Request request = new Request(); request.setMethod(com.sendgrid.Method.POST); request.setEndpoint("mail/send"); request.setBody(mail.build()); Response response = sendGridClient.api(request); // SendGrid WebAPI返回202即表示请求已被接收处理 return response.getStatusCode() == 202; } // 如需发送HTML邮件,仅需将Content的type修改为text/html即可 // 如需使用模板、附件、批量发送等能力,直接调用SendGrid SDK对应原生接口即可 }
4. 调用示例
直接注入封装好的服务即可调用:
import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.QueryParam; import java.io.IOException; @Path("/test-mail") public class TestMailResource { @Inject SendGridMailService mailService; @GET @Path("/send") public String sendTestMail(@QueryParam("receiver") String receiver) throws IOException { boolean sendRes = mailService.sendTextMail(receiver, "测试邮件", "这是通过Quarkus + SendGrid WebAPI发送的测试邮件"); return sendRes ? "邮件发送请求提交成功" : "邮件发送失败"; } }
注意事项
- 生产环境请为SendGrid API密钥配置最小权限,仅开通邮件发送相关权限即可
- 可结合Quarkus提供的
@Retry注解实现发送失败自动重试逻辑 - 相比SMTP协议,WebAPI方式对接更简单、延迟更低,也能使用SendGrid的所有原生高级功能
内容的提问来源于stack exchange,提问作者chandni
相关产品推荐
相关产品推荐

