From 16f3d6fc60846d3b4884e32b6b7dba3630040ba7 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 20 Jan 2017 13:25:49 +0100 Subject: [PATCH] Renamed smtp client and fix non flushing issue --- src/zutil/net/smtp/Email.java | 2 +- src/zutil/net/smtp/SMTPClient.java | 11 +++++---- test/zutil/net/smtp/EmailTest.java | 2 +- test/zutil/net/smtp/SmtpClientTest.java | 31 +++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) mode change 100644 => 100755 test/zutil/net/smtp/EmailTest.java create mode 100755 test/zutil/net/smtp/SmtpClientTest.java diff --git a/src/zutil/net/smtp/Email.java b/src/zutil/net/smtp/Email.java index 1528949..0038220 100755 --- a/src/zutil/net/smtp/Email.java +++ b/src/zutil/net/smtp/Email.java @@ -7,7 +7,7 @@ import java.util.Date; import java.util.Locale; import java.util.regex.Pattern; -import static zutil.net.smtp.SMTPClient.NEWLINE; +import static zutil.net.smtp.SmtpClient.NEWLINE; /** diff --git a/src/zutil/net/smtp/SMTPClient.java b/src/zutil/net/smtp/SMTPClient.java index b8e579a..9c0f636 100755 --- a/src/zutil/net/smtp/SMTPClient.java +++ b/src/zutil/net/smtp/SMTPClient.java @@ -40,7 +40,7 @@ import java.util.logging.Logger; * @author Ziver * */ -public class SMTPClient { +public class SmtpClient { private static final Logger logger = LogUtil.getLogger(); protected static final String NEWLINE = "\r\n"; @@ -62,16 +62,16 @@ public class SMTPClient { /** * Will look for a SMTP server on localhost on port 25 */ - public SMTPClient() throws IOException { + public SmtpClient() throws IOException { this("localhost", 25); } /** * Will look for a SMTP server on specified host on port 25 */ - public SMTPClient(String host) throws IOException { + public SmtpClient(String host) throws IOException { this(host, 25); } - public SMTPClient(String host, int port) throws IOException { + public SmtpClient(String host, int port) throws IOException { socket = new Socket(host, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new OutputStreamWriter(socket.getOutputStream()); @@ -80,6 +80,8 @@ public class SMTPClient { sendCommand(CMD_HELO + " " + InetAddress.getLocalHost().getHostName()); } + + /** * Sends a basic email to the smtp server * @@ -133,6 +135,7 @@ public class SMTPClient { public synchronized int sendCommand(String cmd) throws IOException{ logger.finest(">> "+cmd); out.write(cmd + NEWLINE); + out.flush(); String reply = readCommand(); return parseReturnCode(reply); } diff --git a/test/zutil/net/smtp/EmailTest.java b/test/zutil/net/smtp/EmailTest.java old mode 100644 new mode 100755 index 9400fdd..6a03d1f --- a/test/zutil/net/smtp/EmailTest.java +++ b/test/zutil/net/smtp/EmailTest.java @@ -9,7 +9,7 @@ import java.util.Date; import java.util.GregorianCalendar; import static org.junit.Assert.*; -import static zutil.net.smtp.SMTPClient.NEWLINE; +import static zutil.net.smtp.SmtpClient.NEWLINE; /** * Created by Ziver on 2017-01-19. diff --git a/test/zutil/net/smtp/SmtpClientTest.java b/test/zutil/net/smtp/SmtpClientTest.java new file mode 100755 index 0000000..4e6e43c --- /dev/null +++ b/test/zutil/net/smtp/SmtpClientTest.java @@ -0,0 +1,31 @@ +package zutil.net.smtp; + +import zutil.log.CompactLogFormatter; +import zutil.log.LogUtil; + +import java.io.IOException; +import java.util.logging.Level; + +/** + * Sends a test email with SmtpClient class. + * + * @See Fake SMTP Server + */ +public class SmtpClientTest { + + public static void main(String[] args) throws IOException { + LogUtil.setGlobalFormatter(new CompactLogFormatter()); + LogUtil.setGlobalLevel(Level.ALL); + + SmtpClient smtp = new SmtpClient(); + smtp.send("from@example.com", + "to@example.com", + "Test email", + "Disregard this email"); + smtp.send("from2@example.com", + "to2@example.com", + "Test 2 email", + "Disregard this email"); + smtp.close(); + } +}