邮件发送代码书写
1.导包
mail.jar
2.书写代码
package com.itcast.mail;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
//email:邮件发给谁 subject:主题 emailMsg:邮件的内容
public static void sendMail(String email, String subject, String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
props.setProperty("mail.host", "localhost");//发送邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("tom", "123456");//发邮件的账号的验证
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
message.setSubject(subject);//邮件的主题
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}
package com.itcast.mail;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
public class SendMailTest {
public static void main(String[] args) throws AddressException, MessagingException {
MailUtils.sendMail("[email protected]", "测试邮件","这是一封测试邮件");
}
}
实现定时发送生日祝福
package com.itcast.birthday;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.mail.MessagingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.itcast.mail.MailUtils;
public class BirthdayListenner implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// 当web应用器开启任务调动--功能在用户的生日当前发送邮件
// 开启一个定时器
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 为当前的生日的用户发送邮件
//1.获得今天过生日的人
// 获得今天的日期
SimpleDateFormat format=new SimpleDateFormat();
String currentDate=format.format(new Date());
// 根据当前时间从数据查询今天过生日的人
QueryRunner runner =new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from customer where birthday like ?";
List<Customer> customerList=null;
try {
customerList = runner.query(sql,new BeanListHandler<Customer>(Customer.class), "%"+currentDate+"%");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 2.发邮件
if(customerList!=null&&customerList.size()>0){
for(Customer c:customerList){
String emailMsg="亲爱的lucy"+c.getRealname();
try {
MailUtils.sendMail(c.getEmail(), "ceshi", emailMsg);
System.out.println(c.getRealname());
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}, new Date(), 1000*30);
// 实际开发中起始时间是一个固定的时间
// 实际开发中间时间是一天
}
}
package com.itcast.birthday;
public class Customer {
private String username;
private String password;
private String realname;
private String bithday;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getBithday() {
return bithday;
}
public void setBithday(String bithday) {
this.bithday = bithday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}