diff --git a/src/zutil/net/smtp/Email.java b/src/zutil/net/smtp/Email.java index c240be1..12b3722 100755 --- a/src/zutil/net/smtp/Email.java +++ b/src/zutil/net/smtp/Email.java @@ -27,6 +27,8 @@ package zutil.net.smtp; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.Locale; import java.util.regex.Pattern; @@ -44,8 +46,7 @@ public class Email { public enum ContentType{ PLAIN, HTML } - private static final SimpleDateFormat dateFormatter = - new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); + private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH); private static final Pattern PATTERN_NEWLINE = Pattern.compile("(\\r\\n|\\n)"); private String fromAddress; @@ -53,14 +54,14 @@ public class Email { private String toAddress; private String toName = null; private String replyToAddress = null; - private Date date = null; + private String dateStr = dateFormatter.format(new Date(System.currentTimeMillis())); private ContentType type = ContentType.PLAIN; private String subject; private String message; - public Email() { } + public Email() {} public void setFrom(String address) { @@ -99,8 +100,12 @@ public class Email { } 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) { type = t; } @@ -138,6 +143,7 @@ public class Email { throw new IllegalArgumentException("To value cannot be null!"); //************ Headers + // From if (fromName !=null) out.write("From: " + fromName + " <" + fromAddress + ">" + NEWLINE); @@ -155,10 +161,7 @@ public class Email { out.write("To: " + toAddress + NEWLINE); // Date - if (date != null) - out.write("Date: " +dateFormatter.format(date) + NEWLINE); - else - out.write("Date: " +dateFormatter.format(new Date(System.currentTimeMillis())) + NEWLINE); + out.write("Date: " + dateStr + NEWLINE); // Content type switch(type) { @@ -169,10 +172,12 @@ public class Email { } // Subject - out.write("Subject: " +(subject!=null ? subject : "") + NEWLINE); + out.write("Subject: " + (subject!=null ? subject : "") + NEWLINE); out.write(NEWLINE); + //*********** Mesasge + out.write(message); } } \ No newline at end of file diff --git a/test/zutil/net/smtp/EmailTest.java b/test/zutil/net/smtp/EmailTest.java index b012833..55b5eaf 100755 --- a/test/zutil/net/smtp/EmailTest.java +++ b/test/zutil/net/smtp/EmailTest.java @@ -29,8 +29,13 @@ import zutil.io.StringOutputStream; import java.io.IOException; 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.GregorianCalendar; +import java.util.TimeZone; import static org.junit.Assert.assertEquals; import static zutil.net.smtp.SmtpClient.NEWLINE; @@ -110,7 +115,7 @@ public class EmailTest { assertEquals( "From: test@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 + "Subject: " + NEWLINE + NEWLINE + @@ -133,7 +138,7 @@ public class EmailTest { "From: Test Tester " + NEWLINE + "Reply-To: " + NEWLINE + "To: To Totter " + 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 + "Subject: Title" + NEWLINE + NEWLINE + @@ -141,8 +146,9 @@ public class EmailTest { getEmailString(email)); } - - + // --------------------------------------------- + // Utility functions + // --------------------------------------------- private String getEmailString(Email email) throws IOException { StringOutputStream buff = new StringOutputStream(); @@ -152,8 +158,8 @@ public class EmailTest { return buff.toString(); } - private Date getDate(){ - GregorianCalendar date = new GregorianCalendar(2000,10,22, 15,20,55); - return date.getTime(); + private OffsetDateTime getDate(){ + OffsetDateTime dateTime = OffsetDateTime.of(2000,10,22, 15,20,55, 0, ZoneOffset.UTC); + return dateTime; } } \ No newline at end of file