为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

javamail 发送邮件

2012-10-08 5页 pdf 181KB 38阅读

用户头像

is_740086

暂无简介

举报
javamail 发送邮件 public class SendMail implements Runnable { private String to; //接收方 private String subject; //标题 private String content; //邮件内容 private Thread thread; //定义线程 private String sname; //邮件署名 private List attach; private String type = ...
javamail 发送邮件
public class SendMail implements Runnable { private String to; //接收方 private String subject; //标题 private String content; //邮件内容 private Thread thread; //定义线程 private String sname; //邮件署名 private List attach; private String type = "text/html"; //or text/plain 发送邮件的格式 private String smtpHost; //邮件服务器 smtp.163.com等 private String emailFrom; //发送方邮箱 private String fromPwd; //密码 //带模板的邮件 public SendMail(String to, String subject, String content, String fileName) { try { if(to != null && !"".equals(to)) { thread = new Thread(this); this.to = to; this.subject = subject; attach = new ArrayList(); //读取文件模板 File file=new File(fileName); BufferedReader br = new BufferedReader(new FileReader(file)); StringBuffer sb=new StringBuffer(); while (br.ready()) { sb.append(br.readLine()); sb.append("\r\n"); } //賛换模块中的##content变量内容为content this.content=sb.toString().replaceAll("##content",content); } } catch(Exception e) { e.printStackTrace(); } j a v a ? ? ? ? :h t t p :/ /5 1c cs j .t a o b a o .c o m / j a v a ? ? ? ? :h t t p :/ /c h e n g ch e n g t e ch .t a o b a o .c o m / } //带上署名和模块的邮件 public SendMail(String sname, String to, String subject, String content,String fileName) { try { thread = new Thread(this); this.to = to; this.subject = subject; this.sname=sname; attach = new ArrayList(); //读取文件模板 File file=new File(fileName); BufferedReader br = new BufferedReader(new FileReader(file)); StringBuffer sb = new StringBuffer(); while (br.ready()) { sb.append(br.readLine()); } this.content = sb.toString().replaceAll("##caicainet_content",content); } catch(Exception e) { e.printStackTrace(); } } //普通邮件 public SendMail(String sname,String to, String subject, String content,int typeId) { thread = new Thread(this); this.to = to; this.subject = subject; this.sname=sname; attach = new ArrayList(); this.content=content; this.type = "text/plain"; } public void run() { try j a v a ? ? ? ? :h t t p :/ /5 1c cs j .t a o b a o .c o m / j a v a ? ? ? ? :h t t p :/ /c h e n g ch e n g t e ch .t a o b a o .c o m / { send(); } catch (Exception e) { e.printStackTrace(); } } public void start() { thread.start(); } public boolean send() throws Exception { return send(to, subject, content); } private boolean send(String to, String subject, String content) { boolean blnResult = false; InternetAddress[] address = null; //mailserver邮件服务器 Form. 发送邮件的邮箱 pwd密码 //此处三个参数可能通过数据库或.properties等方式来获取,方便后期的管理与设置 String mailserver = "邮件服务器地址"; String From = "发信者邮箱"; } else { msg.setText(messageText); } Transport transport = mailSession.getTransport("smtp"); transport.connect(mailserver, From, pwd); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); Date d2=new Date(); System.out.println("发送给"+to+"成功!耗时"+(d2.getTime()-d1.getTime())+"毫秒,发送方:"+From); blnResult=true; } catch(MessagingException mex) j a v a ? ? ? ? :h t t p :/ /5 1c cs j .t a o b a o .c o m / j a v a ? ? ? ? :h t t p :/ /c h e n g ch e n g t e ch .t a o b a o .c o m / { mex.printStackTrace(); blnResult=false; } catch(UnsupportedEncodingException e) { e.printStackTrace(); } return blnResult; } public void setTo(String to) { this.to = to; } public String getTo() { return this.to; } public void setSubject(String subject) { this.subject = subject; } public String getSubject() { return this.subject; } public void setContent(String content) { this.content = content; } public String getContent() { return this.content; } public void addAttach(String file) { attach.add(file); } j a v a ? ? ? ? :h t t p :/ /5 1c cs j .t a o b a o .c o m / j a v a ? ? ? ? :h t t p :/ /c h e n g ch e n g t e ch .t a o b a o .c o m / public boolean isAttach() { return attach != null && attach.size() > 0; } public static void main(String[] args) { try { //普通的文字邮件 SendMail sendMail=new SendMail("署名","邮件地址", "标题", "内容",1); sendMail.start(); //带模版的邮件 //String path = "D:/project/tongji/WebRoot/template/email.html"; //获取存放路径 //String chtml=""; //SendMail sendMail=new SendMail("邮件地址,"内容",chtml,path); //sendMail.start(); } catch (Exception e) { e.printStackTrace(); } } } j a v a ? ? ? ? :h t t p :/ /5 1c cs j .t a o b a o .c o m / j a v a ? ? ? ? :h t t p :/ /c h e n g ch e n g t e ch .t a o b a o .c o m /
/
本文档为【javamail 发送邮件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索