Appfuse的登录页上有一个密码提醒功能,负责把用户注册时填写的密码提醒信息发送到用户注册时填写的邮箱里,默认这个功能是处于关闭状态的,要使用这个功能要首先进行以下设置
1. 修改邮件的资源文件
/myproject/src/main/resources/mail.properties
#负责发送邮件的邮箱
#mail.default.from=AppFuse <
[email protected]>
mail.default.from=AppFuse <
[email protected]>
#是否开启debug
mail.debug=false
#负责发送邮件的邮箱的相关信息
mail.transport.protocol=smtp
mail.host=smtp.126.com
mail.username=username
mail.password=password
2. 修改邮件的配置文件
/myproject/src/main/resources/applicationContext-service.xml
……
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="defaultEncoding" value="UTF-8"/>
<!-- Uncomment if you need to authenticate with your SMTP Server -->
<!—这里原来是注销的,如果设置发送邮件,只需把这里的注销放开即可 -->
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<property name="javaMailProperties">
<value>
mail.smtp.auth=true
</value>
</property>
</bean>
……
做完这些修改后,重新编译,启动系统,新注册一个用户,然后退出系统,再在用户名框中填写上新注册的用户名,点击页面下方的”密码提示信息已e-mail形式发送给您”,当然,它这里有一个错别字,应该是“以e-mail形式发送给您”,这需要修改中文的资源文件/myproject/src/main/resources/ApplicationResources_zh.properties
下面来看邮件提醒功能的实现过程
1. 首先查看login.jsp源码
/myproject/src/main/webapp/login.jsp
……
<li>
<label for="j_username" class="required desc">
<fmt:message key="label.username"/> <span class="req">*</span>
</label>
<input type="text" class="text medium" name="j_username" id="j_username" tabindex="1" />
</li>
……
<%@ include file="/scripts/login.js"%>
<p><fmt:message key="login.passwordHint"/></p>
查看资源文件中key所对应的内容
login.passwordHint=忘记了密码? 让系统将 <a href="?" onmouseover="window.status='系统发送密码提示。'; return true" onmouseout="window.status=''; return true" title="系统发送密码提示。" onclick="passwordHint(); return false">密码提示信息以e-mail形式发送给您</a>
2. 再看login.js中的passwordHint()方法
function passwordHint() {
if ($("j_username").value.length == 0) {
alert("<s:text name="errors.requiredField"><s:param><s:text name="label.username"/></s:param></s:text>");
$("j_username").focus();
} else {
location.href="<c:url value="/passwordHint.html"/>?username=" + $("j_username").value;
}
}
可见它会首先校验用户名是否被填写,因为必须要按照用户名来查找其密码提示信息,此处校验通过后将执行location.href="<c:url value="/passwordHint.html"/>?username=" + $("j_username").value;
3. 再来看struts的相关设置
/myproject/src/main/resources/struts.xml
<action name="passwordHint" class="passwordHintAction">
<result name="input">/</result>
<result name="success">/</result>
</action>
/myproject/src/main/webapp/WEB-INF/applicationContext-struts.xml
<bean id="passwordHintAction" class="com.mycompany.app.webapp.action.PasswordHintAction" scope="prototype">
<property name="userManager" ref="userManager"/>
<property name="mailEngine" ref="mailEngine"/>
<property name="mailMessage" ref="mailMessage"/>
</bean>
4. 再来看passwordHintAction的execute方法
/myproject/src/main/java/com/mycompany/app/webapp/action/PasswordHintAction.java
public String execute() {
……
// look up the user's information
try {
User user = userManager.getUserByUsername(username);
String hint = user.getPasswordHint();
if (hint == null || hint.trim().equals("")) {
log.warn("User '" + username + "' found, but no password hint exists.");
addActionError(getText("login.passwordHint.missing"));
return INPUT;
}
StringBuffer msg = new StringBuffer();
msg.append("Your password hint is: ").append(hint);
msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));
mailMessage.setTo(user.getEmail());
String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
mailMessage.setSubject(subject);
mailMessage.setText(msg.toString());
//在mailMessage中填充上邮件信息,由mailEngine完成发送
mailEngine.send(mailMessage);
args.add(username);
args.add(user.getEmail());
saveMessage(getText("login.passwordHint.sent", args));
} catch (UsernameNotFoundException e) {
……
} catch (MailException me) {
……
}
return SUCCESS;
}
5. 再来看spring中如何配置的mailEngine
/myproject/src/main/resources/applicationContext-service.xml
<bean id="mailEngine" class="com.mycompany.app.service.MailEngine">
<property name="mailSender" ref="mailSender"/>
<property name="velocityEngine" ref="velocityEngine"/>
<property name="from" value="${mail.default.from}"/>
</bean>
mailEngine的send(SimpleMailMessage meg)方法
/myproject/src/main/java/com/mycompany/app/service/MailEngine.java
public void send(SimpleMailMessage msg) throws MailException {
try {
mailSender.send(msg);
} catch (MailException ex) {
……
}
}
再回到/myproject/src/main/resources/applicationContext-service.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="defaultEncoding" value="UTF-8"/>
<!-- Uncomment if you need to authenticate with your SMTP Server -->
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<property name="javaMailProperties">
<value>
mail.smtp.auth=true
</value>
</property>
</bean>
mailSender的send(SimpleMailMessage simpleMessage)方法
public void send(SimpleMailMessage simpleMessage) throws MailException {
send(new SimpleMailMessage[] { simpleMessage });
}
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
List mimeMessages = new ArrayList(simpleMessages.length);
for (int i = 0; i < simpleMessages.length; i++) {
SimpleMailMessage simpleMessage = simpleMessages[i];
MimeMailMessage message = new MimeMailMessage(createMimeMessage());
//将simpleMessage中的邮件信息拷贝到message中
simpleMessage.copyTo(message);
mimeMessages.add(message.getMimeMessage());
}
doSend((MimeMessage[]) mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), simpleMessages);
}