国外做机器的好的网站,国际版网站可以在国内做推广吗,北京装修公司口碑最好的是哪家,免费外链代发一#xff0e;邮箱服务器的基本概念
邮件的客户端#xff1a;可以只安装在电脑上#xff08;C/S#xff09;的也可以是网页形式#xff08;B/S#xff09;的 邮件服务器#xff1a;起到邮件的接受与推送的作用 邮件发送的协议#xff1a; 协议#xff1a;就是数据传输…一邮箱服务器的基本概念
邮件的客户端可以只安装在电脑上C/S的也可以是网页形式B/S的 邮件服务器起到邮件的接受与推送的作用 邮件发送的协议 协议就是数据传输的约束 接受邮件的协议POP3 、 IMAP 发送邮件的协议SMTP 网易邮箱服务器地址 pop.126.com smtp.126.com imap.126.com
邮件发送与接收的详细过程 二demo—做一个生日祝福定时发送
package beyond.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.创建一个程序与邮件服务器会话对象 SessionProperties 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(yan, beyond);//发邮件的账号验证}};Session session Session.getInstance(props, auth);// 2.创建一个Message它相当于是邮件内容Message message new MimeMessage(session);message.setFrom(new InternetAddress(yanyy.com)); // 设置发送者message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者message.setSubject(subject);//设置邮件的主题// message.setText(这是一封激活邮件请a href#点击/a);message.setContent(emailMsg, text/html;charsetutf-8);// 3.创建 Transport用于将邮件发送Transport.send(message);}
}
package beyond.birthday;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DataSourceUtils {private static DataSource dataSource new ComboPooledDataSource();private static ThreadLocalConnection tl new ThreadLocalConnection();// 直接可以获取一个连接池public static DataSource getDataSource() {return dataSource;}public static Connection getConnection() throws SQLException{return dataSource.getConnection();}// 获取连接对象public static Connection getCurrentConnection() throws SQLException {Connection con tl.get();if (con null) {con dataSource.getConnection();tl.set(con);}return con;}// 开启事务public static void startTransaction() throws SQLException {Connection con getCurrentConnection();if (con ! null) {con.setAutoCommit(false);}}// 事务回滚public static void rollback() throws SQLException {Connection con getCurrentConnection();if (con ! null) {con.rollback();}}// 提交并且 关闭资源及从ThreadLocall中释放public static void commitAndRelease() throws SQLException {Connection con getCurrentConnection();if (con ! null) {con.commit(); // 事务提交con.close();// 关闭资源tl.remove();// 从线程绑定中移除}}// 关闭资源方法public static void closeConnection() throws SQLException {Connection con getCurrentConnection();if (con ! null) {con.close();}}public static void closeStatement(Statement st) throws SQLException {if (st ! null) {st.close();}}public static void closeResultSet(ResultSet rs) throws SQLException {if (rs ! null) {rs.close();}}}
web.xml
listenerlistener-classbeyond.birthday.BirthdayListener/listener-class /listenerpackage beyond.birthday;public class Customer {private int id;private String username;private String password;private String realname;public int getId() {return id;}public void setId(int id) {this.id id;}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 getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday birthday;}public String getEmail() {return email;}public void setEmail(String email) {this.email email;}private String birthday;private String email;}
package beyond.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 beyond.birthday.Customer;
import beyond.mail.MailUtils;public class BirthdayListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {//当web应用启动开启调动---功能在用户的生日当前发送邮件Timer timer new Timer();//开启定时器导util下的包timer.scheduleAtFixedRate(new TimerTask() {Overridepublic void run() {//为当前生日用户发邮件//1获得当前过生日的用户SimpleDateFormat format new SimpleDateFormat(MM-dd);//获得当天的日期String currentDate format.format(new Date());//currentDate----10-14QueryRunner runner new QueryRunner(DataSourceUtils.getDataSource());//根据当前的时间从数据库查询今天过生日的用户String sql select * from customer where birthday like ?;//写SQL语句ListCustomer customerList null;try {customerList runner.query(sql, new BeanListHandlerCustomer(Customer.class),%currentDate);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//2发邮件给当前过生日的用户if(customerList!nullcustomerList.size()0){for(Customer c :customerList){String emailMsg 亲爱的 c.getRealname() ,生日快乐;try {MailUtils.sendMail(c.getEmail(), 生日祝福, emailMsg);System.out.println(c.getRealname()邮件发送完毕);} catch (MessagingException e) {e.printStackTrace();}}}}}, new Date(),1000*30 );//1000ms*30半分钟//实际开发中起始时间肯定是一个固定的时间//period间隔时间 时间开发中间隔时间是1天为一天会有好多人过生日把今天过生日的人给发送生日祝福}Overridepublic void contextDestroyed(ServletContextEvent sce) {// TODO Auto-generated method stub}}