1

In our application we use java mail to send emails. Every thing was working fine until a new requirement has come to send an email to an id which takes the form testUser@em_company.com (The part which causes JavaMail to throw an exception is the fact that email address contains _ in the domain name. (em_company.com).

In our code I'm initializing InternetAddress like :

  //harding coding toAddress for simplicity
 String toAddress = "testUser@em_company.com";
 InternetAddress address = new InternetAddress(toAddress);

After looking in the code (Java Mail 1.4.4)

Class : InternetAddress

Method : private static void checkAddress(String addr, boolean routeAddr, boolean validate) throws AddressException

I see a check which causes this exception :

  if (!(Character.isLetterOrDigit(c) || c == '-' || c == '.'))
        throw new AddressException(
                "Domain contains illegal character", addr);

So the question is, is there any way that we can bypass this check and ask java Mail not to do validation check on domain name.

Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
jithin iyyani
  • 751
  • 1
  • 7
  • 19

3 Answers3

3

It is kinda weird requirement...Domain name generally contains only letters, digits, dash or dot characters (see here).

Anyway, InternetAddress class has secondary constructor that takes second boolean parameter strict which enforces RFC822 syntax. But it calls first constructor with the check anyway so it is not working as expected.

InternetAddress address = new InternetAddress("testUser@em_company.com", false);

// from API sources:
public InternetAddress(String address, boolean strict) throws AddressException {
   this(address); // THIS CAUSES EXCEPTION ANYWAY
   if (strict) {
      if (isGroup())
         getGroup(true);    // throw away the result
      else
        checkAddress(this.address, true, true);
   }
}

So the only solution that comes on my mind after code investigation is this one but you have to try it out:

InternetAddress address = new InternetAddress();
address.setAddress("testUser@em_company.com");

Unfortunately with this code you lose all parsing logic of InternetAddress. But if you want to use this class only to conform Java Mail API, this may be the way to go.

zdenda.online
  • 2,451
  • 3
  • 23
  • 45
0

You can also try this:-

String addressString="ars.kov@gmail.com";
String personalString="??????? ?????????";
InternetAddress address=new InternetAddress(addressString,personalString,"UTF-8");
Chetan chadha
  • 558
  • 1
  • 4
  • 19
0

I had the same issue, I was using postman Post call :

I set parameter as key=recipient value='abc@gmail.com'

It started working after removing single quotes from the value. e.g. value=abc@gmail.com

S'chn T'gai Spock
  • 1,203
  • 18
  • 16