if (!SMTPReply.isPositiveIntermediate(sendCommand(SMTPCommand.AUTH, AUTH_METHOD.getAuthName(method)))) { return false; return SMTPReply.isPositiveCompletion(sendCommand( Base64.encodeBase64StringUnChunked(("\000" + username + "\000" + password).getBytes(getCharset())) )); byte[] serverChallenge = Base64.decodeBase64(getReplyString().substring(4).trim()); hmac_md5.init(new SecretKeySpec(password.getBytes(getCharset()), "HmacMD5")); byte[] hmacResult = _convertToHexString(hmac_md5.doFinal(serverChallenge)).getBytes(getCharset()); byte[] usernameBytes = username.getBytes(getCharset()); byte[] toEncode = new byte[usernameBytes.length + 1 /* the space */ + hmacResult.length]; System.arraycopy(usernameBytes, 0, toEncode, 0, usernameBytes.length); return SMTPReply.isPositiveCompletion(sendCommand( Base64.encodeBase64StringUnChunked(toEncode))); if (!SMTPReply.isPositiveIntermediate(sendCommand( Base64.encodeBase64StringUnChunked(username.getBytes(getCharset()))))) { return false; return SMTPReply.isPositiveCompletion(sendCommand( Base64.encodeBase64StringUnChunked(password.getBytes(getCharset())))); return SMTPReply.isPositiveIntermediate(sendCommand(
/*** * Login to the ESMTP server by sending the EHLO command with the * client hostname as an argument. Before performing any mail commands, * you must first login. * <p> * @return True if successfully completed, false if not. * @throws SMTPConnectionClosedException * If the SMTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send SMTP reply code 421. This exception may be caught either * as an IOException or independently as itself. * @throws IOException If an I/O error occurs while either sending a * command to the server or receiving a reply from the server. ***/ public boolean elogin() throws IOException { String name; InetAddress host; host = getLocalAddress(); name = host.getHostName(); if (name == null) { return false; } return SMTPReply.isPositiveCompletion(ehlo(name)); }
client = new AuthenticatingSMTPClient(); client.setDefaultTimeout(10 * 1000); client.connect(hostname, port); client.ehlo("localhost"); if (client.execTLS()) { client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, login, password); checkReply(client); client.setSender(from); checkReply(client); for (String to : toAddress) { if(to != null && to.length() > 0) { client.addRecipient(to); if (address.length() == 0) { address += ","; Writer writer = client.sendMessageData(); writer.write(body); writer.close(); if (!client.completePendingCommand()) {// failure throw new IOException("Failure to send the email " + client.getReply() + client.getReplyString());
public static SMTPMessageSender authentication(String ip, int port, String senderDomain, String username, String password) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException, InvalidKeyException { AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient(); smtpClient.connect(ip, port); if (smtpClient.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password) == false) { throw new RuntimeException("auth failed"); } return new SMTPMessageSender(smtpClient, senderDomain); }
private void doAddRcpt(String rcpt) throws IOException { boolean success = smtpClient.addRecipient(rcpt); if (!success) { throw new SMTPSendingException(SmtpSendingStep.RCPT, smtpClient.getReplyString()); } } }
private void doSetSender(String from) throws IOException { boolean success = smtpClient.setSender(from); if (!success) { throw new SMTPSendingException(SmtpSendingStep.Sender, smtpClient.getReplyString()); } }
public SMTPMessageSender authenticate(String username, String password) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { if (smtpClient.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password) == false) { throw new SMTPSendingException(SmtpSendingStep.Authentication, smtpClient.getReplyString()); } return this; }
/*** * Returns the integer values of the enhanced reply code of the last SMTP reply. * @return The integer values of the enhanced reply code of the last SMTP reply. * First digit is in the first array element. ***/ public int[] getEnhancedReplyCode() { String reply = getReplyString().substring(4); String[] parts = reply.substring(0, reply.indexOf(' ')).split ("\\."); int[] res = new int[parts.length]; for (int i = 0; i < parts.length; i++) { res[i] = Integer.parseInt (parts[i]); } return res; }
public static SMTPMessageSender noAuthentication(String ip, int port, String senderDomain) throws IOException { AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient(); smtpClient.connect(ip, port); return new SMTPMessageSender(smtpClient, senderDomain); }
private void doRCPT(String recipient) throws IOException { int code = smtpClient.rcpt(recipient); if (code != 250) { throw new SMTPSendingException(SmtpSendingStep.RCPT, smtpClient.getReplyString()); } }
private void doHelo() throws IOException { int code = smtpClient.helo(senderDomain); if (code != 250) { throw new SMTPSendingException(SmtpSendingStep.Helo, smtpClient.getReplyString()); } }
private void doData(String message) throws IOException { boolean success = smtpClient.sendShortMessageData(message); if (!success) { throw new SMTPSendingException(SmtpSendingStep.Data, smtpClient.getReplyString()); } }
/*** * Login to the ESMTP server by sending the EHLO command with the * given hostname as an argument. Before performing any mail commands, * you must first login. * <p> * @param hostname The hostname with which to greet the SMTP server. * @return True if successfully completed, false if not. * @throws SMTPConnectionClosedException * If the SMTP server prematurely closes the connection as a result * of the client being idle or some other reason causing the server * to send SMTP reply code 421. This exception may be caught either * as an IOException or independently as itself. * @throws IOException If an I/O error occurs while either sending a * command to the server or receiving a reply from the server. ***/ public boolean elogin(String hostname) throws IOException { return SMTPReply.isPositiveCompletion(ehlo(hostname)); }
@Override public void close() throws IOException { smtpClient.disconnect(); }
public SMTPMessageSender connect(String ip, int port) throws IOException { smtpClient.connect(ip, port); return this; }
public SMTPMessageSender(String senderDomain) { this(new AuthenticatingSMTPClient(), senderDomain); }