private List<ImapResponse> login() throws IOException, MessagingException { /* * Use quoted strings which permit spaces and quotes. (Using IMAP * string literals would be better, but some servers are broken * and don't parse them correctly.) */ // escape double-quotes and backslash characters with a backslash Pattern p = Pattern.compile("[\\\\\"]"); String replacement = "\\\\$0"; String username = p.matcher(settings.getUsername()).replaceAll(replacement); String password = p.matcher(settings.getPassword()).replaceAll(replacement); try { String command = String.format(Commands.LOGIN + " \"%s\" \"%s\"", username, password); return executeSimpleCommand(command, true); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }
private List<ImapResponse> saslAuthPlain() throws IOException, MessagingException { String command = Commands.AUTHENTICATE_PLAIN; String tag = sendCommand(command, false); readContinuationResponse(tag); String credentials = "\000" + settings.getUsername() + "\000" + settings.getPassword(); byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes()); outputStream.write(encodedCredentials); outputStream.write('\r'); outputStream.write('\n'); outputStream.flush(); try { return responseParser.readStatusResponse(tag, command, getLogId(), null); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }
private List<ImapResponse> authCramMD5() throws MessagingException, IOException { String command = Commands.AUTHENTICATE_CRAM_MD5; String tag = sendCommand(command, false); ImapResponse response = readContinuationResponse(tag); if (response.size() != 1 || !(response.get(0) instanceof String)) { throw new MessagingException("Invalid Cram-MD5 nonce received"); } byte[] b64Nonce = response.getString(0).getBytes(); byte[] b64CRAM = Authentication.computeCramMd5Bytes(settings.getUsername(), settings.getPassword(), b64Nonce); outputStream.write(b64CRAM); outputStream.write('\r'); outputStream.write('\n'); outputStream.flush(); try { return responseParser.readStatusResponse(tag, command, getLogId(), null); } catch (NegativeImapResponseException e) { throw handleAuthenticationFailure(e); } }