Skip to content

Commit dfacb7b

Browse files
committed
add java mail util
1 parent 2654b2a commit dfacb7b

File tree

19 files changed

+3113
-0
lines changed

19 files changed

+3113
-0
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</attributes>
1414
</classpathentry>
1515
<classpathentry kind="src" path="src/test/resources"/>
16+
<classpathentry kind="src" path="src/main/resources"/>
1617
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
1718
<attributes>
1819
<attribute name="maven.pomderived" value="true"/>

log/debug.log

Lines changed: 1068 additions & 0 deletions
Large diffs are not rendered by default.

log/error.log

Whitespace-only changes.

log/info.log

Whitespace-only changes.

pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<argLine>-Dfile.encoding=UTF-8</argLine>
1616
<poi.version>3.16</poi.version>
17+
<!-- Logging -->
18+
<logback.version>1.0.13</logback.version>
19+
<slf4j.version>1.7.5</slf4j.version>
1720
</properties>
1821

1922
<dependencies>
@@ -92,5 +95,30 @@
9295
<version>1.3.1</version>
9396
</dependency>
9497

98+
<!-- Logging with SLF4J & LogBack -->
99+
<dependency>
100+
<groupId>org.slf4j</groupId>
101+
<artifactId>slf4j-api</artifactId>
102+
<version>${slf4j.version}</version>
103+
<scope>compile</scope>
104+
</dependency>
105+
<dependency>
106+
<groupId>ch.qos.logback</groupId>
107+
<artifactId>logback-classic</artifactId>
108+
<version>${logback.version}</version>
109+
<scope>runtime</scope>
110+
</dependency>
111+
112+
<dependency>
113+
<groupId>org.freemarker</groupId>
114+
<artifactId>freemarker</artifactId>
115+
<version>2.3.20</version>
116+
</dependency>
117+
<dependency>
118+
<groupId>javax.mail</groupId>
119+
<artifactId>mail</artifactId>
120+
<version>1.4.7</version>
121+
</dependency>
122+
95123
</dependencies>
96124
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.coderdream.timer;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Calendar;
5+
import java.util.Timer;
6+
7+
public class MyTimer {
8+
9+
public static void main(String[] args) {
10+
//
11+
Timer timer = new Timer();
12+
13+
MyTimerTask myTimerTask = new MyTimerTask("No.1");
14+
15+
// timer.schedule(myTimerTask, 2000L, 1000L);
16+
/**
17+
* <pre>
18+
* 获取当前时间,并设置成距离当前时间三秒之后的时间
19+
* 如当前时间是2016-11-10 23:59:57
20+
* 则设置后的时间则为2016-11-11 00:00:00
21+
* </pre>
22+
*/
23+
Calendar calendar = Calendar.getInstance();
24+
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
25+
System.out.println(sf.format(calendar.getTime()));
26+
calendar.add(Calendar.SECOND, 3);
27+
28+
// --- scheduale的用法
29+
/**
30+
* 1.时间等于或超过time的时候执行且仅执行一次task 如在2016-11-11 00:00:00执行一次task:打印任务的名字
31+
*/
32+
// myTimerTask.setName("schedual1");
33+
// timer.schedule(myTimerTask, calendar.getTime());
34+
35+
/**
36+
* 2.
37+
*/
38+
// myTimerTask.setName("schedual_2");
39+
// timer.schedule(myTimerTask, calendar.getTime(), 2000);
40+
/**
41+
* 3.等待delay毫秒后执行且执行一次task
42+
*/
43+
// myTimerTask.setName("schedule_3");
44+
// timer.schedule(myTimerTask, 3000);
45+
/**
46+
* 4.等待delay毫秒后每隔period执行一次task
47+
*/
48+
// myTimerTask.setName("schedule_4");
49+
// timer.schedule(myTimerTask, 3000, 2000);
50+
/**
51+
* 5
52+
*/
53+
// myTimerTask.setName("scheduleAtFixedRate_1");
54+
// timer.scheduleAtFixedRate(myTimerTask, calendar.getTime(), 2000);
55+
/**
56+
*
57+
*/
58+
// myTimerTask.setName("scheduleAtFixedRate_2");
59+
// timer.scheduleAtFixedRate(myTimerTask, 3000, 2000);
60+
61+
myTimerTask.setName("scheduleAtFixedRate_3");
62+
timer.schedule(myTimerTask, 3000);
63+
System.out.println("scheduled time is " + sf.format(myTimerTask.scheduledExecutionTime()));
64+
}
65+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.coderdream.timer;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Calendar;
5+
import java.util.TimerTask;
6+
7+
public class MyTimerTask extends TimerTask {
8+
9+
private String name;
10+
11+
private Integer count = 0;
12+
13+
public MyTimerTask(String inputName) {
14+
name = inputName;
15+
}
16+
17+
@Override
18+
public void run() {
19+
if (count < 3) {
20+
//
21+
22+
Calendar calendar = Calendar.getInstance();
23+
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
24+
System.out.println("Current exec time is: \t" + sf.format(calendar.getTime()));
25+
26+
// 打印当前name的内容
27+
System.out.println("Current exec name is: \t" + name);
28+
count++;
29+
} else {
30+
cancel();
31+
System.out.println("Task Cancel");
32+
}
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.coderdream.util.mail;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Properties;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/**
10+
* 邮件发送配置信息加载类
11+
*
12+
* @date 2014年4月26日 下午2:52:06
13+
* @author 曹志飞
14+
* @Description:
15+
* @project mailUtil
16+
*/
17+
public class ConfigLoader {
18+
//日志记录对象
19+
private static Logger log = LoggerFactory.getLogger(ConfigLoader.class);
20+
// 配置文件路径
21+
private static String mailPath = "mail/mail.properties";
22+
// 邮件发送SMTP主机
23+
private static String server;
24+
// 发件人邮箱地址
25+
private static String sender;
26+
// 发件人邮箱用户名
27+
private static String username;
28+
// 发件人邮箱密码
29+
private static String password;
30+
// 发件人显示昵称
31+
private static String nickname;
32+
static {
33+
// 类初始化后加载配置文件
34+
InputStream in = ConfigLoader.class.getClassLoader()
35+
.getResourceAsStream(mailPath);
36+
Properties props = new Properties();
37+
try {
38+
props.load(in);
39+
} catch (IOException e) {
40+
log.error("load mail setting error,pleace check the file path:"
41+
+ mailPath);
42+
log.error(e.toString(), e);
43+
}
44+
server = props.getProperty("mail.server");
45+
sender = props.getProperty("mail.sender");
46+
username = props.getProperty("mail.username");
47+
password = props.getProperty("mail.password");
48+
nickname = props.getProperty("mail.nickname");
49+
log.debug("load mail setting success,file path:" + mailPath);
50+
}
51+
52+
public static String getServer() {
53+
return server;
54+
}
55+
56+
public static String getSender() {
57+
return sender;
58+
}
59+
60+
public static String getUsername() {
61+
return username;
62+
}
63+
64+
public static String getPassword() {
65+
return password;
66+
}
67+
68+
public static String getNickname() {
69+
return nickname;
70+
}
71+
72+
public static void setMailPath(String mailPath) {
73+
ConfigLoader.mailPath = mailPath;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)