有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用SendGrid API v3 for Java。它起作用了。然而,如果发送方是,比如说,hello@world.org,那么接收方只会看到非常hello@world.org。我试图做到的是,收件人也会看到一个简单的名称(例如,Hello World <hello@world.org>),如下所示:

Example sender address and name

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

我如何通过编程实现这一点


共 (1) 个答案

  1. # 1 楼答案

    如果没有您的代码,很难确切地建议该做什么,但根据他们的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;
        }
      }
    }
    

    使用源代码,您可以向电子邮件构造函数提供一个“名称”,如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;
        }
      }
    }
    

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

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