|
|
|
java send mail หลายๆข้อความ โดยดึงข้อความมาจาก array ซึ่ง array ก็ get มาจาก database : HelpMe !!!! |
|
|
|
|
|
|
|
ต้องการ นำข้อความใน array ทุกตัวที่ get มาจาก datebase แล้วส่งในรูปแบบ message.setText(); อะครับ
คือผมทำมันส่งแค่บรรทัดเดียว ต้องการให้ส่ง message ใน array ทุกตัวครับ
Code (Java)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jobpending;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
//mail
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author life
*/
public class JobPending {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Connection connect = null;
Statement s = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connect = DriverManager.getConnection("" +
"jdbc:oracle:thin:@//localhost:1521/XE", "user","pass");
s = connect.createStatement();
int STATUS = 0;
String sql = "SELECT * FROM MSG4ACT WHERE STATUS = "+STATUS+"";
ResultSet rec = s.executeQuery(sql);
final String auth_host = "mail.test.co.th";
final String auth_port = "25";
final String auth_email = "[email protected]";
final String auth_password = "pass";
Properties props = new Properties();
props.put("mail.smtp.host", auth_host);
props.put("mail.smtp.socketFactory.port", auth_port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", auth_port);
try {
Session mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication
(auth_email,auth_password);
}
});
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(auth_email)); // From
InternetAddress[] toAddresses = { new InternetAddress("[email protected]") };
message.setRecipients(Message.RecipientType.TO,toAddresses);// To
message.setSubject("Alert !! Activity-Plan : STATUS = Pending");
int i=0;
List rowValues = new ArrayList();
while((rec!=null) && (rec.next()))
{
rowValues.add(rec.getString("ACTNO"));
//System.out.println(rec.getString("ACTNO"));
String[] contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
System.out.println(contactListNames[i]);
message.setText(contactListNames[i]);
}
Transport.send(message);
System.out.println("Mail Send Successfully.");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Tag : Java, Oracle, JAVA
|
|
|
|
|
|
Date :
2016-03-16 16:55:18 |
By :
jakkrit01 |
View :
1213 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-03-18 21:20:19 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|