5

We're using the new gmail api on iOS to send emails and everything works great for messages with single recipients. When we specify more than one in the "to" field, we get the following error:

Error Domain=com.google.GTLJSONRPCErrorDomain Code=400 "The operation couldn’t be completed. (Invalid to header)

I have verified the content we are sending is in fact a valid rfc822 message.

FlyingHorse
  • 332
  • 4
  • 15
yellowandy
  • 89
  • 1
  • 1
  • 7
  • Getting the same error only using the python client – Chris Watts Aug 20 '14 at 23:13
  • Can you include the code that actually produces the error? The version of iOS would probably be helpful too.. Try readying [this](http://stackoverflow.com/help/how-to-ask) and then editing your question – LeonH Aug 21 '14 at 12:59
  • The version of iOS and sample code is irrelevant to this question, please don't troll. As I mentioned we have verified the rfc822 message sent via the gmail api is valid but results in an error when more than one recipient is specified. It's that simple. – yellowandy Aug 21 '14 at 18:26
  • I have the same issue creating drafts using the C# api. One recipient works fine, multiple recipients gives an "Invalid to header" error message. – David Libido Aug 25 '14 at 12:25
  • Answered here: http://stackoverflow.com/questions/25437820/how-to-send-message-to-multiple-recipients – Eric D Aug 25 '14 at 19:04

5 Answers5

0

You should use a list in your to field.

E.g. :

[ "liz6beigle@hotmail.com",
  "another.one@email.com" ]

Gmail has a limit of bounces and recipients you can send at the same time.

You cannot store multiple emails under a single string. Placing a single email address on each line will give better readability and prevent parsing errors.

Here is a code sample in Java from google. I hope it will help others to understand :

 /**
   * Create a MimeMessage using the parameters provided.
   *
   * @param to Email address of the receiver.
   * @param from Email address of the sender, the mailbox account.
   * @param subject Subject of the email.
   * @param bodyText Body text of the email.
   * @return MimeMessage to be used to send email.
   * @throws MessagingException
   */
  public static MimeMessage createEmail(String to, String from, String subject,
      String bodyText) throws MessagingException {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);
    InternetAddress tAddress = new InternetAddress(to);
    InternetAddress fAddress = new InternetAddress(from);

    email.setFrom(new InternetAddress(from));
    email.addRecipient(javax.mail.Message.RecipientType.TO,
                       new InternetAddress(to));
    email.setSubject(subject);
    email.setText(bodyText);
    return email;
  }

Gmail API : Sending Messages

Check the first code sample.

  • Thank you for your help but you are incorrect with the statement that you can't specify multiple addresses in the to line. If you take a look at RFC 822 you'll see that this is infact the recommended approach to specify multiple recipients. – yellowandy Aug 25 '14 at 18:14
0

This was a regression but we finished deploying the fix on Monday, 2014-08-25.

Eric D
  • 6,901
  • 1
  • 15
  • 26
0

I think you can do the following

get the 'To' fields as this "test1@example.com, test2@example.com"

then split it with ','

String mail1 = "test1@example.com";
String mail2 = "test2@example.com";

then do this

email.addRecipient(javax.mail.Message.RecipientType.TO,
                   new InternetAddress(mail1));
email.addRecipient(javax.mail.Message.RecipientType.TO,
                   new InternetAddress(mail2));

I checked this it worked

Pradeep
  • 11
0

You can use comma separated emails and loop through those emails

    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage email = new MimeMessage(session);
    Multipart multiPart = new MimeMultipart("alternative");
    email.setFrom(new InternetAddress(from));


    String to = "xyz@gmail.com,sjaksks@gmail.cm,hysrtt@gmail.com";
    String[] split = to.split(",");
    for(int i=0;i<split.length;i++) {
        email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(split[i]));
    }


    email.setSubject(subject);

    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setText(text, "utf-8");

    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(html, "text/html; charset=utf-8");

    multiPart.addBodyPart(textPart);
    multiPart.addBodyPart(htmlPart);
    email.setContent(multiPart);
    return email;
MD AFSAR ALI
  • 167
  • 1
  • 3
-1

I had an exchange with the gmail team and they did confirm that this is actually a bug with their api. Not sure when it will be fixed as they didn't provide any more details but it's on their radar.

yellowandy
  • 89
  • 1
  • 1
  • 7