您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java自动发邮件
  所属分类: Java
  开发工具:
  文件大小: 35kb
  下载次数: 0
  上传时间: 2013-07-09
  提 供 者: u0113*****
 详细说明: java自动发邮件不错哦,美女程序猿web.xml文件 mailsenderservlet com.email.MailSenderServlet server smtp.163.com port 25 address username@163.com username username password ****** validate true time 5 1 servlet: package com.email; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class MailSenderServlet extends HttpServlet { private MailSender sender; private Thread t; public void init() throws ServletException { sender = new MailSender(); sender.setRoot(root); String server = getInitParameter("server"); String port = getInitParameter("port"); String address = getInitParameter("address"); String username = getInitParameter("username"); String password = getInitParameter("password"); if (server == null || port == null || address == null || username == null || password == null) { System.out.println("系统文件web.xml错误:邮件发送程序初始化失败!"); return; } sender.setServer(server); sender.setPort(port); sender.setAddress(address); sender.setUsername(username); sender.setPassword(password); String time = getInitParameter("time"); if (time != null) { sender.setTime(Integer.parseInt(time)); } String validate = getInitParameter("validate"); if (validate != null) { sender.setValidate(Boolean.valueOf(validate).booleanValue()); } t = new Thread(sender); t.start(); //启动邮件发送线程 } public void destroy() { sender.stop(); //停止邮件发送线程 try { t.join(1000); if (t.isAlive()) { System.out.println("邮件发送线程未停止。"); } } catch (Exception e) {} } } 邮件发送程序 package com.email; import java.io.*; import java.sql.*; import java.util.*; import java.util.Date; import javax.activation.*; import javax.mail.*; import javax.mail.Message.*; import javax.mail.internet.*; /** *

Copyright: Copyright (c) 2004

* @author flyxxxxx@163.com * @version 1.0 */ public class MailSender implements Runnable { private int time = 5 * 60 * 1000; //扫描数据库时间间隔 private boolean flag = true; //停止线程标记 private String server = "127.0.0.1"; //SMTP服务器地址 private String port = "25"; //SMTP服务器端口 private String address; //用于发送EMAIL的发送者地址 private String username; //发送者的用户名 private String password; //发送者的密码 private boolean validate = true; //邮件服务器是否要求验证 private File root = null; //WEB根目录 MailConfigManager manager = MailConfigManager.getInstance(); public MailSender() { } public void setAddress(String address) { this.address = address; } public void setPassword(String password) { this.password = password; } public void setPort(String port) { if (port != null && port.length() > 0) { this.port = port; } } public void setServer(String server) { this.server = server; } public void setUsername(String username) { this.username = username; } public void setValidate(boolean validate) { this.validate = validate; } public void setTime(int minute) { this.time = minute * 60 * 1000; } public void run() { long lastTime = new Date().getTime(); //保存前一次发送邮件的时间 while (flag) { //服务器停止时退出线程 long k = new Date().getTime() - lastTime; if (k < -1000) { //防止系统修改时间 lastTime = new Date().getTime(); continue; } if (k > time) { //超过设定时间间隔开始发送邮件 sendData(); lastTime = new Date().getTime(); } try { Thread.sleep(100); } catch (Exception e) {} } } public void stop() { flag = false; } /** * 检索数据库,并发送邮件 */ private void sendData() { ResultSet rs=null;//读取数据库数据 try { Session session = Session.getInstance(getProperties(), new Authentic()); while(flag) {//服务器停止时退出线程 String toAddress = null;//发送地址(从rs中得到) String subject = null;//邮件主题 String content = null ;//邮件内容 String file[] = null;//所有附件(绝对路径) sendMail(session, toAddress, subject, content, file);//发送邮件 } } catch (Exception e) { e.printStackTrace(); } finally { if(rs!=null){ try{ rs.close(); } catch(SQLException e){} } } } /** * 发送邮件 * @param session 会话 * @param toAddress 目的地址 * @param subject 邮件主题 * @param content 邮件内容(HTML) * @param files 邮件附件 * @return 是否发送成功 */ private boolean sendMail(Session session, String toAddress, String subject, String content, String[] files) { toAddress = "lijin@regaltec.com.cn"; try { Message rs = new MimeMessage(session); Address from = new InternetAddress(address); rs.setFrom(from); //发送地址 rs.setRecipient(RecipientType.TO, new InternetAddress(toAddress)); //接收地址 rs.setSubject(subject); //邮件主题 Multipart mp = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(content, "text/html; charset=GBK"); //邮件HTML内容 mp.addBodyPart(html); if (files != null && files.length > 0) { //邮件附件 for (int i = 0; i < files.length; i++) { MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource(files[i]); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(MimeUtility.encodeWord(files[i]), "GBK", null)); mp.addBodyPart(mbp); } } rs.setContent(mp); //邮件全部内容 rs.setSentDate(new Date()); //发送时间 Transport.send(rs); //发送 return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 邮件会话属性 * @return 会话属性 */ private Properties getProperties() { Properties rs = new Properties(); rs.put("mail.smtp.host", server); rs.put("mail.smtp.port", port); rs.put("mail.smtp.auth", validate ? "true" : "false"); return rs; } public void setRoot(File root) { this.root = root; } class Authentic extends Authenticator { //验证密码 public Authentic() { } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 发邮件
 输入关键字,在本站1000多万海量源码库中尽情搜索: