Moved Email to new java.time api
This commit is contained in:
parent
e85f00c652
commit
372c02f5fe
2 changed files with 28 additions and 17 deletions
|
|
@ -27,6 +27,8 @@ package zutil.net.smtp;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
@ -44,8 +46,7 @@ public class Email {
|
||||||
public enum ContentType{
|
public enum ContentType{
|
||||||
PLAIN, HTML
|
PLAIN, HTML
|
||||||
}
|
}
|
||||||
private static final SimpleDateFormat dateFormatter =
|
private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
|
||||||
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
|
|
||||||
private static final Pattern PATTERN_NEWLINE = Pattern.compile("(\\r\\n|\\n)");
|
private static final Pattern PATTERN_NEWLINE = Pattern.compile("(\\r\\n|\\n)");
|
||||||
|
|
||||||
private String fromAddress;
|
private String fromAddress;
|
||||||
|
|
@ -53,14 +54,14 @@ public class Email {
|
||||||
private String toAddress;
|
private String toAddress;
|
||||||
private String toName = null;
|
private String toName = null;
|
||||||
private String replyToAddress = null;
|
private String replyToAddress = null;
|
||||||
private Date date = null;
|
private String dateStr = dateFormatter.format(new Date(System.currentTimeMillis()));
|
||||||
private ContentType type = ContentType.PLAIN;
|
private ContentType type = ContentType.PLAIN;
|
||||||
private String subject;
|
private String subject;
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Email() { }
|
public Email() {}
|
||||||
|
|
||||||
|
|
||||||
public void setFrom(String address) {
|
public void setFrom(String address) {
|
||||||
|
|
@ -99,8 +100,12 @@ public class Email {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDate(Date date) {
|
public void setDate(Date date) {
|
||||||
this.date = date;
|
this.dateStr = dateFormatter.format(date);
|
||||||
}
|
}
|
||||||
|
public void setDate(OffsetDateTime date) {
|
||||||
|
this.dateStr = date.format(DateTimeFormatter.RFC_1123_DATE_TIME);;
|
||||||
|
}
|
||||||
|
|
||||||
public void setContentType(ContentType t) {
|
public void setContentType(ContentType t) {
|
||||||
type = t;
|
type = t;
|
||||||
}
|
}
|
||||||
|
|
@ -138,6 +143,7 @@ public class Email {
|
||||||
throw new IllegalArgumentException("To value cannot be null!");
|
throw new IllegalArgumentException("To value cannot be null!");
|
||||||
|
|
||||||
//************ Headers
|
//************ Headers
|
||||||
|
|
||||||
// From
|
// From
|
||||||
if (fromName !=null)
|
if (fromName !=null)
|
||||||
out.write("From: " + fromName + " <" + fromAddress + ">" + NEWLINE);
|
out.write("From: " + fromName + " <" + fromAddress + ">" + NEWLINE);
|
||||||
|
|
@ -155,10 +161,7 @@ public class Email {
|
||||||
out.write("To: " + toAddress + NEWLINE);
|
out.write("To: " + toAddress + NEWLINE);
|
||||||
|
|
||||||
// Date
|
// Date
|
||||||
if (date != null)
|
out.write("Date: " + dateStr + NEWLINE);
|
||||||
out.write("Date: " +dateFormatter.format(date) + NEWLINE);
|
|
||||||
else
|
|
||||||
out.write("Date: " +dateFormatter.format(new Date(System.currentTimeMillis())) + NEWLINE);
|
|
||||||
|
|
||||||
// Content type
|
// Content type
|
||||||
switch(type) {
|
switch(type) {
|
||||||
|
|
@ -169,10 +172,12 @@ public class Email {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subject
|
// Subject
|
||||||
out.write("Subject: " +(subject!=null ? subject : "") + NEWLINE);
|
out.write("Subject: " + (subject!=null ? subject : "") + NEWLINE);
|
||||||
|
|
||||||
out.write(NEWLINE);
|
out.write(NEWLINE);
|
||||||
|
|
||||||
//*********** Mesasge
|
//*********** Mesasge
|
||||||
|
|
||||||
out.write(message);
|
out.write(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,8 +29,13 @@ import zutil.io.StringOutputStream;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static zutil.net.smtp.SmtpClient.NEWLINE;
|
import static zutil.net.smtp.SmtpClient.NEWLINE;
|
||||||
|
|
@ -110,7 +115,7 @@ public class EmailTest {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"From: test@example.com" + NEWLINE +
|
"From: test@example.com" + NEWLINE +
|
||||||
"To: to@example.com" + NEWLINE +
|
"To: to@example.com" + NEWLINE +
|
||||||
"Date: Wed, 22 Nov 2000 15:20:55 +0100" + NEWLINE +
|
"Date: Sun, 22 Oct 2000 15:20:55 GMT" + NEWLINE +
|
||||||
"Content-Type: text/plain;" + NEWLINE +
|
"Content-Type: text/plain;" + NEWLINE +
|
||||||
"Subject: " + NEWLINE +
|
"Subject: " + NEWLINE +
|
||||||
NEWLINE +
|
NEWLINE +
|
||||||
|
|
@ -133,7 +138,7 @@ public class EmailTest {
|
||||||
"From: Test Tester <test@example.com>" + NEWLINE +
|
"From: Test Tester <test@example.com>" + NEWLINE +
|
||||||
"Reply-To: <mokey@example.org>" + NEWLINE +
|
"Reply-To: <mokey@example.org>" + NEWLINE +
|
||||||
"To: To Totter <to@example.com>" + NEWLINE +
|
"To: To Totter <to@example.com>" + NEWLINE +
|
||||||
"Date: Wed, 22 Nov 2000 15:20:55 +0100" + NEWLINE +
|
"Date: Sun, 22 Oct 2000 15:20:55 GMT" + NEWLINE +
|
||||||
"Content-Type: text/html;" + NEWLINE +
|
"Content-Type: text/html;" + NEWLINE +
|
||||||
"Subject: Title" + NEWLINE +
|
"Subject: Title" + NEWLINE +
|
||||||
NEWLINE +
|
NEWLINE +
|
||||||
|
|
@ -141,8 +146,9 @@ public class EmailTest {
|
||||||
getEmailString(email));
|
getEmailString(email));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------
|
||||||
|
// Utility functions
|
||||||
|
// ---------------------------------------------
|
||||||
|
|
||||||
private String getEmailString(Email email) throws IOException {
|
private String getEmailString(Email email) throws IOException {
|
||||||
StringOutputStream buff = new StringOutputStream();
|
StringOutputStream buff = new StringOutputStream();
|
||||||
|
|
@ -152,8 +158,8 @@ public class EmailTest {
|
||||||
return buff.toString();
|
return buff.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Date getDate(){
|
private OffsetDateTime getDate(){
|
||||||
GregorianCalendar date = new GregorianCalendar(2000,10,22, 15,20,55);
|
OffsetDateTime dateTime = OffsetDateTime.of(2000,10,22, 15,20,55, 0, ZoneOffset.UTC);
|
||||||
return date.getTime();
|
return dateTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue