Struts2+Spring发送邮件

Spring本身有mail支持,所以用spring发邮件其实是个挺简单的事,看看其jar包

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>

1:pom.xml

需要下载strut2 spring的核心包 mail.jar 以及activation.jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zhao</groupId>
    <artifactId>Struts2_Spring</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Struts2_Spring Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <!-- 只在编译时和测试时运行 -->
            <scope>provided</scope>
        </dependency>
        <!-- Struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24</version>

        </dependency>
        <!-- convention-plugin插件,使用了这个插件之后,就可以采用注解的方式配置Action -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>
        <!-- struts2和spring整合 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mail</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>Struts2_Spring</finalName>
    </build>
</project>

2:spring_bean.xml

第一个bean是spring的支持,第二个bean是我自定义的Struts2 Action,然后依赖注入

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="username" value="username"/>
        <property name="password" value="password" />
    </bean>
    
    <bean id="sendMail" class="com.zhao.action.SendMailAction" scope="singleton">
        <property name="mailSender" ref="mailSender"></property>
    </bean>

3:struts.xml

页面流程也很简单,成功了就进入成功界面

<struts>
    <package name="default" extends="struts-default">
        <action name="send" class="com.zhao.action.SendMailAction">
            <result>success.jsp</result>
        </action>
        <action name="sendHtml" class="com.zhao.action.SendMailAction"
            method="sendHtml">
            <result>success.jsp</result>
        </action>
    </package>
</struts>

4:SendMailAction

我们通过依赖注入,得到了JavaMailSenderImpl的对象,然后通过这个对象进行邮件的发送。

package com.zhao.action;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author: zhao
 * @time: 2016年5月14日
 * 
 * @description:Struts2 + Spring 发送邮件
 */
public class SendMailAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private JavaMailSenderImpl     mailSender;
  
//简单邮件发送 @Override
public String execute() throws Exception { SimpleMailMessage message=new SimpleMailMessage(); message.setTo("to@163.com"); message.setFrom("from@163.com"); message.setSubject("练习"); message.setText("这只是一个简单的练习"); mailSender.send(message); return SUCCESS; }
//html邮件发送
public String sendHtml() throws Exception{ MimeMessage message=mailSender.createMimeMessage(); MimeMessageHelper messageHelper=new MimeMessageHelper(message); messageHelper.setTo("to@163.com"); messageHelper.setFrom("from@163.com"); messageHelper.setSubject("Html练习"); messageHelper.setText("<html><head></head><body><h1>SUCCESS</h1></body></html>",true); mailSender.send(message); return SUCCESS; } public JavaMailSenderImpl getMailSender() { return mailSender; } public void setMailSender(JavaMailSenderImpl mailSender) { this.mailSender = mailSender; } }

5:错误解决

正常来说上面的步骤就足够了,但是是我遇到了两个错误

550 User has no permission
 535 Error: authentication failed

550是说邮箱没有启用ssl/tls认证,所以我们直接发邮件肯定是失败的。

进入网易邮箱,开始相应服务

  

  

在这个过程会让你自己输入一个授权码,跟密码差不多,只不过你采用ssl方式登录时,需要输入授权码 ,而不是你的账号密码。这个授权码就是负责你在第三方登录的。

接下来我又遇到了535错误,这个错误的意思是验证不通过,密码不对。需要修改sprng_bean.xml中的账号密码,改成授权码,然后就好了。

好了,问题完美解决。

posted @ 2016-05-15 10:24  假寐的我  阅读(515)  评论(0编辑  收藏  举报