@Override public boolean login() throws IOException { final String name = getLocalAddress().getHostName(); if (name == null) { return false; } boolean ok = SMTPReply.isPositiveCompletion(sendCommand("EHLO", name)); authTypes = ""; for (String line : getReplyStrings()) { if (line != null && (line.startsWith("250 AUTH ") || line.startsWith("250-AUTH "))) { authTypes = line; break; } } return ok; }
private boolean authCram(String smtpUser, String smtpPass, String alg) throws UnsupportedEncodingException, IOException { final String macName = "Hmac" + alg; if (sendCommand("AUTH", "CRAM-" + alg) != 334) { return false; } final String enc = getReplyStrings()[0].split(" ", 2)[1]; final byte[] nonce = Base64.decodeBase64(enc.getBytes(UTF_8)); final String sec; try { Mac mac = Mac.getInstance(macName); mac.init(new SecretKeySpec(smtpPass.getBytes(UTF_8), macName)); sec = toHex(mac.doFinal(nonce)); } catch (NoSuchAlgorithmException e) { throw new IOException("Cannot use CRAM-" + alg, e); } catch (InvalidKeyException e) { throw new IOException("Cannot use CRAM-" + alg, e); } String token = smtpUser + ' ' + sec; String cmd = encodeBase64(token.getBytes(UTF_8)); return SMTPReply.isPositiveCompletion(sendCommand(cmd)); }
private boolean authPlain(String smtpUser, String smtpPass) throws UnsupportedEncodingException, IOException { String token = '\0' + smtpUser + '\0' + smtpPass; String cmd = "PLAIN " + encodeBase64(token.getBytes(UTF_8)); return SMTPReply.isPositiveCompletion(sendCommand("AUTH", cmd)); }