Renamed smtp client and fix non flushing issue

This commit is contained in:
Ziver Koc 2017-01-20 13:25:49 +01:00
parent 7664b4bf00
commit 16f3d6fc60
4 changed files with 40 additions and 6 deletions

View file

@ -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;
/**

View file

@ -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);
}

2
test/zutil/net/smtp/EmailTest.java Normal file → Executable file
View file

@ -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.

View file

@ -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 <a href="https://nilhcem.github.io/FakeSMTP/">Fake SMTP Server</a>
*/
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();
}
}