`
webdev2014
  • 浏览: 679562 次
文章分类
社区版块
存档分类
最新评论

Java发送邮件

 
阅读更多

在项目开发中有一个功能是常常 会用到的,那就是“找回密码功能”,该功能有两种方法实现,一是通过手机号码方式来找回,二是通过电子邮箱的方式找回,这里我使用第二种方式来实现。

实现这一功能就是通过我们的java程序像指定的邮箱发送一封邮件,该邮件内容中包含一个连接,该链接指向的是我们项目中的修改密码界面,当用户点击该链接,则定向到修改密码界面

这里我使用的是Spring封装的邮件发送类来实现的,另外还要使用mail-*.*.jar邮件的第三方包

具体操作:

创建一个web项目是必须的

创建相关的配置文件 applicationContext.xml,mail.properties,struts.xml

创建一个Action类,MailAction.java


配置文件内容

applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
			default-autowire="byName" default-lazy-init="true">
		<!-- 属性文件读入 -->
		<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="locations">
				<list>
					<value>classpath*:mail.properties</value>
				</list>
			</property>
		</bean>
		
		<!-- 使用Spring封装的Mail Sender-->
		<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
			<property name="host" value="${mail.host}"/>
			<property name="username" value="${mail.username}"/>
			<property name="password" value="${mail.password}"/>
			<property name="javaMailProperties">
				<props>
					<prop key="mail.smtp.auth">true</prop>
					<prop key="mail.smtp.timeout">25000</prop>
				</props>
			</property>
		</bean>
		
		<!-- MailAction -->
		<bean id="mailAction" class="com.tenghu.mail.action.MailAction"/>
</beans>

mail.properties配置

mail.host=host
mail.username=username
mail.password=password
host代表的是你自己邮箱的网关,如果你的邮箱是qq邮箱,那么该host就为smtp.qq.com

username发送方邮箱用户名

password发送方邮箱密码

struts.xml配置

<struts>
	<package name="mail" extends="struts-default" namespace="/">
		<action name="mail" class="mailAction">
			<result name="success">mail.jsp</result>
			<result name="error">index.jsp</result>
		</action>
	</package>
</struts>

web.xml配置

<!-- 加载Spring文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:*.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置Struts2核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

需要的配置文件到这里就配置完成,接下来编写具体发送的编码,在action中

package com.tenghu.mail.action;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.opensymphony.xwork2.ActionSupport;

public class MailAction extends ActionSupport{
	private JavaMailSender mailSender;
	private String info;
	@Override
	public String execute() throws Exception {
		try {
			//发送方邮箱
			String email="";
			//通过JavaMailSender创建MimeMessage对象
			MimeMessage message=mailSender.createMimeMessage();
			//创建MimeMessageHelper
			MimeMessageHelper helper=new MimeMessageHelper(message, true,"UTF-8");
			helper.setFrom("");//发送方邮箱
			helper.setTo(email);//接收方邮箱
			helper.setSubject("2014马上有钱");//邮箱主题
			helper.setText("祝您在2014年里,马上有钱,马上有对象。总之马上什么都有",true);//邮箱正文,如果想将内容中的html标签也显示出来,就将true去掉即可
			mailSender.send(message);//发送邮件
			info="邮件发送成功!请前往"+email+"查看您的邮件是否收到!!!";
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			info="邮件发送失败"+e.getMessage();
			return ERROR;
		}
		
	}
	public JavaMailSender getMailSender() {
		return mailSender;
	}
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
}
发送界面index.jsp

<a href="mail">发送</a>
    <!-- 发送失败信息 -->
    <c:if test="${info!=''}">${info}</c:if>

成功界面mail.jsp

${info}//输出发送成功信息

这样就完成了java邮件发送功能





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics