title: Base64: java.lang.IllegalArgumentException: Illegal character
body: <p>I'm trying to send a confirmation email after user registration. I'm using the JavaMail library for this purpose and the Java 8 Base64 util class.</p>
  
  <p>I'm encoding user emails in the following way:</p>
  
  <pre><code>byte[] encodedEmail = Base64.getUrlEncoder().encode(user.getEmail().getBytes(StandardCharsets.UTF_8));
  Multipart multipart = new MimeMultipart();
  InternetHeaders headers = new InternetHeaders();
  headers.addHeader("Content-type", "text/html; charset=UTF-8");
  String confirmLink = "Complete your registration by clicking on following"+ "\n&lt;a href='" + confirmationURL + encodedEmail + "'&gt;link&lt;/a&gt;";
  MimeBodyPart link = new MimeBodyPart(headers,
  confirmLink.getBytes("UTF-8"));
  multipart.addBodyPart(link);
  </code></pre>
  
  <p>where <code>confirmationURL</code> is:</p>
  
  <pre><code>private final static String confirmationURL = "http://localhost:8080/project/controller?command=confirmRegistration&amp;ID=";
  </code></pre>
  
  <p>And then decoding this in ConfirmRegistrationCommand in such way:</p>
  
  <pre><code>    String encryptedEmail = request.getParameter("ID");
  
      String decodedEmail = new String(Base64.getUrlDecoder().decode(encryptedEmail), StandardCharsets.UTF_8);
  
      RepositoryFactory repositoryFactory = RepositoryFactory
              .getFactoryByName(FactoryType.MYSQL_REPOSITORY_FACTORY);
      UserRepository userRepository = repositoryFactory.getUserRepository();
      User user = userRepository.find(decodedEmail);
  
      if (user.getEmail().equals(decodedEmail)) {
          user.setActiveStatus(true);
          return Path.WELCOME_PAGE;
      } else {
          return Path.ERROR_PAGE;
      }
  </code></pre>
  
  <p>And when I'm trying to decode:</p>
  
  <pre><code>http://localhost:8080/project/controller?command=confirmRegistration&amp;ID=[B@6499375d
  </code></pre>
  
  <p>I'm getting <code>java.lang.IllegalArgumentException: Illegal base64 character 5b</code>.</p>
  
  <p>I tried to use basic Encode/Decoder (not URL ones) with no success.</p>
  
  <p><strong>SOLVED:</strong></p>
  
  <p>The problem was the next - in the line:</p>
  
  <pre><code> String confirmLink = "Complete your registration by clicking on following"+ "\n&lt;a href='" + confirmationURL + encodedEmail + "'&gt;link&lt;/a&gt;";
  </code></pre>
  
  <p>I'm calling toString on an array of bytes, so I should do the following:</p>
  
  <pre><code>String encodedEmail = new String(Base64.getEncoder().encode(
                  user.getEmail().getBytes(StandardCharsets.UTF_8)));
  </code></pre>
  
  <p>Thanks to <strong>Jon Skeet</strong> and <strong>ByteHamster</strong>.</p>