예제 #1
0
  /**
   * Create a mailto URI.
   *
   * @param to the recipient, can be <code>null</code>.
   * @param subject the subject, can be <code>null</code>.
   * @param body the body of the email, can be <code>null</code>.
   * @param attachments files to attach, for security reason this parameter is ignored by at least
   *     Outlook 2007, Apple Mail and Thunderbird.
   * @return the mailto URI.
   * @throws IOException if an encoding error happens.
   * @see <a href="http://tools.ietf.org/html/rfc2368">RFC 2368</a>
   * @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=67254">Don&apos;t allow attachment
   *     of local file from non-local link</a>
   */
  public static final URI getMailToURI(
      final String to, final String subject, final String body, final File... attachments)
      throws IOException {
    // mailto:[email protected]?subject=Sujet%20du%20courrier&[email protected]&[email protected]&body=Bonjour

    // Outlook doesn't support the to header as mandated by 2. of the RFC
    final String encodedTo = to == null ? "" : PercentEncoder.encode(to, StringUtils.UTF8);
    final List<String> l = new ArrayList<String>(4);
    if (subject != null) l.add(createEncodedParam("subject", subject));
    if (body != null) l.add(createEncodedParam("body", body));
    for (final File attachment : attachments)
      l.add(createEncodedParam("attachment", attachment.getAbsolutePath()));
    final String query = CollectionUtils.join(l, "&");
    try {
      return new URI("mailto:" + encodedTo + "?" + query);
    } catch (URISyntaxException e) {
      throw new IOException("Couldn't create mailto URI", e);
    }
  }
예제 #2
0
 private static final String createEncodedParam(final String name, final String value) {
   return name + "=" + PercentEncoder.encode(value, StringUtils.UTF8);
 }