3
votes

I am trying to send a test email using sendgrid to multiple recipients. I used the following as a starting point : https://github.com/sendgrid/sendgrid-google-java

I would all like the users receiving the email also be able to see all the other users on the TO field when they receive the email. Using the mail.addTo API sends the email to all the users however the email is sent individually to all of them (they can't see who all they message went to).

Basically my use case is to send an email to a few users and they should be able to "Reply all" and start communicating with each other. How can I achieve this using appengine/sendgrid/java?

5

5 Answers

6
votes

Something must have changed in the implementation, I'm using sendgrid-java-3.1.0.jar.

My code,

Mail mail = new Mail();
...
Personalization p1 = new Personalization();
p1.addTo(new Email("[email protected]"));
p1.addBcc(new Email("[email protected]"));
mail.addPersonalization(p1);

This appears to work as expected.

2
votes

If you want that users could see all list of recipients you can create one personalization for all user list:

var mail = new Mail();
var personalization = new Personalization();
personalization.addTo(new Email([email protected]), name1));
personalization.addTo(new Email([email protected]), name2));
mail.addPersonalization(personalization);

If you want that user could see only themselves - you can specify personalization for each recipient:

var mail = new Mail();
var personalization1 = new Personalization();
personalization1.addTo(new Email([email protected]), name1));
var personalization2 = new Personalization();
personalization2.addTo(new Email([email protected]), name2));
mail.addPersonalization(personalization);
1
votes

Uisng Sendgrid v3 api.

To achieve this - Add multiple to_email addresses to Personalization object, then add the Personalization to the Mail object.

 Mail mail = new Mail();
 ....
 String[] toEmails = {"[email protected]","[email protected]","[email protected]"};      
 Personalization personalization = new Personalization();

 for (int i = 0, size = toEmails.length; i < size; i++) {
    personalization.addTo(new Email(toEmails[i]));             
 }
 mail.addPersonalization(personalization);
1
votes

Sendgrid allows you to send mail to multiple users at time by creating different personalizations. You just need to create object on personalization for each user set fields that you need. Than add personalization to mail and send that mail. You can refer following code:

    DynamicTemplatePersonalization personalization = null;
    Email from = new Email("from-email");

    Mail mail = new Mail();
    for (UserModel admin : admins) {
        personalization = new DynamicTemplatePersonalization();
        personalization.addTo(new Email(admin.getEmail()));
        personalization.addDynamicTemplateData("admin_first_name", admin.getFirstName());
        personalization.addDynamicTemplateData("admin_last_name", admin.getLastName());
        mail.addPersonalization(personalization);
    }
    mail.setFrom(from);
0
votes

I would recommend adding the addCc() method instead. This way all of your recipients will be able to see each other's email addresses.