发送 email 可以用 spring 的 jar 包来发送,如果要在 ofbiz 中引用的话就等于又多了一个框架,所以不用它,只用 javax.mail.*; 和 freemarker 模板jar包。如果不发html格式,那么代码将只依赖javax的mail包适合所有情况 。
核心代码 :
- 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");
-
- multipart.addBodyPart(messageBodyPart);
-
- message.setContent(multipart);
-
- 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 模板结合过的字符串内容:
结合代码 :
- Public static String getBody(Map map, String tmpName) {
-
- StringWriter result = new StringWriter();
-
-
-
- String path = UtilProperties.getConfigureMessage("email.template.path");
-
- if (tmpName == null) {
-
- path +="test";
-
- } else {
-
- path += tmpName;
-
- }
-
- try {
-
- Template template = FreeMarkerWorker.getTemplate(path);
-
- template.process(map, result);
-
-
-
- } catch (TemplateException e1) {
-
- Debug.logError(e1.getMessage().toString(), module);
-
- } catch (IOException e1) {
-
- Debug.logError(e1.getMessage().toString(), module);
-
- }
-
- String body = result.toString();
-
- return body;
-
- }
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;
}
阅读(2095) | 评论(0) | 转发(0) |