Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6514501
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2011-09-27 22:00:26

发送 email 可以用 spring jar 包来发送,如果要在 ofbiz 中引用的话就等于又多了一个框架,所以不用它,只用 javax.mail.*; freemarker 模板jar包。如果不发html格式,那么代码将只依赖javax的mail包适合所有情况

 核心代码

Java代码 复制代码 收藏代码
  1. Properties _props  = System.getProperties();   
  2.   
  3. _props.put("mail.smtp.host""localhost");//放置邮件服务器地址    
  4.   
  5.      Session session = Session.getDefaultInstance(_props, null);       
  6.   
  7.      MimeMessage message = new MimeMessage(session);//多用途网际邮件扩充协议的邮件信息对象   
  8.   
  9.     
  10.   
  11.      message.setFrom(new InternetAddress(sender));//发送者信息   
  12.   
  13.                InternetAddress[] ia = new InternetAddress[1];   
  14.   
  15.                ia[1] = "test@test.com";   
  16.   
  17.      message.addRecipients(Message.RecipientType.TO, ia);//接收者地址   
  18.   
  19.                InternetAddress[] ia1 = new InternetAddress[1];   
  20.   
  21.                ia1[1] = "bbc@bbc.com";   
  22.   
  23.      message.addRecipients(Message.RecipientType.BCC, ia1);//密文抄送地址   
  24.   
  25.      message.setSubject("hello");//主题   
  26.   
  27.           Multipart multipart = new MimeMultipart();//邮件内容复合主体   
  28.   
  29.           MimeBodyPart messageBodyPart = new MimeBodyPart();//邮件内容单体   
  30.   
  31.      messageBodyPart.setContent(body,"text/plain");//邮件内容,是文本的,还是text/html页面格式的   
  32.   
  33.           multipart.addBodyPart(messageBodyPart);//单体内容加入到复合主体中   
  34.   
  35.      message.setContent(multipart);//邮件加入内容信息   
  36.   
  37.      Transport.send(message);//发送邮件  
Properties _props = System.getProperties(); _props.put("mail.smtp.host", "localhost");//放置邮件服务器地址 Session session = Session.getDefaultInstance(_props, null); MimeMessage message = new MimeMessage(session);//多用途网际邮件扩充协议的邮件信息对象 message.setFrom(new InternetAddress(sender));//发送者信息 InternetAddress[] ia = new InternetAddress[1]; ia[1] = "test@test.com"; message.addRecipients(Message.RecipientType.TO, ia);//接收者地址 InternetAddress[] ia1 = new InternetAddress[1]; ia1[1] = "bbc@bbc.com"; message.addRecipients(Message.RecipientType.BCC, ia1);//密文抄送地址 message.setSubject("hello");//主题 Multipart multipart = new MimeMultipart();//邮件内容复合主体 MimeBodyPart messageBodyPart = new MimeBodyPart();//邮件内容单体 messageBodyPart.setContent(body,"text/plain");//邮件内容,是文本的,还是text/html页面格式的 multipart.addBodyPart(messageBodyPart);//单体内容加入到复合主体中 message.setContent(multipart);//邮件加入内容信息 Transport.send(message);//发送邮件 

 

 

 

其中邮件内容body为已经和 freemarker 模板结合过的字符串内容:

结合代码

 

 

Java代码 复制代码 收藏代码
  1. Public static String getBody(Map map, String tmpName) {   
  2.   
  3. StringWriter result = new StringWriter();//输出流   
  4.   
  5.     
  6.   
  7.         String path = UtilProperties.getConfigureMessage("email.template.path");//freemarker模板地址   
  8.   
  9.         if (tmpName == null) {   
  10.   
  11.             path +="test";//默认模板   
  12.   
  13.         } else {   
  14.   
  15.             path += tmpName;   
  16.   
  17.         }   
  18.   
  19.         try {   
  20.   
  21.             Template template = FreeMarkerWorker.getTemplate(path);//取模板   
  22.   
  23.             template.process(map, result);//将所给的map中K,Value值与模板中的值相匹配,将结果放入输出流   
  24.   
  25.             //注意,map中的键值对一定要和模板中的值相匹配   
  26.   
  27.         } catch (TemplateException e1) {   
  28.   
  29.             Debug.logError(e1.getMessage().toString(), module);   
  30.   
  31.         } catch (IOException e1) {   
  32.   
  33.             Debug.logError(e1.getMessage().toString(), module);   
  34.   
  35.         }   
  36.   
  37.         String body = result.toString();   
  38.   
  39.         return body;   
  40.   
  41. }  
Public static String getBody(Map map, String tmpName) { StringWriter result = new StringWriter();//输出流 String path = UtilProperties.getConfigureMessage("email.template.path");//freemarker模板地址 if (tmpName == null) { path +="test";//默认模板 } else { path += tmpName; } try { Template template = FreeMarkerWorker.getTemplate(path);//取模板 template.process(map, result);//将所给的map中K,Value值与模板中的值相匹配,将结果放入输出流 //注意,map中的键值对一定要和模板中的值相匹配 } catch (TemplateException e1) { Debug.logError(e1.getMessage().toString(), module); } catch (IOException e1) { Debug.logError(e1.getMessage().toString(), module); } String body = result.toString(); return body; } 

 

 

阅读(2028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~