1

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

发件人地址和姓名示例

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

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

4

1 回答 1

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

使用源代码,您可以为 Email 构造函数提供一个“名称”,如此处所示可以 重新设计为:

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

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

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

于 2017-12-06T20:06:30.533 回答