List of usage examples for org.apache.commons.net.smtp SMTPClient getReplyCode
public int getReplyCode()
From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java
private SampleResult sampleReset(SampleResult sr) { SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient()); SMTPClient client = null; if (soclient instanceof SMTPClient) client = (SMTPClient) soclient;/*from w w w . j ava 2s .c o m*/ String request = "COMMAND\n"; request += "Client : " + getClient() + "\n"; sr.setRequestHeaders(request); if (client == null) { sr.setResponseCode("404"); sr.setResponseData(("Client `" + getClient() + "` not found").getBytes()); sr.setSuccessful(false); return sr; } else { synchronized (client) { sr.sampleStart(); try { sr.setSuccessful(client.reset()); sr.setResponseCode(String.valueOf(client.getReplyCode())); sr.setResponseData(client.getReplyString().getBytes()); setSuccessfulByResponseCode(sr, client.getReplyCode()); } catch (IOException e) { sr.setSuccessful(false); sr.setResponseData(e.toString().getBytes()); sr.setResponseCode(e.getClass().getName()); log.error("client `" + client + "` ", e); removeClient(); } sr.sampleEnd(); } } return sr; }
From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java
private SampleResult sampleCommand(SampleResult sr) { SocketClient soclient = SessionStorage.getInstance().getClient(getSOClient()); SMTPClient client = null; int responseCode = 0; if (soclient instanceof SMTPClient) client = (SMTPClient) soclient;/*from ww w . j av a 2 s .c o m*/ String request = "COMMAND\n"; request += "Client : " + getClient() + "\n"; request += "Command : " + getCommand() + "\n"; sr.setRequestHeaders(request); if (client == null) { sr.setResponseCode("404"); sr.setResponseData(("Client `" + getClient() + "` not found").getBytes()); sr.setSuccessful(false); return sr; } else { synchronized (client) { sr.sampleStart(); try { responseCode = client.sendCommand(getCommand()); sr.setResponseCode(String.valueOf(responseCode)); sr.setSuccessful(SMTPReply.isPositiveIntermediate(responseCode)); String response = client.getReplyString(); setSuccessfulByResponseCode(sr, client.getReplyCode()); if (SessionStorage.getInstance() .getClientType(getSOClient()) == SessionStorage.proto_type.STARTTLS) { String command; if (getCommand().indexOf(' ') != -1) command = getCommand().substring(0, getCommand().indexOf(' ')); else command = getCommand(); if ((command.equalsIgnoreCase("lhlo") || command.equalsIgnoreCase("ehlo") || command.equalsIgnoreCase("helo")) && getUseSTARTTLS() && client instanceof SMTPSClient) { SMTPSClient sclient = (SMTPSClient) client; if (sclient.execTLS() == false) { sr.setSuccessful(false); sr.setResponseCode("403"); ; response += sclient.getReplyString(); log.error("client `" + client + "` STARTTLS failed"); removeClient(); } else { response += "\nSTARTTLS OK"; } } } sr.setResponseData(response.getBytes()); } catch (IOException e) { sr.setSuccessful(false); sr.setResponseData(e.toString().getBytes()); sr.setResponseCode(e.getClass().getName()); log.error("client `" + client + "` ", e); removeClient(); } sr.sampleEnd(); } } return sr; }
From source file:ProtocolRunner.java
private static void handleSMTP( SMTPClient client, ProtocolRunner runner, /*from www .jav a 2 s . c o m*/ String commandString) throws IOException { if(commandString == null) { // means just connected // check if the server response was valid if(!SMTPReply.isPositiveCompletion(client.getReplyCode())) { runner.handleDisconnect(); return; } } else { // need to handle a command client.sendCommand(commandString); } runner.getTCPServerResponse().append(client.getReplyString()); }
From source file:me.schiz.jmeter.protocol.smtp.sampler.SMTPSampler.java
private SampleResult sampleConnect(SampleResult sr) { SMTPClient client; if (getUseSSL()) { client = new SMTPSClient(true); } else if (getUseSTARTTLS()) { client = new SMTPSClient(false); } else {/*from w w w . j a va2 s . com*/ client = new SMTPClient(); } try { String request = "CONNECT \n"; request += "Host : " + getHostname() + ":" + getPort() + "\n"; request += "Default Timeout : " + getDefaultTimeout() + "\n"; request += "Connect Timeout : " + getConnectionTimeout() + "\n"; request += "So Timeout : " + getSoTimeout() + "\n"; request += "Client : " + getClient() + "\n"; if (getUseSSL()) request += "SSL : true\n"; else request += "SSL : false\n"; if (getUseSTARTTLS()) request += "STARTTLS : true\n"; else request += "STARTTLS : false\n"; sr.setRequestHeaders(request); sr.sampleStart(); client.setDefaultTimeout(getDefaultTimeout()); client.setConnectTimeout(getConnectionTimeout()); client.connect(getHostname(), getPort()); if (client.isConnected()) { SessionStorage.proto_type protoType = SessionStorage.proto_type.PLAIN; if (getUseSSL() && !getUseSTARTTLS()) protoType = SessionStorage.proto_type.SSL; if (!getUseSSL() && getUseSTARTTLS()) protoType = SessionStorage.proto_type.STARTTLS; SessionStorage.getInstance().putClient(getSOClient(), client, protoType); client.setSoTimeout(getSoTimeout()); client.setTcpNoDelay(getTcpNoDelay()); sr.setResponseCode(String.valueOf(client.getReplyCode())); sr.setResponseData(client.getReplyString().getBytes()); setSuccessfulByResponseCode(sr, client.getReplyCode()); } } catch (SocketException se) { sr.setResponseMessage(se.toString()); sr.setSuccessful(false); sr.setResponseCode(se.getClass().getName()); log.error("client `" + client + "` ", se); } catch (IOException ioe) { sr.setResponseMessage(ioe.toString()); sr.setSuccessful(false); sr.setResponseCode(ioe.getClass().getName()); log.error("client `" + client + "` ", ioe); } sr.sampleEnd(); return sr; }
From source file:com.xpn.xwiki.XWiki.java
/** * @deprecated replaced by the <a href="http://code.xwiki.org/xwiki/bin/view/Plugins/MailSenderPlugin">Mail Sender * Plugin</a>//from ww w . ja v a 2 s. c om */ @Deprecated private void sendMessageOld(String sender, String[] recipient, String message, XWikiContext context) throws XWikiException { SMTPClient smtpc = null; try { String server = getXWikiPreference("smtp_server", context); String port = getXWikiPreference("smtp_port", context); String login = getXWikiPreference("smtp_login", context); if (context.get("debugMail") != null) { StringBuffer msg = new StringBuffer(message); msg.append("\n Recipient: "); msg.append(recipient); recipient = ((String) context.get("debugMail")).split(","); message = msg.toString(); } if ((server == null) || server.equals("")) { server = "127.0.0.1"; } if ((port == null) || (port.equals(""))) { port = "25"; } if ((login == null) || login.equals("")) { login = InetAddress.getLocalHost().getHostName(); } smtpc = new SMTPClient(); smtpc.connect(server, Integer.parseInt(port)); int reply = smtpc.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { Object[] args = { server, port, Integer.valueOf(reply), smtpc.getReplyString() }; throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_CONNECT_FAILED, "Could not connect to server {0} port {1} error code {2} ({3})", null, args); } if (smtpc.login(login) == false) { reply = smtpc.getReplyCode(); Object[] args = { server, port, Integer.valueOf(reply), smtpc.getReplyString() }; throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_LOGIN_FAILED, "Could not login to mail server {0} port {1} error code {2} ({3})", null, args); } if (smtpc.sendSimpleMessage(sender, recipient, message) == false) { reply = smtpc.getReplyCode(); Object[] args = { server, port, Integer.valueOf(reply), smtpc.getReplyString() }; throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_SEND_FAILED, "Could not send mail to server {0} port {1} error code {2} ({3})", null, args); } } catch (IOException e) { Object[] args = { sender, recipient }; throw new XWikiException(XWikiException.MODULE_XWIKI_EMAIL, XWikiException.ERROR_XWIKI_EMAIL_ERROR_SENDING_EMAIL, "Exception while sending email from {0} to {1}", e, args); } finally { if ((smtpc != null) && (smtpc.isConnected())) { try { smtpc.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:org.apache.axis.transport.mail.MailSender.java
/** * Send the soap request message to the server * * @param msgContext message context// www . j a v a 2s .c o m * * @return id for the current message * @throws Exception */ private String writeUsingSMTP(MessageContext msgContext) throws Exception { String id = (new java.rmi.server.UID()).toString(); String smtpHost = msgContext.getStrProp(MailConstants.SMTP_HOST); SMTPClient client = new SMTPClient(); client.connect(smtpHost); // After connection attempt, you should check the reply code to verify // success. System.out.print(client.getReplyString()); int reply = client.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { client.disconnect(); AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); throw fault; } client.login(smtpHost); System.out.print(client.getReplyString()); reply = client.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { client.disconnect(); AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); throw fault; } String fromAddress = msgContext.getStrProp(MailConstants.FROM_ADDRESS); String toAddress = msgContext.getStrProp(MailConstants.TO_ADDRESS); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromAddress)); msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toAddress)); // Get SOAPAction, default to "" String action = msgContext.useSOAPAction() ? msgContext.getSOAPActionURI() : ""; if (action == null) { action = ""; } Message reqMessage = msgContext.getRequestMessage(); msg.addHeader(HTTPConstants.HEADER_USER_AGENT, Messages.getMessage("axisUserAgent")); msg.addHeader(HTTPConstants.HEADER_SOAP_ACTION, action); msg.setDisposition(MimePart.INLINE); msg.setSubject(id); ByteArrayOutputStream out = new ByteArrayOutputStream(8 * 1024); reqMessage.writeTo(out); msg.setContent(out.toString(), reqMessage.getContentType(msgContext.getSOAPConstants())); ByteArrayOutputStream out2 = new ByteArrayOutputStream(8 * 1024); msg.writeTo(out2); client.setSender(fromAddress); System.out.print(client.getReplyString()); client.addRecipient(toAddress); System.out.print(client.getReplyString()); Writer writer = client.sendMessageData(); System.out.print(client.getReplyString()); writer.write(out2.toString()); writer.flush(); writer.close(); System.out.print(client.getReplyString()); if (!client.completePendingCommand()) { System.out.print(client.getReplyString()); AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null); throw fault; } System.out.print(client.getReplyString()); client.logout(); client.disconnect(); return id; }
From source file:org.apache.axis.transport.mail.MailWorker.java
/** * Send the soap request message to the server * //from w w w . j a v a2s. c o m * @param msgContext * @param smtpHost * @param sendFrom * @param replyTo * @param output * @throws Exception */ private void writeUsingSMTP(MessageContext msgContext, String smtpHost, String sendFrom, String replyTo, String subject, Message output) throws Exception { SMTPClient client = new SMTPClient(); client.connect(smtpHost); // After connection attempt, you should check the reply code to verify // success. System.out.print(client.getReplyString()); int reply = client.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { client.disconnect(); AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); throw fault; } client.login(smtpHost); System.out.print(client.getReplyString()); reply = client.getReplyCode(); if (!SMTPReply.isPositiveCompletion(reply)) { client.disconnect(); AxisFault fault = new AxisFault("SMTP", "( SMTP server refused connection )", null, null); throw fault; } MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(sendFrom)); msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(replyTo)); msg.setDisposition(MimePart.INLINE); msg.setSubject(subject); ByteArrayOutputStream out = new ByteArrayOutputStream(8 * 1024); output.writeTo(out); msg.setContent(out.toString(), output.getContentType(msgContext.getSOAPConstants())); ByteArrayOutputStream out2 = new ByteArrayOutputStream(8 * 1024); msg.writeTo(out2); client.setSender(sendFrom); System.out.print(client.getReplyString()); client.addRecipient(replyTo); System.out.print(client.getReplyString()); Writer writer = client.sendMessageData(); System.out.print(client.getReplyString()); writer.write(out2.toString()); writer.flush(); writer.close(); System.out.print(client.getReplyString()); if (!client.completePendingCommand()) { System.out.print(client.getReplyString()); AxisFault fault = new AxisFault("SMTP", "( Failed to send email )", null, null); throw fault; } System.out.print(client.getReplyString()); client.logout(); client.disconnect(); }
From source file:org.apache.common.net.examples.mail.SMTPMail.java
public final static void main(String[] args) { String sender, recipient, subject, filename, server, cc; List<String> ccList = new ArrayList<String>(); BufferedReader stdin;/*from ww w . j ava2s . c o m*/ FileReader fileReader = null; Writer writer; SimpleSMTPHeader header; SMTPClient client; if (args.length < 1) { System.err.println("Usage: mail smtpserver"); System.exit(1); } server = args[0]; stdin = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("From: "); System.out.flush(); sender = stdin.readLine(); System.out.print("To: "); System.out.flush(); recipient = stdin.readLine(); System.out.print("Subject: "); System.out.flush(); subject = stdin.readLine(); header = new SimpleSMTPHeader(sender, recipient, subject); while (true) { System.out.print("CC <enter one address per line, hit enter to end>: "); System.out.flush(); cc = stdin.readLine(); if (cc == null || cc.length() == 0) { break; } header.addCC(cc.trim()); ccList.add(cc.trim()); } System.out.print("Filename: "); System.out.flush(); filename = stdin.readLine(); try { fileReader = new FileReader(filename); } catch (FileNotFoundException e) { System.err.println("File not found. " + e.getMessage()); } client = new SMTPClient(); client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); client.connect(server); if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) { client.disconnect(); System.err.println("SMTP server refused connection."); System.exit(1); } client.login(); client.setSender(sender); client.addRecipient(recipient); for (String recpt : ccList) { client.addRecipient(recpt); } writer = client.sendMessageData(); if (writer != null) { writer.write(header.toString()); Util.copyReader(fileReader, writer); writer.close(); client.completePendingCommand(); } if (fileReader != null) { fileReader.close(); } client.logout(); client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } }
From source file:org.apache.commons.net.examples.mail.SMTPMail.java
public static void main(String[] args) { String sender, recipient, subject, fileName, server, cc; List<String> ccList = new ArrayList<String>(); BufferedReader stdin;//w w w. j a va 2 s . co m FileReader fileReader = null; Writer writer; SimpleSMTPHeader header; SMTPClient client; if (args.length < 1) { System.err.println("Usage: SMTPMail <smtpserver>"); System.exit(1); } server = args[0]; stdin = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("From: "); System.out.flush(); sender = stdin.readLine(); System.out.print("To: "); System.out.flush(); recipient = stdin.readLine(); System.out.print("Subject: "); System.out.flush(); subject = stdin.readLine(); header = new SimpleSMTPHeader(sender, recipient, subject); while (true) { System.out.print("CC <enter one address per line, hit enter to end>: "); System.out.flush(); cc = stdin.readLine(); if (cc == null || cc.length() == 0) { break; } header.addCC(cc.trim()); ccList.add(cc.trim()); } System.out.print("Filename: "); System.out.flush(); fileName = stdin.readLine(); try { fileReader = new FileReader(fileName); } catch (FileNotFoundException e) { System.err.println("File not found. " + e.getMessage()); } client = new SMTPClient(); client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true)); client.connect(server); if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) { client.disconnect(); System.err.println("SMTP server refused connection."); System.exit(1); } client.login(); client.setSender(sender); client.addRecipient(recipient); for (String recpt : ccList) { client.addRecipient(recpt); } writer = client.sendMessageData(); if (writer != null) { writer.write(header.toString()); Util.copyReader(fileReader, writer); writer.close(); client.completePendingCommand(); } if (fileReader != null) { fileReader.close(); } client.logout(); client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } }
From source file:org.apache.james.protocols.lmtp.AbstractLMTPServerTest.java
@Override public void testMailWithoutBrackets() throws Exception { TestMessageHook hook = new TestMessageHook(); InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort()); ProtocolServer server = null;// w ww . j a v a2 s .c o m try { server = createServer(createProtocol(hook), address); server.bind(); SMTPClient client = createClient(); client.connect(address.getAddress().getHostAddress(), address.getPort()); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.helo("localhost"); assertTrue(SMTPReply.isPositiveCompletion(client.getReplyCode())); client.mail(SENDER); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.quit(); assertTrue("Reply=" + client.getReplyString(), SMTPReply.isPositiveCompletion(client.getReplyCode())); client.disconnect(); Iterator<MailEnvelope> queued = hook.getQueued().iterator(); assertFalse(queued.hasNext()); } finally { if (server != null) { server.unbind(); } } }