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

SendGrid: how to add a simple non-email address name for the sender

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

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

如何以编程方式执行此操作?

如果没有您的代码,很难确切地建议要做什么,但根据他们的 API 文档,端点实际上支持发件人的可选 'name' 属性

再看他们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;
    }
  }
}

使用源代码,您可以向电子邮件构造函数提供 "name",如 here 所示 可以重新加工成这个:

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;
    }
  }
}

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

如果您出于某种原因没有使用邮件助手 class,请告诉我,我也许可以重做一个示例。