I am using SendGrid API v3 for Java. It works and does the job. However, if the sender is, say, hello@world.org
, the recipient sees only that very hello@world.org
. What I try to accomplish is that the recipient sees also a simple name (for example, Hello World <hello@world.org>
) like this:
(Above, note that the actual address is noreply@k...
, yet it is preceded with Kela Fpa
.)
How can I do that programmatically?
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("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;
}
}
}
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("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;
}
}
}
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.