EBS中使用JAVA方式发送HTML格式邮件
转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769
EBS中使用JAVA方式发送HTML格式邮件
二、注意事项:
package cux.oracle.apps.pos.Util; import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Date;
import java.util.Properties; import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import oracle.apps.fnd.cp.request.JavaConcurrentProgram; /**
* 创建 HTML 格式的邮件
*
* @author Jason Gu
*/
public class SendHtmlMail
{ public String sendMessage(String host, String user, String pwd, String from,
String to, String subject,
String body) throws MessagingException,
java.io.UnsupportedEncodingException
{
Properties props = new Properties(); // 设置发送邮件的邮件服务器的属性
props.put("mail.smtp.host", host); // 需要经过授权,也就是用户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true"); // 创建该邮件应用程序所需的环境信息以及会话信息
Session session = Session.getDefaultInstance(props); // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
// 用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true); // 根据上面的 Session 实例创建 MimeMessage 实例,即一封邮件
MimeMessage msg = new MimeMessage(session); try
{
// 设置发件人地址
msg.setFrom(new InternetAddress(from)); // 设置收件人地址
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // 设置 E-mail 主题
msg.setSubject(subject); // 设置发送时间
msg.setSentDate(new Date()); // 设置 E-mail 正文部分
// msg.setText(body);
msg.setContent(body, "text/html;charset = UTF-8"); // 保存对该 MimeMessage 实例的更改
msg.saveChanges(); // 发送邮件
Transport transport = session.getTransport("smtp"); // 连接服务器的邮箱
transport.connect(host, user, pwd); // 把邮件发送出去
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
// 将 msg 对象中内容写入文件
msg.writeTo(new FileOutputStream("SendHtmlMail.eml"));
return "S";
} catch (Exception e)
{
e.printStackTrace();
return "E";
}
} public static String main(String to,String subject,String body) throws MessagingException,
UnsupportedEncodingException
{
String host = "172.17.27.249"; // smtp服务器
String user = "Ebs-admin"; // 用户名
String pwd = "[CONTRACT]2013(approval)"; // 密码
String from = "Ebs-admin@jd.com"; SendHtmlMail sendmail = new SendHtmlMail();
String result =
sendmail.sendMessage(host, user, pwd, from, to, subject, body);
return result;
}
}
package cux.oracle.apps.pos.Util; import java.sql.PreparedStatement; import java.util.Date; import java.text.SimpleDateFormat; import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.cp.request.CpContext;
import oracle.apps.fnd.cp.request.JavaConcurrentProgram;
import oracle.apps.fnd.cp.request.LogFile;
import oracle.apps.fnd.cp.request.ReqCompletion;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.util.ParameterList; import java.sql.Connection;
import java.sql.ResultSet; import oracle.apps.fnd.util.NameValueType; import cux.oracle.apps.pos.Util.SendHtmlMail; /**
* 请求调用生成 HTML 格式的邮件
*
* @author Jason Gu
*/
public class CuxPtebsReport implements JavaConcurrentProgram
{
public static final String RCS_ID =
"$Header: CuxRepayReport.java 120.1 2013/09/06 14:36:06 sabatra noship $";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion("$Header: CuxRepayReport.java 120.1 2013/09/06 14:36:06 sabatra noship $",
"%packagename%");
protected LogFile log;
protected ReqCompletion reqc;
private static SimpleDateFormat mDateFormat;
private String l_return_status = "S"; public void runProgram(CpContext cpContext)
{
ParameterList lPara = cpContext.getParameterList();
StringBuffer com_content = new StringBuffer();
StringBuffer dept_content = new StringBuffer();
String sendresult = "S";
String cux_combody = new String();
String cux_deptbody = new String();
String body = new String();
try
{
this.log = cpContext.getLogFile();
this.reqc = cpContext.getReqCompletion(); Connection con = cpContext.getJDBCConnection(); this.log.writeln("input parameters list:", 1);
while (lPara.hasMoreElements())
{
NameValueType nvt = lPara.nextParameter();
this.log.writeln(nvt.getName() + ":" + nvt.getValue(), 1); }
//获取当前日期
java.util.Calendar c = java.util.Calendar.getInstance();
java.text.SimpleDateFormat f =
new java.text.SimpleDateFormat("yyyy年MM月dd日");
//邮件主题
String subject = f.format(c.getTime()) + "新增公司和部门";
//表格标题
String com = f.format(c.getTime()) + "数据新增公司";
String dept = f.format(c.getTime()) + "数据新增部门";
String comheader =
"<table border=1 cellspacing=0 cellpadding=2><tr><td colspan=4 align=center>" +
com +
"</td></tr><tr><td nowrap>公司编码</td><td nowrap>公司名称</td><td nowrap>生效日期</td><td>最后更新日期</td></tr>";
String combody =
"<tr align=left><td nowrap>VAR_A</td><td nowrap>VAR_B</td><td nowrap>VAR_C</td><td nowrap>VAR_D</td></tr>";
String deptheader =
"<table border=1 cellspacing=0 cellpadding=2><tr><td colspan=4 align=center>" +
dept +
"</td></tr><tr><td nowrap>部门编码</td><td nowrap>部门名称</td><td nowrap>生效日期</td><td>最后更新日期</td></tr>";
String deptbody =
"<tr align=left><td nowrap>VAR_E</td><td nowrap>VAR_F</td><td nowrap>VAR_G</td><td nowrap>VAR_H</td></tr>";
String foot = "</table><br><br>"; PreparedStatement compre = null;
PreparedStatement deptpre = null;
PreparedStatement updatecompre = null;
PreparedStatement updatedeptpre = null;
PreparedStatement mailtopre = null;
ResultSet comresult = null;
ResultSet deptresult = null;
ResultSet mailtoresult = null;
ResultSet updatecomresult = null;
ResultSet updatedeptresult = null;
this.log.writeln("start datebase connection...", 1); //新增公司数据
String comsql =
"SELECT company_code, company_name, to_char(start_date, 'yyyy-mm-dd') start_date, to_char(last_update_date,'yyyy-mm-dd') last_update_date \n" +
"FROM cux_test_com_data WHERE send_flag = 'N' \n"; compre = con.prepareStatement(comsql);
comresult = compre.executeQuery();
while (comresult.next())
{
this.log.writeln("com_code:" + comresult.getString("company_code"), 1);
cux_combody = combody;
cux_combody =
cux_combody.replace("VAR_A", comresult.getString("company_code"));
cux_combody =
cux_combody.replace("VAR_B", comresult.getString("company_name"));
String com_start_date = comresult.getString("start_date");
if (com_start_date == null)
{
com_start_date = " ";
}
String date = com_start_date;
cux_combody = cux_combody.replace("VAR_C", date);
cux_combody =
cux_combody.replace("VAR_D", comresult.getString("last_update_date"));
com_content.append(cux_combody);
} //部门新增数据
String deptsql =
"SELECT dept_code, dept_name, to_char(start_date,'yyyy-mm-dd') start_date,to_char(last_update_date,'yyyy-mm-dd') last_update_date \n" +
" FROM cux_test_dept_data where send_flag = 'N'";
deptpre = con.prepareStatement(deptsql);
deptresult = deptpre.executeQuery();
//deptresult.
while (deptresult.next())
{
cux_deptbody = deptbody;
cux_deptbody =
cux_deptbody.replace("VAR_E", deptresult.getString("dept_code"));
cux_deptbody =
cux_deptbody.replace("VAR_F", deptresult.getString("dept_name"));
String dept_start_date = deptresult.getString("start_date");
if (dept_start_date == null)
{
dept_start_date = " ";
}
String start_date = dept_start_date;
cux_deptbody = cux_deptbody.replace("VAR_G", start_date);
cux_deptbody =
cux_deptbody.replace("VAR_H", deptresult.getString("last_update_date"));
dept_content.append(cux_deptbody);
} int resp_id = cpContext.getRespId();
this.log.writeln("resp_id:" + resp_id, 2);
//获取职责id
String addrsql =
"SELECT distinct pf.email_address\n" + " FROM fnd_user_resp_groups_direct c, fnd_responsibility r, fnd_user fu, per_all_people_f pf\n" +
" WHERE c.responsibility_id = r.responsibility_id\n" +
" AND c.user_id = fu.user_id\n" +
" AND pf.email_address is not null\n" +
" AND fu.employee_id = pf.person_id\n" +
" AND c.responsibility_id = " + resp_id;
//this.log.writeln("addrsql:" + addrsql, 2);
mailtopre = con.prepareStatement(addrsql);
mailtoresult = mailtopre.executeQuery();
while (mailtoresult.next())
{
String addr = mailtoresult.getString("email_address");
this.log.writeln("addr:" + addr, 1);
if (addr != null && addr != "")
{
if (!"".equals(com_content.toString()) ||
!"".equals(dept_content.toString()))
{
this.log.writeln("com not null or dept not null in", 4);
//收件人
//to = "jisuqing@jd.com";
//String to = "cwzhaorui@jd.com";
//邮件内容
if (com_content.toString().equals("") &&
!"".equals(dept_content.toString()))
{
this.log.writeln("com null,dept not null", 4);
body = deptheader + dept_content.toString() + foot;
} else if (!"".equals(com_content.toString()) &&
dept_content.toString().equals(""))
{
this.log.writeln("com not null,dept null", 4);
body = comheader + com_content.toString() + foot;
} else if (!"".equals(com_content.toString()) &&
!"".equals(dept_content.toString()))
{
this.log.writeln("com not null,dept not null", 4);
body =
comheader + com_content.toString() + foot + deptheader + dept_content.toString() +
foot;
}
//发送邮件
SendHtmlMail sendmail = new SendHtmlMail();
sendresult = sendmail.main(addr, subject, body);
} else
{
this.log.writeln("没有需要发送的内容", 4);
}
} else
{
write("resp_id:" + resp_id + "未维护邮箱");
}
} if (!"S".equals(sendresult))
{
this.l_return_status = "E";
} if ("S".equals(this.l_return_status))
{
this.reqc.setCompletion(0, "program completed Successfully.");
String update_com_sql =
"UPDATE cux_test_com_data c\n" + " SET c.send_flag = 'Y', c.last_update_date = SYSDATE, c.last_updated_by = fnd_global.user_id\n" +
" WHERE c.send_flag = 'N'";
updatecompre = con.prepareStatement(update_com_sql);
updatecomresult = updatecompre.executeQuery();
String update_dept_sql =
"UPDATE cux_test_dept_data d\n" + " SET d.send_flag = 'Y', d.last_update_date = SYSDATE, d.last_updated_by = fnd_global.user_id\n" +
" WHERE d.send_flag = 'N'";
updatedeptpre = con.prepareStatement(update_dept_sql);
updatedeptresult = updatedeptpre.executeQuery();
} else
{
this.reqc.setCompletion(2,
"program completed with error. Please see request log for details.");
}
} catch (OAException localOAException)
{
localOAException.printStackTrace();
this.reqc.setCompletion(2, localOAException.getMessage());
} catch (Exception localException)
{
localException.printStackTrace();
this.reqc.setCompletion(2, localException.toString());
}
} protected void write(String paramString)
{
this.log.writeln(getCurrDateStr() + paramString, 0);
} private String getCurrDateStr()
{
if (mDateFormat == null)
{
mDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss:SSS");
} return "[" + mDateFormat.format(new Date()) + "] ";
}
}
EBS中使用JAVA方式发送HTML格式邮件的更多相关文章
- python测试开发django-29.发送html格式邮件
前言 上一篇已经通过send_mail()函数发送纯文本的邮件,发送成功了,如果我们想发送一个html格式的邮件,如何实现呢? 发送html格式的邮件实际上还是调用send_mail()函数 ,只需多 ...
- 用Java发送HTML格式邮件测试类(支持中文)
代码由纯Java写成,支持中文,一目了然,只要将Main函数中的相关信息填写正确就直接用了,便于修改,可以在此类基础上任意扩展成自己的类. 注意做HTML形式的邮件,最好把HTML,CSS都写全,只写 ...
- [Java] JavaMail 发送 html 格式、带附件的邮件
本案例演示发送 html 格式,可带附件的邮件发送.发送纯文本邮件的例子可参照上一篇博文JavaMail 简单案例. EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名 ...
- 一个用于发送HTML格式邮件的类
以下类是在网上孙钰佳的版本上改写而来,主要变化了三点:1.去掉了附件部分:2.形式从纯Java类改成可注入方式:3.to,cc和bcc都变成了一堆人,以前是一个人. 以下是Java类的代码: impo ...
- Play framework框架中通过post方式发送请求
搞了好久这个最终还是在play官方文档中看见的发送请求的方式,国内好像很少有使用这个框架的,加之自己不是太愿意宣传,好东西总归是好东西,不说废话了. 在play中发送请求有两种常用的方式,一种get, ...
- EBS中使用java进行 JavaConcurrentProgram 请求获取参数
public class MainTest implements JavaConcurrentProgram { //实现interface中的runProgram方法 public void run ...
- java代码发送JSON格式的httpPOST请求
package com.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOE ...
- (转)java代码发送JSON格式的httpPOST请求
import Java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...
- java mail发送html格式的邮件
// 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("ma ...
随机推荐
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- JavaScript之Promise对象
含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了 Promise 对象. ...
- 11_Python的列表推导式_Python编程之路
上一节我们结束了有关python的方法相关内容 这一节我们先来学习python的列表推导式 之前跟大家说过range方法的使用,在这里我就不做过多的解释了,如忘记了,请翻阅之前的介绍 列表推导式,是P ...
- 【NOIP 2017】列队
Description Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为 n ...
- bzoj 4870: [Shoi2017]组合数问题
Description Solution 考虑这个式子的组合意义: 从 \(n*k\) 个球中取若干个球,使得球的数量 \(\%k=r\) 的方案数 可以转化为 \(DP\) 模型,设 \(f[i][ ...
- 【BZOJ1036】【ZJOI2008】数的统计
接着找树剖的题...传送门(点我) 题意:给你一棵无根树,有三种操作:查询树上2点路径的点权和/最大点权:更改某点的点权. 解题思路:树链剖分裸题,我采用了常数较小的zkw线段树维护剖下来的树(毕竟线 ...
- Linux上安装Libssh2
由于项目需要使用libssh2,在安装时,遇到一些问题,发现网上的都是互相抄,把自己遇到的问题,记下来,希望可以帮助到别人,自己下次使用时候,也方便查找,节约时间. 安装的流程: 1.下载源码,wge ...
- poj3185 高斯消元
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5329 Accepted: 2081 D ...
- SpringBoot学习之自动依赖
在前面使用SSM集成时,我们可以使用注解实现无配置化注入,但是这种依赖被进行“人工干预了的”,换句话就是说我们手动进行装配,那么此时还没有达到SpringBoot这种自动装配的效果,那么究竟Sprin ...
- POJ-2299 Ultra-QuickSort---树状数组求逆序对+离散化
题目链接: https://vjudge.net/problem/POJ-2299 题目大意: 本题要求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 两个数(a, ...