1

Javaのサーブレットコンテキストリスナーを使用して自動メールを送信しようとしています。Tomcatサーバーの起動時にこれを実行したいと思います。私はweb.xmlに次のように書いています

<listener>
    <listener-class>com.ServletListenerTimmer</listener-class>
</listener>

そして、ServletListenerTimmerに私が配置した次のコード

package com;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public String date,name,month,email;
public class ServletListenerTimmer implements ServletContextListener {


public void contextInitialized(ServletContextEvent event) {

new SendReport(event.getServletContext()), 0, 1 * 1000);

}
public void contextDestroyed(ServletContextEvent event) {

}
}
public class SendReport extends TimerTask {

private ServletContext context = null;
public SendReport(ServletContext context) {
this.context = context;
}
public void run() {
Calendar cal = Calendar.getInstance();
int sysdate=cal.get(Calendar.Date);
int sysmonth=cal.get(Calendar.Month);
sysmonth+=11;
Connection con=null;
Statement st=null;
ResultSet rs=null;
boolean f=false;


try{  

    Class.forName("com.mysql.jdbc.Driver").newInstance();  
    String connectionURL = "jdbc:mysql://localhost:1234/email"; 
    con=DriverManager.getConnection(connectionURL, "root", "root");  
    st=con.createStatement();  
    String sql = "SELECT * FROM emp"; 
    rs = st.executeQuery(sql); 
while(rs.next())
{
    name=rs.getString(2);
    date=rs.getString(3);
    month=rs.getString(4);
    email=rs.getString(6);
    if(date==sysdate && month==sysmonth)
    {
doReportTask();// you can write your sendmail task there
    }
}
}
} else {
context.log("the prevenient task is still running!");
}
}

 public void doReportTask()
{
String result;
final String username = "kavithagowda15@gmail.com";
 final String password = "Kavitha9480317174";
// Recipient's email ID needs to be mentioned.

String cc = "kruthi@corasent.com";
// Sender's email ID needs to be mentioned
String from = "kavithagowda15@gmail.com";

// Assuming you are sending email from localhost
String host = "localhost";

// Get system properties object
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");



// Get the default Session object.
Session mailSession = Session.getInstance(properties,new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
  });

 try{
   Message message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(email));

    // Add to Cc: header
    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
    message.setSubject("Testing Subject");
    message.setText("Dear " +name +","
        + "\n\n No spam to my email, please!");

    Transport.send(message);

    System.out.println("Done");
}catch (MessagingException mex) {
  mex.printStackTrace();
  result = "Error: unable to send message....";
}
}
}

それでも機能しません。問題が何であるかわかりません。これで私を助けてください

4

0 に答える 0