• Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • mailer-plugin
    • None

      MimeMessageBuilder uses a simple StringTokenizer for parsing the email list. It needs to be a bit more clever about it because, as Stephen Connolly pointed out, some of the chars in the delimiter-set are valid address characters in some situations - quoted local part.

      http://tools.ietf.org/html/rfc5321

          [JENKINS-26706] Email list parsing not spec compliant

          Alex Earl added a comment -

          Please provide some examples of failing cases.

          Alex Earl added a comment - Please provide some examples of failing cases.

          István Gulyás added a comment - - edited

          Hello All,
          I wrote here a simple code, and I suggest to use a well defined javax.mail parsing first

          https://github.com/jenkinsci/mailer-plugin/blob/master/src/main/java/jenkins/plugins/mailer/tasks/MimeMessageBuilder.java#L173

              public MimeMessageBuilder addRecipients(@NonNull String recipients, @NonNull Message.RecipientType recipientType) throws UnsupportedEncodingException {
          
                  AtrrayList<InternetAddress> parsed = new ArrayList<>();
                  try {
                      InternetAddress[] internetAddresses = InternetAddress.parse(recipients); // standard javax.mail works well
                      parsed .addAll(internetAddresses);
                  } catch(AddressException e) {
          
                      // throw new exception or log when read behavior from config or system property?
                      // the following code run the old code default because backward compatibility, but regular expression splitter in Jenkins admin web page may be better...
          
                      StringTokenizer tokens = new StringTokenizer(recipients, " \t\n\r\f,");
                      while (tokens.hasMoreTokens()) {
                          String addressToken = tokens.nextToken();
                          InternetAddress internetAddress = toNormalizedAddress(addressToken);
                          if (internetAddress != null) {
                              parsed .add(internetAddress);
                          }
                      }
                  }
          
                  for (InternetAddress internetAddress : parsed) {
                          if (recipientType == Message.RecipientType.TO) {
                              to.add(internetAddress);
                          } else if (recipientType == Message.RecipientType.CC) {
                              cc.add(internetAddress);
                          } else if (recipientType == Message.RecipientType.BCC) {
                              bcc.add(internetAddress);
                          }
                  }
          
                  return this;
              }
          

          Edit:
          RFC822 syntax strict mode may be configurable also:
          https://javaee.github.io/javamail/docs/api/javax/mail/internet/InternetAddress.html#parse-java.lang.String-boolean-

          István Gulyás added a comment - - edited Hello All, I wrote here a simple code, and I suggest to use a well defined javax.mail parsing first https://github.com/jenkinsci/mailer-plugin/blob/master/src/main/java/jenkins/plugins/mailer/tasks/MimeMessageBuilder.java#L173 public MimeMessageBuilder addRecipients(@NonNull String recipients, @NonNull Message.RecipientType recipientType) throws UnsupportedEncodingException { AtrrayList<InternetAddress> parsed = new ArrayList<>(); try { InternetAddress[] internetAddresses = InternetAddress.parse(recipients); // standard javax.mail works well parsed .addAll(internetAddresses); } catch (AddressException e) { // throw new exception or log when read behavior from config or system property? // the following code run the old code default because backward compatibility, but regular expression splitter in Jenkins admin web page may be better... StringTokenizer tokens = new StringTokenizer(recipients, " \t\n\r\f," ); while (tokens.hasMoreTokens()) { String addressToken = tokens.nextToken(); InternetAddress internetAddress = toNormalizedAddress(addressToken); if (internetAddress != null ) { parsed .add(internetAddress); } } } for (InternetAddress internetAddress : parsed) { if (recipientType == Message.RecipientType.TO) { to.add(internetAddress); } else if (recipientType == Message.RecipientType.CC) { cc.add(internetAddress); } else if (recipientType == Message.RecipientType.BCC) { bcc.add(internetAddress); } } return this ; } Edit: RFC822 syntax strict mode may be configurable also: https://javaee.github.io/javamail/docs/api/javax/mail/internet/InternetAddress.html#parse-java.lang.String-boolean-

            Unassigned Unassigned
            tfennelly Tom FENNELLY
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: