javamail文件上传

//创建entity

package cn.bdqn.pojo;

import java.io.File;

public class Mail {
    private String from;
    private String to;
    private String subject;
    private String content;
    private File file;
    private String fileName;
    
    public Mail(){}
    public Mail(String from, String to, String subject, String content, File file, String fileName){
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.content = content;
        this.file = file;
        this.fileName = fileName;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    
}

 

2.创建service

package cn.bdqn.service;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

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

import cn.bdqn.pojo.Mail;

public class MailService {
    private JavaMailSender mailSender;
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
    public void sendMail(Mail mail){
        try{
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            helper.setFrom(mail.getFrom());
            helper.setTo(mail.getTo());
            helper.setSubject(mail.getSubject());
            helper.setText(mail.getContent());
            helper.addAttachment(MimeUtility.encodeWord(mail.getFileName()),mail.getFile());
            mailSender.send(mimeMessage);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

 

3.创建action

 

package cn.bdqn.action;

import java.io.File;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.bdqn.pojo.Mail;
import cn.bdqn.service.MailService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SendMailAction extends ActionSupport{
    private static final long serialVersionUID = 1L;

    private MailService mailService =null;
    private String from;
    private String to;
    private String subject;
    private String content;
    private File upload;
    private String uploadFileName;
    
    @Override
    public String execute() throws Exception {
        Mail mail = new Mail(getFrom(),getTo(),getSubject(),getContent(),getUpload(),getUploadFileName());
        mailService.sendMail(mail);
        return "success";
    }
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MailService service=(MailService)context.getBean("mailService");
        Mail mail = new Mail(getFrom(),getTo(),getSubject(),getContent(),getUpload(),getUploadFileName());
        service.sendMail(mail);
        System.out.println("ok!");
    }
    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }
    
    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}

 

 

4、applicationXml:

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com"></property><!-- 服务器 -->
        <property name="port" value="465"></property><!-- 端口 -->
        <property name="username" value="2226016500@qq.com"></property><!-- 用户名 -->
        <property name="password" value="123.shuai-+.0"></property><!-- 密码 -->
        <property name="protocol" value="SSL" ></property><!-- 协议 -->
        <property name="defaultEncoding" value="utf-8"></property><!-- 默认编码 -->
        <property name="javaMailProperties">
            <props>
                <!-- 设置SMTP服务器需要用户验证  -->
                <prop key="mail.smtp.auth">true</prop>
            </props>
        </property>
    </bean>
    <bean id="mailService" class="cn.bdqn.service.MailService">
        <property name="mailSender" ref="mailSender"></property>
    </bean>

</beans>

 

6、创建struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="sendmailAction" class="cn.bdqn.action.SendMailAction">
            <result name="success">/sendmail_success.jsp</result>
        </action>
    </package>
    
</struts>    

 

6、创建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 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>*.action</url-pattern> -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Spring的配置信息 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!-- <filter>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
     </filter-class>
   </filter> -->
   <!-- <filter-mapping>
      <filter-name>OpenSessionInViewFilter</filter-name>
      <url-pattern>*.action</url-pattern>
   </filter-mapping> -->
</web-app>

 

7、创建接收用户信息界面

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags"  prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>邮件发送页</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <H2>邮件发送</H2><br>
    <s:form action="sendmailAction" enctype="multipart/form-data" method="post">
        <s:textfield name="from" label="发件人" value="tina@mail.com"/>
        <s:textfield name="to" label="收件人"/>
        <s:textfield name="subject" label="主题"/>
        <s:textarea name="content" label="内容"/>
        <s:file name="upload" lable="选择附件"/>
        <s:submit name="submit" value="发送邮件"/>
    </s:form>
    
  </body>
</html>

 

 8、成功页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags"  prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>成功页</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
        <H2>邮件发送成功!</H2>
  </body>
</html>

 

posted on 2016-12-18 15:56 卅年 阅读(...) 评论(...) 编辑 收藏

导航