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 19 import java.net.IDN; 20 import java.util.function.Function; 21 22 import jakarta.mail.internet.InternetAddress; 23 24 /** 25 * Converts email addresses containing International Domain Names into an ASCII representation suitable for sending an email. 26 * 27 * @see <a href="https://docs.oracle.com/javase/tutorial/i18n/network/idn.html">https://docs.oracle.com/javase/tutorial/i18n/network/idn.html</a> 28 * @see <a href="https://en.wikipedia.org/wiki/Punycode">https://en.wikipedia.org/wiki/Punycode</a> 29 * @see <a href="https://tools.ietf.org/html/rfc5891">https://tools.ietf.org/html/rfc5891</a> 30 * @see <a href="https://en.wikipedia.org/wiki/Punycode">https://en.wikipedia.org/wiki/Punycode</a> 31 * 32 * @since 1.5 33 */ 34 public class IDNEmailAddressConverter { 35 36 /** 37 * Constructs a new instance. 38 */ 39 public IDNEmailAddressConverter() { 40 // empty 41 } 42 43 /** 44 * Extracts the domain part of the email address. 45 * 46 * @param email email address. 47 * @param idx index of '@' character. 48 * @return domain part of email 49 */ 50 private String getDomainPart(final String email, final int idx) { 51 return email.substring(idx + 1); 52 } 53 54 /** 55 * Extracts the local part of the email address. 56 * 57 * @param email email address. 58 * @param idx index of '@' character. 59 * @return local part of email 60 */ 61 private String getLocalPart(final String email, final int idx) { 62 return email.substring(0, idx); 63 } 64 65 /** 66 * Converts an email address to its ASCII representation using "Punycode". 67 * 68 * @param email email address. 69 * @return The ASCII representation 70 */ 71 public String toASCII(final String email) { 72 return toString(email, IDN::toASCII); 73 } 74 75 private String toString(final String email, final Function<String, String> converter) { 76 final int idx = email == null ? -1 : email.indexOf('@'); 77 if (idx < 0) { 78 return email; 79 } 80 return getLocalPart(email, idx) + '@' + converter.apply(getDomainPart(email, idx)); 81 } 82 83 /** 84 * Converts the address part of an InternetAddress to its Unicode representation. 85 * 86 * @param address email address. 87 * @return The Unicode representation 88 */ 89 String toUnicode(final InternetAddress address) { 90 return address != null ? toUnicode(address.getAddress()) : null; 91 } 92 93 /** 94 * Converts an "Punycode" email address to its Unicode representation. 95 * 96 * @param email email address. 97 * @return The Unicode representation 98 */ 99 String toUnicode(final String email) { 100 return toString(email, IDN::toUnicode); 101 } 102 }