Search code examples
javasendgrid-api-v3

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


I am using SendGrid API v3 for Java. It works and does the job. However, if the sender is, say, [email protected], the recipient sees only that very [email protected]. What I try to accomplish is that the recipient sees also a simple name (for example, Hello World <[email protected]>) like this:

Example sender address and name

(Above, note that the actual address is noreply@k..., yet it is preceded with Kela Fpa.)

How can I do that programmatically?


Solution

  • Without your code, it's hard to suggest exactly what to do, but according to their API documentation, the endpoint does in fact support an optional 'name' attribute for the sender

    Taking a further glance at their Java API's source code, it looks like the example, which is this:

    import com.sendgrid.*;
    import java.io.IOException;
    
    public class Example {
      public static void main(String[] args) throws IOException {
        Email from = new Email("[email protected]");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("[email protected]");
        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;
        }
      }
    }
    

    Using the source code, you can supply a "name" to the Email constructor, as seen here Could be re-worked into this:

    import com.sendgrid.*;
    import java.io.IOException;
    
    public class Example {
      public static void main(String[] args) throws IOException {
        Email from = new Email("[email protected]", "John Doe");
        String subject = "Sending with SendGrid is Fun";
        Email to = new Email("[email protected]", "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;
        }
      }
    }
    

    Note, the email constructors being changed.

    If you're not using the Mail helper class for some reason, please let me know and I can re-work an example maybe.