GmailSender.java
package test;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GmailSender {
public void gmailSend(){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "用户名@gmail.com";//发件人用户名
final String password = "密码";
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
// msg.setFrom(new InternetAddress("用户名@hotmail.com"));//收件人用户名
try {
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("用户名@hotmail.com",false));//收件人用户名
msg.setSubject("Hello");
msg.setText("How are you");
msg.setSentDate(new Date());
Transport.send(msg);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Message sent.");
}
}
MyListener.java
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener{
private Timer timer = null;
public void contextDestroyed(ServletContextEvent arg0) {
timer.cancel();
}
public void contextInitialized(ServletContextEvent arg0) {
timer = new Timer(true);
Calendar calendar = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
String nowTime = dateFormat.format(date);
int year = Integer.parseInt(nowTime.substring(0, 4));
int month = Integer.parseInt(nowTime.substring(nowTime.indexOf("-") + 1, nowTime.lastIndexOf("-"))) - 1;
int day = Integer.parseInt(nowTime.substring(nowTime.lastIndexOf("-") + 1, nowTime.indexOf(" ")));
//每天中午 12 点定时发邮件
int hour = 12;
int minute = 0;
int second= 0;
calendar.set(year, month, day, hour, minute, second);//第二个参数是实际月份减一,比如一月份是 0
if(date.after(calendar.getTime())){
day = day + 1;
calendar.set(year, month, day, hour, minute, second);
}
date = calendar.getTime();
timer.schedule(new MyTask(), date, 86400000);//每隔 24 小时发一次
}
}
MyTask.java
package test;
import java.util.TimerTask;
public class MyTask extends TimerTask{
@Override
public void run() {
new GmailSender().gmailSend();
}
}
web.xml
<listener>
<listener-class>test.MyListener</listener-class>
</listener>
package test;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GmailSender {
public void gmailSend(){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "用户名@gmail.com";//发件人用户名
final String password = "密码";
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
// msg.setFrom(new InternetAddress("用户名@hotmail.com"));//收件人用户名
try {
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("用户名@hotmail.com",false));//收件人用户名
msg.setSubject("Hello");
msg.setText("How are you");
msg.setSentDate(new Date());
Transport.send(msg);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Message sent.");
}
}
MyListener.java
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener{
private Timer timer = null;
public void contextDestroyed(ServletContextEvent arg0) {
timer.cancel();
}
public void contextInitialized(ServletContextEvent arg0) {
timer = new Timer(true);
Calendar calendar = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
String nowTime = dateFormat.format(date);
int year = Integer.parseInt(nowTime.substring(0, 4));
int month = Integer.parseInt(nowTime.substring(nowTime.indexOf("-") + 1, nowTime.lastIndexOf("-"))) - 1;
int day = Integer.parseInt(nowTime.substring(nowTime.lastIndexOf("-") + 1, nowTime.indexOf(" ")));
//每天中午 12 点定时发邮件
int hour = 12;
int minute = 0;
int second= 0;
calendar.set(year, month, day, hour, minute, second);//第二个参数是实际月份减一,比如一月份是 0
if(date.after(calendar.getTime())){
day = day + 1;
calendar.set(year, month, day, hour, minute, second);
}
date = calendar.getTime();
timer.schedule(new MyTask(), date, 86400000);//每隔 24 小时发一次
}
}
MyTask.java
package test;
import java.util.TimerTask;
public class MyTask extends TimerTask{
@Override
public void run() {
new GmailSender().gmailSend();
}
}
web.xml
<listener>
<listener-class>test.MyListener</listener-class>
</listener>