1.背景介绍:公司10多年一直做oracle ilearning的实施,oracle是不提供源码的,我们只能靠自己反编译、继承等一些手段来实现客户化,系统发送邮件功能有一个配置文件可配置,但是要求不验证邮箱密码通过javamail发送邮件。之前我们面对的客户主要的都是一些大型企业客户,他们一般都有自己的邮件服务器,在邮件服务器上做一下设置就行,但是目前一些客户一般都使用网易、腾讯等企业邮箱,这些供应商一般不会提供免验证发送邮件的。这就需要我们解决这个问题,有两个方案:一、自己搭建一个邮件服务器。二、修改oracle ilearning原始发送邮件功能的代码。
2.进入正题:我这边使用Apache的James mail自己搭建过邮件服务器。但自己搭建邮件代价较大,这里说一下如何修改原始功能代码使用javamail发送邮件。
oracle ilearning邮件发送功能的配置在 ~WEB-INF/ilearning.properties文件中
# ******************************************************************* #Example: MAIL_HOST=smtp02.us.oracle.com MAIL_HOST=smtp.163.com #ADMIN_EMAIL: The email address where system generated emails will be sent from #Recommendation: set to valid email address #Default setting: nobody@<domain> #Example: [email protected] #This email account is used for forgot password emails. Bounced emails #will be returned to this address. [email protected]
MAIL_HOST对应的是邮件服务器发送地址,ADMIN_EMAIL是发件人。
那么oracle ilearning 原始代码是怎么调用的呢?没有源码,只有通过简单的找回密码功能一步一步一层一层的找。终于使用强大的MyEclipse反编译工具插件(附件可下载),找到邮件发送类:oracle.ila.common.collaboration.email.EmailUtil。以下是反编译出来的部分代码
//发送失败后重复尝试发送的次数 public static final int RESEND_EMAIL = 5; //构造函数 public EmailUtil() { Properties props = setMailHost(); this.session = Session.getDefaultInstance(props, null); this.msg = new MimeMessage(this.session); } //设置smtp邮件发送地址配置 private Properties setMailHost(){ Properties _props = new Properties(); _props.put("mail.smtp.host", ILAProperties.getMailHost()); return _props; } //具体的发送逻辑代码 private void transportMessageToMailer(boolean forceSend) throws SendFailedException, MessagingException{ SendFailedException lastError = null; int count = 0; for (int i = 0; i < 5; ++count) { lastError = null; try { Transport.send(this.msg); } catch (SendFailedException es) { Address[] validUnsent = es.getValidUnsentAddresses(); Address[] vailidSent = es.getValidSentAddresses(); Address[] InvalidAddress = es.getInvalidAddresses(); if ((validUnsent == null) && (validUnsent == null) && (InvalidAddress == null)) { lastError = es; } else { if ((forceSend) && (validUnsent != null)) { Transport.send(this.msg, validUnsent); throw new SendFailedException(es.getMessage(), es, validUnsent, null, es.getInvalidAddresses()); } throw es; } } ++i; } if (lastError != null) { Logger.error("EmailUtil ->transportMessageToMailer: SMTP Connection Failed - Have tried " + Integer.toString(count) + " times to send message"); throw lastError; } }
以上反编译的结果肯定会和源码有些差别的,很明显的错误需要修改一下即可。那么我们就来说说,如何修改可实现配置验证邮箱密码发送邮件。
第一步,检查反编译出来的代码是否有错,解决明显的错误,可能大家会忽视下面一个错误:
private void transportMessageToMailer(boolean forceSend) throws SendFailedException, MessagingException { SendFailedException lastError = null; int count = 0; //发编译的结果常量直接变成了具体的值 for (int i = 0; i < 5; ++count) { for (int i = 0; i < RESEND_EMAIL; ++count) { lastError = null; try { Transport.send(this.msg); //这里很明显漏了一个return ,如果不加return那么系统会一次性发送5封相同的邮件。下面加上 return; } catch (SendFailedException es) { Address[] validUnsent = es.getValidUnsentAddresses(); Address[] vailidSent = es.getValidSentAddresses(); Address[] InvalidAddress = es.getInvalidAddresses(); if ((validUnsent == null) && (validUnsent == null) && (InvalidAddress == null)) { lastError = es; } else { if ((forceSend) && (validUnsent != null)) { Transport.send(this.msg, validUnsent); throw new SendFailedException(es.getMessage(), es, validUnsent, null, es.getInvalidAddresses()); } throw es; } } ++i; } if (lastError != null) { Logger.error("EmailUtil ->transportMessageToMailer: SMTP Connection Failed - Have tried " + Integer.toString(count) + " times to send message"); throw lastError; } }
第二步,自己创建添加一个javamail的验证类
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * @author Chan Lewis * @version 2014-3-24 */ public class MyAuthenticator extends Authenticator{ String userName = null; String password = null; public MyAuthenticator() { } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
第三步,修改邮件的初始化配置
//构造函数 public EmailUtil(){ Properties props = setMailHost(); //是否验证密码,可在ilearning.properties中添加配置 String isValidateStr = ILAProperties.getProperty("SMTP_AUTH"); if(null!=isValidateStr && "true".equalsIgnoreCase(isValidateStr)){ //如果验证密码 //添加初始化配置 props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true"); MyAuthenticator authenticator = new MyAuthenticator(ILAProperties.getProperty("ADMIN_EMAIL"), ILAProperties.getProperty("ADMIN_PASSWORD")); this.session = Session.getDefaultInstance(props, authenticator); }else{ //如果不验证密码 this.session = Session.getDefaultInstance(props, null); } this.msg = new MimeMessage(this.session); }
ilearning.properties中添加相关配置SMTP_AUTH 、ADMIN_PASSWORD
# ******************************************************************* #Example: MAIL_HOST=smtp02.us.oracle.com MAIL_HOST=smtp.163.com SMTP_AUTH=true #ADMIN_EMAIL: The email address where system generated emails will be sent from #Recommendation: set to valid email address #Default setting: nobody@<domain> #Example: [email protected] #This email account is used for forgot password emails. Bounced emails #will be returned to this address. [email protected] ADMIN_PASSWORD=password #******************************************************************************
OK到此为止就修改完成了。这样修改之后,既可以使用javamail不验证发送邮件,也可以验证用户名密码发送邮件,具体可在ilearning.properties文件里面配置。
3.问题答疑:有些新手可能还会问,我这边反编译后怎么替换原始功能呢?
答:原始的功能除oracle官方发布的补丁之外,一般都打包在ilearning.jar中,主要看你现在使用的web服务器,一般买了oracle ilearning的基本都是使用oracle IAS服务器的,所以把自己修改的java文件生成类文件放到对应的目录即可。另外一种办法是通过打包技术把生成的类文件重新打包到ilearning.jar(不建议使用)。