IDNEmailAddressConverter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.mail2.jakarta.util;

  18. import java.net.IDN;
  19. import java.util.function.Function;

  20. import jakarta.mail.internet.InternetAddress;

  21. /**
  22.  * Converts email addresses containing International Domain Names into an ASCII representation suitable for sending an email.
  23.  *
  24.  * @see <a href="https://docs.oracle.com/javase/tutorial/i18n/network/idn.html">https://docs.oracle.com/javase/tutorial/i18n/network/idn.html</a>
  25.  * @see <a href="https://en.wikipedia.org/wiki/Punycode">https://en.wikipedia.org/wiki/Punycode</a>
  26.  * @see <a href="https://tools.ietf.org/html/rfc5891">https://tools.ietf.org/html/rfc5891</a>
  27.  * @see <a href="https://en.wikipedia.org/wiki/Punycode">https://en.wikipedia.org/wiki/Punycode</a>
  28.  *
  29.  * @since 1.5
  30.  */
  31. public class IDNEmailAddressConverter {

  32.     /**
  33.      * Constructs a new instance.
  34.      */
  35.     public IDNEmailAddressConverter() {
  36.         // empty
  37.     }

  38.     /**
  39.      * Extracts the domain part of the email address.
  40.      *
  41.      * @param email email address.
  42.      * @param idx   index of '@' character.
  43.      * @return domain part of email
  44.      */
  45.     private String getDomainPart(final String email, final int idx) {
  46.         return email.substring(idx + 1);
  47.     }

  48.     /**
  49.      * Extracts the local part of the email address.
  50.      *
  51.      * @param email email address.
  52.      * @param idx   index of '@' character.
  53.      * @return local part of email
  54.      */
  55.     private String getLocalPart(final String email, final int idx) {
  56.         return email.substring(0, idx);
  57.     }

  58.     /**
  59.      * Converts an email address to its ASCII representation using "Punycode".
  60.      *
  61.      * @param email email address.
  62.      * @return The ASCII representation
  63.      */
  64.     public String toASCII(final String email) {
  65.         return toString(email, IDN::toASCII);
  66.     }

  67.     private String toString(final String email, final Function<String, String> converter) {
  68.         final int idx = email == null ? -1 : email.indexOf('@');
  69.         if (idx < 0) {
  70.             return email;
  71.         }
  72.         return getLocalPart(email, idx) + '@' + converter.apply(getDomainPart(email, idx));
  73.     }

  74.     /**
  75.      * Converts the address part of an InternetAddress to its Unicode representation.
  76.      *
  77.      * @param address email address.
  78.      * @return The Unicode representation
  79.      */
  80.     String toUnicode(final InternetAddress address) {
  81.         return address != null ? toUnicode(address.getAddress()) : null;
  82.     }

  83.     /**
  84.      * Converts an "Punycode" email address to its Unicode representation.
  85.      *
  86.      * @param email email address.
  87.      * @return The Unicode representation
  88.      */
  89.     String toUnicode(final String email) {
  90.         return toString(email, IDN::toUnicode);
  91.     }
  92. }