+ * 新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作。 + */ + public static void main(String[] args) { + + /* 本地化日期时间 API */ + localDateTime(); + + /* 使用时区的日期时间API */ + zonedDateTime(); + } + + public static void localDateTime() { + // LocalDateTime + LocalDateTime localDateTime = LocalDateTime.now(); + System.out.println("LocalDataTime.now: " + localDateTime); + System.out.println("Month: " + localDateTime.getMonth()); + System.out.println("Day: " + localDateTime.getDayOfMonth()); + System.out.println("Minute: " + localDateTime.getMinute()); + System.out.println("Seconds: " + localDateTime.getSecond()); + LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(15).withYear(2018); + System.out.println("LocalDateTime1: " + localDateTime1); + + // LocalDate + LocalDate localDate = localDateTime.toLocalDate(); + System.out.println("LocalDate: " + localDate); + LocalDate localDate1 = LocalDate.of(2019, 8, 15); + System.out.println("LocalDate1: " + localDate1); + + // LocalTime + LocalTime localTime = LocalTime.now(); + System.out.println("LocalTime.now: " + localTime); + LocalTime localTime1 = LocalTime.of(12, 15); + System.out.println("LocalTime1: " + localTime1); + LocalTime localTime2 = LocalTime.parse("13:15:30"); + System.out.println("Parse local time hour: " + localTime2.getHour()); + } + + public static void zonedDateTime() { + ZonedDateTime zonedDateTime = ZonedDateTime.now(); + System.out.println("ZonedDateTime: " + zonedDateTime); + + ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2018-08-15T13:24:46.506+08:00[Asia/Shanghai]"); + System.out.println("Parse zoned dat time zone: " + zonedDateTime1.getZone()); + + ZoneId zoneId = ZoneId.of("Europe/Paris"); + System.out.println("ZonedId: " + zoneId); + + ZoneId currentZone = ZoneId.systemDefault(); + System.out.println("ZonedId.systemDefault: " + currentZone); + } +} diff --git a/src/tutorial/mails/JavaMail.java b/src/tutorial/mails/JavaMail.java new file mode 100644 index 0000000..3d10dd5 --- /dev/null +++ b/src/tutorial/mails/JavaMail.java @@ -0,0 +1,147 @@ +package mails; + +import javax.activation.DataHandler; +import javax.activation.FileDataSource; +import javax.mail.*; +import javax.mail.internet.*; +import java.io.IOException; +import java.io.InputStream; +import java.security.Security; +import java.util.Properties; + +/** + * Created by Oliver on 03/07/2017. + */ +public class JavaMail { + private String to = ""; + private String from = ""; + private String password = ""; + private String host = ""; + private Properties properties = System.getProperties(); + private boolean debug = false; + + public JavaMail(boolean debug) { + InputStream in = JavaMail.class.getResourceAsStream("smtp.properties"); + try { + properties.load(in); + to = properties.getProperty("mail.sender.send.to"); + from = properties.getProperty("mail.sender.username"); + password = properties.getProperty("mail.sender.password"); + host = properties.getProperty("mail.smtp.host"); + } catch (IOException e) { + e.printStackTrace(); + } + this.debug = debug; + } + + + public void init() { + //设置SSL连接、邮件环境 + Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); + final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; + properties.setProperty("mail.smtp.host", host); // 设置邮件服务器 + properties.setProperty("mail.smtp.auth", "true"); + properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); + properties.setProperty("mail.smtp.socketFactory.fallback", "false"); + properties.setProperty("mail.smtp.port", "465"); + properties.setProperty("mail.smtp.socketFactory.port", "465"); + } + + public void plain_mail(String subject, String text) { + init(); + // 获取默认session对象 + Session session = Session.getDefaultInstance(properties, new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(from, password); + } + }); + + session.setDebug(debug); + + try { + MimeMessage mimeMessage = new MimeMessage(session); + mimeMessage.setFrom(new InternetAddress(from)); + mimeMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); + + mimeMessage.setSubject(subject); + mimeMessage.setText(text); // 设置消息体 + Transport.send(mimeMessage); + System.out.println("Sent plain message successfully...."); + } catch (AddressException a) { + a.printStackTrace(); + } catch (MessagingException m) { + m.printStackTrace(); + } + } + + public void html_mail(String subject, String content) { + init(); + Session session = Session.getDefaultInstance(properties, new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(from, password); + } + }); + + session.setDebug(debug); + + try { + MimeMessage mimeMessage = new MimeMessage(session); + mimeMessage.setFrom(new InternetAddress(from)); + mimeMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); + mimeMessage.setSubject(subject); + mimeMessage.setContent(content, "text/html"); // 发送 HTML 消息, 可以插入html标签 + Transport.send(mimeMessage); + System.out.println("Sent html message successfully...."); + } catch (AddressException a) { + a.printStackTrace(); + } catch (MessagingException m) { + m.printStackTrace(); + } + } + + public void attachment_mail(String subject, String text, String filePath) { + init(); + Session session = Session.getDefaultInstance(properties, new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(from, password); + } + }); + + session.setDebug(debug); + + try { + // 创建默认的 MimeMessage 对象 + MimeMessage mimeMessage = new MimeMessage(session); + mimeMessage.setFrom(new InternetAddress(from)); + mimeMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to)); + mimeMessage.setSubject(subject); + + // 创建消息部分 + BodyPart bodyPart = new MimeBodyPart(); + bodyPart.setText(text); + + // 设置文本消息部分 + FileDataSource fileDataSource = new FileDataSource(filePath); + bodyPart.setDataHandler(new DataHandler(fileDataSource)); + bodyPart.setFileName(filePath); + + // 创建多重消息 + Multipart multipart = new MimeMultipart(); + multipart.addBodyPart(bodyPart); + + // 发送完整消息 + mimeMessage.setContent(multipart); + + // 发送消息 + Transport.send(mimeMessage); + System.out.println("Sent attachment message successfully...."); + + } catch (MessagingException m) { + m.printStackTrace(); + } + } + +} diff --git a/src/tutorial/mails/TestMail.java b/src/tutorial/mails/TestMail.java new file mode 100644 index 0000000..e058be4 --- /dev/null +++ b/src/tutorial/mails/TestMail.java @@ -0,0 +1,17 @@ +package mails; + +import java.io.File; + +/** + * Created by Oliver on 04/07/2017. + */ +public class TestMail { + public static void main(String[] args) { + JavaMail javaMail = new JavaMail(true); + javaMail.plain_mail("Java smtp testing with plain content", "Plain Message using JavaMail"); + javaMail.html_mail("Java smtp testing with html content", "