`
SavageGarden
  • 浏览: 227047 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

appfuse学习笔记(六)邮件相关

阅读更多
Appfuse的登录页上有一个密码提醒功能,负责把用户注册时填写的密码提醒信息发送到用户注册时填写的邮箱里,默认这个功能是处于关闭状态的,要使用这个功能要首先进行以下设置
1. 修改邮件的资源文件
/myproject/src/main/resources/mail.properties
#负责发送邮件的邮箱
#mail.default.from=AppFuse <appfuse@raibledesigns.com>
mail.default.from=AppFuse <username@126.com>
#是否开启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);
}

分享到:
评论

相关推荐

    appfuse学习笔记(一)安装部署

    **AppFuse 学习笔记(一):安装与部署** AppFuse 是一个开源项目,它提供了一种快速构建企业级 Web 应用程序的方式。它使用了多种流行的技术栈,如 Spring Boot、Hibernate、Thymeleaf 和 Maven,使得开发者可以更...

    AppFuse学习笔记(J2EE入门级框架)

    【AppFuse 框架详解】 AppFuse 是一个由 Matt Raible 创建的开源项目,它为初学者提供了一个基础的 J2EE 框架,用于演示如何集成多个流行的技术,如 ...AppFuse 不仅是一个框架,更是一个学习 J2EE 技术的良好起点。

    appfuse 学习笔记

    ### Appfuse 学习笔记 #### 一、Appfuse 简介 Appfuse 是一个开源框架,旨在帮助开发者高效地构建企业级应用。通过提供一套完善的架构模板、最佳实践和技术栈组合,使得开发者能够专注于业务逻辑的实现,而不是...

    appfuse学习笔记(二)新建模块

    在本篇“appfuse学习笔记(二)新建模块”中,我们将深入探讨AppFuse框架的模块创建过程。AppFuse是一个开源项目,它提供了一个快速开发Web应用的基础结构,旨在简化开发流程并提高代码质量。通过AppFuse,开发者...

    appfuse学习笔记(三)解决乱码和菜单设置

    在本篇“appfuse学习笔记(三)解决乱码和菜单设置”中,我们将深入探讨在使用AppFuse框架时遇到的编码问题以及如何定制应用程序的菜单。AppFuse是一款开源项目,它提供了一个快速开发Web应用的基础,特别是对于Java...

    AppFuse学习笔记

    AppFuse 是一个开源项目,专为加速 J2EE 应用程序开发而设计。...通过本文的学习,读者将能够熟练运用 AppFuse,体验其带来的高效和便捷。同时,结合 Ant 脚本,开发者可以灵活地管理和构建项目,进一步提高开发效率。

    appfuse

    通过理解和学习AppFuse的这些组件及其相互作用,你可以更好地掌握Java Web开发的基础,并且能够利用AppFuse快速创建自己的项目。对于初学者来说,这是一个很好的起点,而对于经验丰富的开发者,它则可以作为一个高效...

    appfuse2学习日记

    ### AppFuse2 学习知识点总结 #### 一、AppFuse 概述 - **定义与价值**:AppFuse 是一款开源项目,旨在利用一系列开源工具帮助开发者高效地搭建 Web 应用程序的基础架构。通过使用 AppFuse,开发人员可以在构建新...

    AppFuse入门文档(AppFuse与SpringMVC+mybatis整合)

    3. **SMTP邮件服务器**: 如果需要使用邮件功能,需安装SMTP邮件服务器,并配置`mail.properties`文件(位于`src/main/resources`目录下)。 4. **Maven**: 下载并安装Maven 3.1.0或更高版本。 5. **Eclipse集成环境*...

    AppFuse

    ### AppFuse:加速J2EE项目开发 #### 一、简介与背景 AppFuse是一个用于启动J2EE项目的工具包,它提供了一种快速而简便的方法来构建基于Java的应用程序。该工具包由Matt Raible创建,他在网络开发领域拥有丰富的...

    APPFUSE工具研究.doc

    AppFuse 是一个基于Java平台的开源项目,旨在加速和简化Web应用程序的开发。它通过集成各种流行框架,如Struts、Spring、Hibernate等,提供了一个项目骨架,使得开发者能够快速搭建新项目的结构。AppFuse分为1.x和...

    appfuse1.4-architecture

    06年时的appfuse,学习SSH架构的经典入门框架。相对比较老的资料,可以欣赏一下当时的架构,向牛人致敬

    SSH学习及开发框架-appfuse

    appfuse 有struts2+hibernate+spring的整合 springmvc+hibernate+spring的整合 多模块,但模块都有 学习开发参考使用非常方便 可以到官方下载最新版的,我只是把自己下载的打包整理一下 注意哈,都是基于maven的...

    appfuse 2.0 教程

    - **JSF**:除了 Spring MVC 和 Struts 2 外,AppFuse 还支持 JSF 框架,提供了相关的文档和教程。 4. **安全性**: - **安全框架集成**:AppFuse 支持多种安全框架的集成,如 Spring Security 等。 - **认证与...

    参考appfuse学习实例

    ssh 博文链接:https://melet.iteye.com/blog/104496

Global site tag (gtag.js) - Google Analytics