001.
002.
003.
004.
005.
006.
package
jobpending;
007.
008.
import
java.sql.ResultSet;
009.
import
java.sql.SQLException;
010.
import
java.sql.Statement;
011.
import
java.sql.Connection;
012.
import
java.sql.DriverManager;
013.
014.
015.
016.
import
java.util.ArrayList;
017.
import
java.util.List;
018.
import
java.util.Properties;
019.
import
javax.mail.Message;
020.
import
javax.mail.MessagingException;
021.
import
javax.mail.PasswordAuthentication;
022.
import
javax.mail.Session;
023.
import
javax.mail.Transport;
024.
import
javax.mail.internet.InternetAddress;
025.
import
javax.mail.internet.MimeMessage;
026.
027.
028.
029.
030.
031.
public
class
JobPending {
032.
033.
034.
035.
036.
public
static
void
main(String[] args) {
037.
038.
039.
Connection connect =
null
;
040.
Statement s =
null
;
041.
042.
try
{
043.
Class.forName(
"oracle.jdbc.driver.OracleDriver"
);
044.
connect = DriverManager.getConnection(
""
+
045.
"jdbc:oracle:thin:@//localhost:1521/XE"
,
"user"
,
"pass"
);
046.
047.
s = connect.createStatement();
048.
049.
int
STATUS =
0
;
050.
051.
String sql =
"SELECT * FROM MSG4ACT WHERE STATUS = "
+STATUS+
""
;
052.
053.
ResultSet rec = s.executeQuery(sql);
054.
055.
056.
final
String auth_host =
"mail.test.co.th"
;
057.
final
String auth_port =
"25"
;
058.
final
String auth_email =
"test@test.co.th"
;
059.
final
String auth_password =
"pass"
;
060.
061.
Properties props =
new
Properties();
062.
props.put(
"mail.smtp.host"
, auth_host);
063.
props.put(
"mail.smtp.socketFactory.port"
, auth_port);
064.
props.put(
"mail.smtp.socketFactory.class"
,
065.
"javax.net.ssl.SSLSocketFactory"
);
066.
props.put(
"mail.smtp.auth"
,
"true"
);
067.
props.put(
"mail.smtp.port"
, auth_port);
068.
try
{
069.
070.
Session mailSession = Session.getInstance(props,
071.
new
javax.mail.Authenticator() {
072.
protected
PasswordAuthentication
073.
getPasswordAuthentication() {
074.
return
new
PasswordAuthentication
075.
(auth_email,auth_password);
076.
}
077.
});
078.
079.
Message message =
new
MimeMessage(mailSession);
080.
081.
message.setFrom(
new
InternetAddress(auth_email));
082.
InternetAddress[] toAddresses = {
new
InternetAddress(
"test@test.co.th"
) };
083.
message.setRecipients(Message.RecipientType.TO,toAddresses);
084.
message.setSubject(
"Alert !! Activity-Plan : STATUS = Pending"
);
085.
int
i=
0
;
086.
List rowValues =
new
ArrayList();
087.
while
((rec!=
null
) && (rec.next()))
088.
{
089.
rowValues.add(rec.getString(
"ACTNO"
));
090.
091.
String[] contactListNames = (String[]) rowValues.toArray(
new
String[rowValues.size()]);
092.
System.out.println(contactListNames[i]);
093.
094.
message.setText(contactListNames[i]);
095.
096.
}
097.
Transport.send(message);
098.
System.out.println(
"Mail Send Successfully."
);
099.
100.
}
catch
(MessagingException e) {
101.
throw
new
RuntimeException(e);
102.
}
103.
104.
}
catch
(Exception e) {
105.
106.
e.printStackTrace();
107.
}
108.
109.
try
{
110.
s.close();
111.
connect.close();
112.
}
catch
(SQLException e) {
113.
114.
e.printStackTrace();
115.
}
116.
117.
118.
119.
}
120.
121.
}