Renamed smtp client and fix non flushing issue
This commit is contained in:
parent
7664b4bf00
commit
16f3d6fc60
4 changed files with 40 additions and 6 deletions
|
|
@ -7,7 +7,7 @@ import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static zutil.net.smtp.SMTPClient.NEWLINE;
|
import static zutil.net.smtp.SmtpClient.NEWLINE;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ import java.util.logging.Logger;
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SMTPClient {
|
public class SmtpClient {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
protected static final String NEWLINE = "\r\n";
|
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
|
* Will look for a SMTP server on localhost on port 25
|
||||||
*/
|
*/
|
||||||
public SMTPClient() throws IOException {
|
public SmtpClient() throws IOException {
|
||||||
this("localhost", 25);
|
this("localhost", 25);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Will look for a SMTP server on specified host on port 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);
|
this(host, 25);
|
||||||
}
|
}
|
||||||
public SMTPClient(String host, int port) throws IOException {
|
public SmtpClient(String host, int port) throws IOException {
|
||||||
socket = new Socket(host, port);
|
socket = new Socket(host, port);
|
||||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
out = new OutputStreamWriter(socket.getOutputStream());
|
out = new OutputStreamWriter(socket.getOutputStream());
|
||||||
|
|
@ -80,6 +80,8 @@ public class SMTPClient {
|
||||||
sendCommand(CMD_HELO + " " + InetAddress.getLocalHost().getHostName());
|
sendCommand(CMD_HELO + " " + InetAddress.getLocalHost().getHostName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a basic email to the smtp server
|
* Sends a basic email to the smtp server
|
||||||
*
|
*
|
||||||
|
|
@ -133,6 +135,7 @@ public class SMTPClient {
|
||||||
public synchronized int sendCommand(String cmd) throws IOException{
|
public synchronized int sendCommand(String cmd) throws IOException{
|
||||||
logger.finest(">> "+cmd);
|
logger.finest(">> "+cmd);
|
||||||
out.write(cmd + NEWLINE);
|
out.write(cmd + NEWLINE);
|
||||||
|
out.flush();
|
||||||
String reply = readCommand();
|
String reply = readCommand();
|
||||||
return parseReturnCode(reply);
|
return parseReturnCode(reply);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
test/zutil/net/smtp/EmailTest.java
Normal file → Executable file
2
test/zutil/net/smtp/EmailTest.java
Normal file → Executable file
|
|
@ -9,7 +9,7 @@ import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
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.
|
* Created by Ziver on 2017-01-19.
|
||||||
|
|
|
||||||
31
test/zutil/net/smtp/SmtpClientTest.java
Executable file
31
test/zutil/net/smtp/SmtpClientTest.java
Executable 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue