Advertisement
Guest User

Untitled

a guest
Jul 20th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Properties;
  9.  
  10. import javax.mail.Message;
  11. import javax.mail.MessagingException;
  12. import javax.mail.PasswordAuthentication;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeMessage;
  17.  
  18. public class TestSendEmails {
  19. private String emailTo;
  20. private String emailSubject;
  21. private String emailBody;
  22. private String emailAttachments;
  23.  
  24. public TestSendEmails(){
  25.  
  26. }
  27.  
  28. public TestSendEmails(String emailTo, String emailSubject, String emailBody, String emailAttachments){
  29. super();
  30. this.emailTo = emailTo;
  31. this.emailSubject = emailSubject;
  32. this.emailBody = emailBody;
  33. this.emailAttachments = emailAttachments;
  34. }
  35.  
  36. public String getEmailTo(){
  37. return emailTo;
  38. }
  39.  
  40. public void setEmailTo(String emailTo){
  41. this.emailTo = emailTo;
  42. }
  43.  
  44. public String getEmailSubject(){
  45. return emailSubject;
  46. }
  47.  
  48. public void setEmailSubject(String emailSubject){
  49. this.emailSubject = emailSubject;
  50. }
  51.  
  52. public String getEmailBody(){
  53. return emailBody;
  54. }
  55.  
  56. public void setEmailBody(String emailBody){
  57. this.emailBody = emailBody;
  58. }
  59.  
  60. public String getEmailAttachments(){
  61. return emailAttachments;
  62. }
  63.  
  64. public void setEmailAttachments(String emailAttachments){
  65. this.emailAttachments = emailAttachments;
  66. }
  67. }
  68.  
  69. class TestSendEmailD{
  70. private Connection con;
  71.  
  72. private static final String GET_EMAILS = "Select* From Emails";
  73.  
  74. private void connect() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
  75. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  76. con = DriverManager.getConnection("jdbc:sqlserver://100.000.000.00\:3333;databaseName=Test;user=mmmm;password=1234");
  77. }
  78.  
  79. public List<TestSendEmails> getTestSendEmails() throws Exception{
  80. connect();
  81. PreparedStatement ps = con.prepareStatement(GET_EMAILS);
  82. ResultSet rs = ps.executeQuery();
  83. List<TestSendEmails> result = new ArrayList<TestSendEmails>();
  84. while(rs.next()){
  85. result.add(new TestSendEmails(rs.getString("emailTo"), rs.getString("emailSubject"),rs.getString("emailBody"),rs.getString("emailAttachments")));
  86. }
  87. disconnect();
  88. return result;
  89. }
  90.  
  91. private void disconnect() throws SQLException{
  92. if(con != null){
  93. con.close();
  94. }
  95. }
  96. }
  97.  
  98. class EmailSender{
  99. private Session session;
  100.  
  101. private void init(){
  102. Properties props = new Properties();
  103. props.put("mail.smtp.auth", "true");
  104. props.put("mail.smtp.starttls.enable", "true");
  105. props.put("mail.smtp.host", "100.000.000.00");
  106. props.put("mail.smtp.port", "678");
  107.  
  108. session = Session.getInstance(props,
  109. new javax.mail.Authenticator() {
  110. protected PasswordAuthentication getPasswordAuthentication() {
  111. return new PasswordAuthentication("test@gmail.com", "123");
  112. }
  113. });
  114. }
  115.  
  116. public void sendEmail(TestSendEmails s) throws MessagingException{
  117. init();
  118. Message message = new MimeMessage(session);
  119. message.setFrom(new InternetAddress("test@gmail.com"));
  120. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(s.getEmailTo().replace(";", ",")));
  121. message.setSubject(s.getEmailSubject());
  122. message.setText(s.getEmailBody());
  123. message.setContent(s.getEmailBody(),"text/html");
  124. Transport.send(message);
  125. System.out.println("Done");
  126. }
  127.  
  128. public void sendEmail(List<TestSendEmails> emails) throws MessagingException{
  129. for(TestSendEmails TestSendEmails:emails ){
  130. sendEmail(TestSendEmails);
  131. }
  132. }
  133. }
  134.  
  135. import java.sql.Connection;
  136. import java.sql.DriverManager;
  137. import java.sql.PreparedStatement;
  138. import java.util.Date;
  139.  
  140. public class UpdateEmail {
  141. public static Connection getConnection() throws Exception {
  142. String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  143. String url = "jdbc:sqlserver://100.000.000.00\:3333;databaseName=Test";
  144. String username = "mmmm";
  145. String password = "1234";
  146. Class.forName(driver);
  147. Connection conn = DriverManager.getConnection(url, username, password);
  148. return conn;
  149. }
  150.  
  151. public static void main(String[] args) throws Exception {
  152. java.util.Date date = new Date();
  153. Connection conn = null;
  154. PreparedStatement pstmt = null;
  155. try {
  156. conn = getConnection();
  157. String query = "update Emails set SentOn = ? where Id = ? ";
  158. pstmt = conn.prepareStatement(query); // create a statement
  159. pstmt.setTimestamp(1, new java.sql.Timestamp(date.getTime()));
  160. pstmt.setInt(2, 200); // In this line I want to use my array-list to update my table.
  161. pstmt.executeUpdate(); // execute update statement
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. System.exit(1);
  165. } finally {
  166. pstmt.close();
  167. conn.close();
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement