最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - "The from address does not match a verified Sender Identity" (403 Error) - Stack Overflow

programmeradmin2浏览0评论

I'm using the SendGrid Java SDK in my Spring Boot application to send emails, but I keep getting a 403 error with the message:

"The from address does not match a verified Sender Identity."

Even though:

  • My sender email is verified under Single Sender Verification.
  • I have authenticated my domain in SendGrid.
  • The same API key and payload work correctly with cURL.

Here’s the Java code I’m using:

public void sendMail(String to, String from, String templateId, Map<String, Object> templateModel) {
    Email fromEmail = new Email(from); 
    Email toEmail = new Email(to);
    Mail mail = new Mail();

    mail.setFrom(fromEmail);
    mail.setTemplateId(templateId);

    Personalization personalization = new Personalization();
    personalization.addTo(toEmail);
    templateModel.forEach(personalization::addDynamicTemplateData);
    mail.addPersonalization(personalization);

    SendGrid sg = new SendGrid(sendGridApiKey);
    Request request = new Request();

    try {
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());
        Response response = sg.api(request);
        System.out.println("SendGrid Response: " + response.getStatusCode() + " " + response.getBody());
    } catch (IOException e) {
        throw new RuntimeException("Failed to send email via SendGrid", e);
    }
}

And here’s the example of generated request payload:

{
  "from": { "email": "[email protected]" },
  "personalizations": [{
    "to": [{ "email": "[email protected]" }],
    "dynamic_template_data": {
      "activation_link": "http://localhost:5173/activate?token=83230b58-bf33",
      "name": "john_doe"
    }
  }],
  "template_id": "d-123312bhi321bi"
}

And here’s the error response I get from SendGrid:

{
  "errors": [{
    "message": "The from address does not match a verified Sender Identity.",
    "field": "from",
    "help": null
  }]
}
  • Using cURL with the same API key and payload works fine.
  • Ensuring the API key has "Full Access".
  • Trying a different verified email ([email protected]) as the sender.
  • Explicitly adding Authorization: Bearer API_KEY in the request headers.

Why does the same request work in cURL but fail in the Java SDK? What else should I check?

发布评论

评论列表(0)

  1. 暂无评论