SendGrid:如何为发件人添加一个简单的非电子邮件地址名称

cod*_*dde 1 java sendgrid-api-v3

我正在为 Java 使用 SendGrid API v3。它可以工作并完成工作。但是,如果发件人hello@world.org,则收件人只会看到hello@world.org. 我试图完成的是,收件人也看到一个简单的名称(例如,Hello World <hello@world.org>),如下所示:

发件人地址和姓名示例

(上面注意,实际地址是noreply@k...,但前面是Kela Fpa。)

我怎样才能以编程方式做到这一点?

Zac*_*aig 5

没有你的代码,很难确切地建议做什么,但根据他们的 API 文档,端点实际上支持发件人的可选“名称”属性

进一步看一下他们的 Java API 的源代码,它看起来像下面的例子:

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) throws IOException {
    Email from = new Email("test@example.com");
    String subject = "Sending with SendGrid is Fun";
    Email to = new Email("test@example.com");
    Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
    Mail mail = new Mail(from, subject, to, content);

    SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
    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(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用的源代码,你可以提供一个“名”,以电子邮件的构造,因为看到这里 可以重新加工成这样的:

import com.sendgrid.*;
import java.io.IOException;

public class Example {
  public static void main(String[] args) throws IOException {
    Email from = new Email("test@example.com", "John Doe");
    String subject = "Sending with SendGrid is Fun";
    Email to = new Email("test@example.com", "Jane Smith");
    Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
    Mail mail = new Mail(from, subject, to, content);

    SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
    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(response.getStatusCode());
      System.out.println(response.getBody());
      System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,电子邮件构造函数正在更改。

如果您出于某种原因没有使用邮件助手类,请告诉我,我可以重新编写一个示例。