此次示例有点仿照163邮箱的收件箱,不过目前实现的功能非常少,重点在练习POP3协议解析电子邮件的知识点,所以此Web程序只是随便做做(用的是Struts2)。目前未完全实现的效果图:
Web工程结构:
所有代码:
- /**
- * ParseEmailTest
- * 解决中文乱码的过滤器
- */
- package com.labci.javamail.filter;
- import java.io.IOException;
- import java.util.logging.LogRecord;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[3:06:09 PM]
- *
- */
- public class CharacterEncodingFilter implements Filter,
- java.util.logging.Filter {
- private String encoding="gbk";
- public void destroy() {
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- request.setCharacterEncoding(encoding);
- response.setContentType("message/rfc822");
- chain.doFilter(request, response);
- }
- public void init(FilterConfig config) throws ServletException {
- encoding=config.getInitParameter("encode");
- }
- public boolean isLoggable(LogRecord record) {
- return false;
- }
- }
- /**
- * ParseEmailTest
- * 封装Message的vo类
- */
- package com.labci.javamail.vo;
- import javax.mail.Message;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[1:43:00 PM]
- *
- */
- public class MessageVo {
- private Message msg;//邮件实体类
- private int messageId;//自定义的邮件id
- private String from;//邮件发送人地址
- private String personal;//邮件发送显示名
- private String subject;//邮件主题
- private String content;//邮件内容,此字段暂时未用上
- private String sentDate;//收件箱列表时间的显示格式
- private boolean unRead;//判断邮件是否是新邮件
- private boolean hasAttach=false;//是否有附件
- public Message getMsg() {
- return msg;
- }
- public void setMsg(Message msg) {
- this.msg = msg;
- }
- public String getFrom() {
- return from;
- }
- public void setFrom(String from) {
- this.from = from;
- }
- public int getMessageId() {
- return messageId;
- }
- public void setMessageId(int messageId) {
- this.messageId = messageId;
- }
- public boolean isHasAttach() {
- return hasAttach;
- }
- public void setHasAttach(boolean hasAttach) {
- this.hasAttach = hasAttach;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public String getPersonal() {
- return personal;
- }
- public void setPersonal(String personal) {
- this.personal = personal;
- }
- public String getSentDate() {
- return sentDate;
- }
- public void setSentDate(String sentDate) {
- this.sentDate = sentDate;
- }
- public boolean isUnRead() {
- return unRead;
- }
- public void setUnRead(boolean unRead) {
- this.unRead = unRead;
- }
- }
- /**
- * ParseEmailTest
- * 一个简单地邮件分页组件
- */
- package com.labci.javamail.utils;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.labci.javamail.vo.MessageVo;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[9:29:53 AM]
- *
- */
- public class MessagePageBean {
- //总邮件数
- private List<MessageVo> recordsList=null;
- //当前页码
- private Integer pageNo=1;
- //总页数
- @SuppressWarnings("unused")
- private Integer totalPage=1;
- //总记录数
- private Integer totalCount=0;
- //每页大小
- private Integer pageSize=20;
- //是否有下一页
- private Boolean hasNext=false;
- //是否有上一页
- private Boolean hasPrev=false;
- /**
- * 构造初始化邮件列表和邮件总数
- * @param recordsList
- */
- public MessagePageBean(List<MessageVo> recordsList) {
- this.recordsList = recordsList;
- if(!isListNull(recordsList)){
- totalCount=recordsList.size();
- }
- }
- /**
- * 取得每页的邮件数据
- * @param pageNo 当前页码
- * @param pageSize 取多少条记录
- * @return {"currentPage":当前第几页,"pageSize":每页大小,"totalCount":总记录数,"totalPage":总页数,
- * "recordsList":List<Message> list 每页邮件列表,"hasNext":是否有下一页,"hasPrev":是否有上一页}
- */
- public Map<String,Object> getPageRecordsMap(int pageNo,int pageSize){
- Map<String,Object> pageRecordsMap=new HashMap<String,Object>();
- pageRecordsMap.put("currentPage", pageNo);
- pageRecordsMap.put("pageSize", pageSize);
- pageRecordsMap.put("totalCount", getTotalCount());
- pageRecordsMap.put("totalPage", getTotalPage());
- int fromIndex=0;
- int toIndex=0;
- fromIndex=pageSize*(pageNo-1);
- toIndex=fromIndex+pageSize;
- if(toIndex>getTotalCount()){
- toIndex=getTotalCount();
- }
- if(fromIndex<0){
- fromIndex=0;
- }
- setPageNo(pageNo);
- pageRecordsMap.put("recordsList", recordsList.subList(fromIndex, toIndex));
- pageRecordsMap.put("hasNext", getHasNext());
- pageRecordsMap.put("hasPrev", getHasPrev());
- return pageRecordsMap;
- }
- private boolean isListNull(List<MessageVo> recordsList){
- boolean isNull=false;
- if(null==recordsList||recordsList.size()<=0){
- isNull=true;
- }
- return isNull;
- }
- public Integer getPageNo() {
- return pageNo;
- }
- public void setPageNo(Integer pageNo) {
- this.pageNo = pageNo;
- }
- public Integer getTotalPage() {
- return (getTotalCount()+pageSize-1)/pageSize;
- }
- public Integer getTotalCount() {
- return totalCount;
- }
- public Integer getPageSize() {
- return pageSize<=0?10:pageSize;
- }
- public Boolean getHasNext() {
- hasNext=pageNo<getTotalPage()?true:false;
- return hasNext;
- }
- public Boolean getHasPrev() {
- hasPrev=pageNo>1?true:false;
- return hasPrev;
- }
- }
- /**
- * ParseEmailTest
- * 对邮件按时间排序,即最新的邮件显示在最上面
- * 由于是通过API去访问而不是通过数据库的形式,所以这里的排序性能很差
- */
- package com.labci.javamail.utils;
- import java.util.Comparator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[12:05:48 PM]
- *
- */
- public class MessageComparator implements Comparator<Message> {
- public int compare(Message o1, Message o2) {
- boolean isBefore=false;
- try {
- isBefore = o1.getSentDate().before(o2.getSentDate());
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- int rntValue=0;
- if(isBefore){
- rntValue=1;
- }else{
- rntValue=-1;
- }
- return rntValue;
- }
- }
- /**
- * ParseEmailTest
- * 用BASE64算法解析指定字符串
- */
- package com.labci.javamail.utils;
- import java.io.IOException;
- import sun.misc.BASE64Decoder;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[1:36:22 PM]
- *
- */
- public class DecodeFrom {
- private DecodeFrom(){
- }
- public static String getDecondedString(String from){
- String fromStr="";
- try {
- fromStr=new String(new BASE64Decoder().decodeBuffer(from));
- } catch (IOException e) {
- e.printStackTrace();
- }
- return fromStr;
- }
- }
- /**
- * ParseEmailTest
- * 查询邮件列表的公用类
- * 也即简单单例模式的使用
- */
- package com.labci.javamail.utils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Store;
- import javax.mail.internet.InternetAddress;
- import com.labci.javamail.vo.MessageVo;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[12:14:37 PM]
- *
- */
- public class GetMessageList {
- private static GetMessageList instance=null;
- private GetMessageList(){
- }
- //延迟双重检查机制
- public static GetMessageList getInstance(){
- if(null == instance){
- synchronized(GetMessageList.class){
- if(null == instance){
- instance= new GetMessageList();
- }
- }
- }
- return instance;
- }
- @SuppressWarnings({ "unused", "static-access" })
- public Map<String,Object> getEmailsMap(Store store,int pageNo,int pageSize) throws MessagingException{
- Map<String,Object> rntMap=new HashMap<String,Object>();
- Folder receiveFolder=store.getFolder("inbox");//对于POP3协议此参数值只能是inbox
- receiveFolder.open(Folder.READ_WRITE);//设置收件夹打开后的读写权限
- int messageCount=receiveFolder.getMessageCount();
- if(messageCount<=0){
- return null;
- }
- Message []messages=receiveFolder.getMessages();
- List<Message> messagesList=Arrays.asList(messages);
- //对发送邮件时间按倒序排,这样最新的邮件显示在最上面
- Collections.sort(messagesList, new MessageComparator());
- List<MessageVo> messagesVoList=new ArrayList<MessageVo>();
- int i=0;
- for(Message msg:messagesList){
- InternetAddress[] iAddress=(InternetAddress[]) msg.getFrom();
- String from=iAddress[0].getAddress();
- if(null==from){
- from="";
- }
- String personal=iAddress[0].getPersonal();
- if(null==personal){
- personal="";
- }
- MessageVo mv=new MessageVo();
- mv.setMessageId(i++);
- mv.setMsg(msg);
- mv.setSubject(msg.getSubject());
- try {
- mv.setHasAttach(CommonUtils.getInstance().hasAttach(msg));
- } catch (IOException e) {
- e.printStackTrace();
- }
- mv.setPersonal(personal);
- if(!"".equals(personal)){
- from="<"+from+">";
- }
- mv.setFrom(from);
- mv.setUnRead(CommonUtils.getInstance().isNewMsg(msg));//POP3协议不支持是否是新邮件判断
- mv.setSentDate(DateUtils.getMsgSentDateString(msg.getSentDate()));
- messagesVoList.add(mv);
- }
- Map<String,Object> pageMessageMap=new MessagePageBean(messagesVoList).getPageRecordsMap(pageNo, pageSize);
- rntMap.put("msgMap", pageMessageMap);
- rntMap.put("msgVoList", messagesVoList);
- return rntMap;
- }
- }
- /**
- * ParseEmailTest
- * 处理邮件发送时间格式
- * 该类用到了一个开源jar包,名为date4j.jar
- */
- package com.labci.javamail.utils;
- import hirondelle.date4j.DateTime;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[9:00:31 PM]
- *
- */
- public class DateUtils {
- private DateUtils(){
- }
- public static String getMsgSentDateString(Date sentDate){
- String dateString="";
- DateFormat hhmmSDF=new SimpleDateFormat("HH:mm");
- DateFormat mmddSDF=new SimpleDateFormat("MM月dd日");
- DateFormat yyyySDF=new SimpleDateFormat("yyyy-MM-dd");
- DateFormat fullSDF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- DateTime sentDateTime=new DateTime(fullSDF.format(sentDate));
- DateTime todayDateTime=new DateTime(fullSDF.format(new Date()));
- //如果是在今天之内
- if(sentDateTime.isSameDayAs(todayDateTime)){
- dateString=hhmmSDF.format(sentDate);
- }else if(sentDateTime.numDaysFrom(todayDateTime)<=90){
- //如果在三个月内,就显示月和日
- dateString=mmddSDF.format(sentDate);
- }else{
- //否则就显示yyyy-MM-dd
- dateString=yyyySDF.format(sentDate);
- }
- return dateString;
- }
- public static String getSentDateFullString(Date sentDate){
- DateFormat fullSDF=new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
- return fullSDF.format(sentDate);
- }
- public static String getSentDateWeekString(Date sentDate){
- final String dayNames[] = { "星期日", "星期一", "星期二", "星期三",
- "星期四", "星期五","星期六" };
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(sentDate);
- int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
- if(dayOfWeek <0){
- dayOfWeek=0;
- }
- return dayNames[dayOfWeek];
- }
- }
- /**
- * ParseEmailTest
- * 一些其它操作的工具类
- *
- */
- package com.labci.javamail.utils;
- import java.io.IOException;
- import java.util.Properties;
- import javax.mail.Flags;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.servlet.http.HttpServletRequest;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[12:27:03 PM]
- *
- */
- public class CommonUtils {
- //此处的示例POP3服务器主机名写死为163的,不由用户输入
- private static final String HOST="pop3.163.com";
- private static final String PROTOCOL="pop3";
- private static CommonUtils instance=null;
- private CommonUtils(){
- }
- public static CommonUtils getInstance(){
- if(null == instance){
- synchronized(GetMessageList.class){
- if(null == instance){
- instance= new CommonUtils();
- }
- }
- }
- return instance;
- }
- public int getPageNo(HttpServletRequest request){
- int pageNo=1;
- if(null==request.getParameter("pageNo")
- ||"".equals(request.getParameter("pageNo").trim())){
- pageNo=1;
- }else{
- pageNo=Integer.parseInt(request.getParameter("pageNo"));
- }
- return pageNo;
- }
- //取得JavaMail的Session
- public Session getMailSession(){
- Properties mailProps=new Properties();
- mailProps.put("mail.store.protocol", PROTOCOL);
- mailProps.put("mail.pop3.host", HOST);
- Session session=Session.getDefaultInstance(mailProps);
- return session;
- }
- /**
- * 是否有附件
- * @param msg
- * @return
- * @throws MessagingException
- * @throws IOException
- */
- public boolean hasAttach(Message msg) throws MessagingException, IOException{
- boolean hasAttach=false;
- if(msg.getContentType().contains("multipart/mixed")){
- hasAttach=true;
- }
- return hasAttach;
- }
- public boolean isNewMsg(Message msg) throws MessagingException {
- boolean isNew = false;
- Flags flags = msg.getFlags();
- Flags.Flag[] flag = flags.getSystemFlags();
- for (int i = 0; i < flag.length; i++) {
- if (flag[i] == Flags.Flag.SEEN) {
- isNew = true;
- break;
- }
- }
- return isNew;
- }
- }
- /**
- * ParseEmailTest
- * 登录邮箱
- */
- package com.labci.javamail.action;
- import java.util.Map;
- import javax.mail.MessagingException;
- import javax.mail.Store;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import com.labci.javamail.utils.CommonUtils;
- import com.labci.javamail.utils.GetMessageList;
- import com.opensymphony.xwork2.Action;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[10:22:24 AM]
- *
- */
- public class LoginMailAction implements Action, ServletRequestAware {
- private HttpServletRequest request;
- //此处的示例POP3服务器主机名写死为163的,不由用户输入
- private static final String HOST="pop3.163.com";
- private static final int PAGESIZE=20;
- public String execute() throws Exception {
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- Store store=CommonUtils.getInstance().getMailSession().getStore();
- try{
- store.connect(HOST,username,password);
- request.getSession().setAttribute("store", store);
- }catch(MessagingException e){
- //登录失败
- request.setAttribute("loginFailure", "登录失败,请确认用户名或密码");
- return ERROR;
- }
- //登录成功后的操作
- displayEmails(store,CommonUtils.getInstance().getPageNo(request),PAGESIZE);
- return SUCCESS;//去邮件列表显示页
- }
- //登录成功后的操作
- @SuppressWarnings("unchecked")
- private void displayEmails(Store store,int pageNo,int pageSize) throws MessagingException{
- Map<String,Object> rntMap = GetMessageList.getInstance().getEmailsMap(store, pageNo, pageSize);
- Map<String,Object> mailsMap=(Map<String, Object>) rntMap.get("msgMap");
- if(null == mailsMap){
- request.setAttribute("notAvailableEmail", "您当前的邮箱中没有收到任何邮件");
- }else{
- request.setAttribute("recordsMap", mailsMap);
- }
- }
- public void setServletRequest(HttpServletRequest arg0) {
- this.request=arg0;
- }
- }
- /**
- * ParseEmailTest
- * 邮件列表翻页类
- */
- package com.labci.javamail.action;
- import java.util.Map;
- import javax.mail.MessagingException;
- import javax.mail.Store;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import com.labci.javamail.utils.CommonUtils;
- import com.labci.javamail.utils.GetMessageList;
- import com.opensymphony.xwork2.Action;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[12:25:20 PM]
- *
- */
- public class MailListAction implements Action, ServletRequestAware{
- private HttpServletRequest request;
- private static final int PAGESIZE=20;
- public String execute() throws Exception {
- int pageNo=CommonUtils.getInstance().getPageNo(request);
- Store store=null;
- if(null != request.getSession().getAttribute("store")){
- store=(Store) request.getSession().getAttribute("store");
- }
- displayEmails(store,pageNo,PAGESIZE);
- return SUCCESS;
- }
- //登录成功后的操作
- @SuppressWarnings("unchecked")
- private void displayEmails(Store store,int pageNo,int pageSize) throws MessagingException{
- Map<String,Object> rntMap = GetMessageList.getInstance().getEmailsMap(store, pageNo, pageSize);
- Map<String,Object> mailsMap=(Map<String, Object>) rntMap.get("msgMap");
- if(null == mailsMap){
- request.setAttribute("notAvailableEmail", "您当前的邮箱中没有收到任何邮件");
- }else{
- request.setAttribute("recordsMap", mailsMap);
- }
- }
- public void setServletRequest(HttpServletRequest arg0) {
- this.request=arg0;
- }
- }
- /**
- * ParseEmailTest
- * 查看邮件的类
- */
- package com.labci.javamail.action;
- import java.io.BufferedReader;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.mail.Address;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Part;
- import javax.mail.Store;
- import javax.mail.Message.RecipientType;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import com.labci.javamail.utils.CommonUtils;
- import com.labci.javamail.utils.DateUtils;
- import com.labci.javamail.utils.GetMessageList;
- import com.labci.javamail.vo.MessageVo;
- import com.opensymphony.xwork2.Action;
- /**
- * @author Bill Tu(tujiyue/iwtxokhtd)
- * May 28, 2011[12:25:20 PM]
- *
- */
- public class ViewMailAction implements Action, ServletRequestAware{
- private HttpServletRequest request;
- private static final int PAGESIZE=20;
- @SuppressWarnings("unchecked")
- public String execute() throws Exception {
- int pageNo=CommonUtils.getInstance().getPageNo(request);
- int messageId=Integer.parseInt(request.getParameter("messageId"));
- Store store=null;
- if(null != request.getSession().getAttribute("store")){
- store=(Store) request.getSession().getAttribute("store");
- }
- Map<String,Object> rntMap = GetMessageList.getInstance().getEmailsMap(store, pageNo, PAGESIZE);
- List<MessageVo> messageVoList=(List<MessageVo>) rntMap.get("msgVoList");
- MessageVo msgVo=null;
- if(!isMessageListNull(messageVoList)){
- msgVo=messageVoList.get(messageId);
- }
- Map<String,Object> msgMap=new HashMap<String,Object>();
- if(null!=msgVo){
- msgMap.put("from", msgVo.getFrom());
- msgMap.put("personal", msgVo.getPersonal());
- msgMap.put("messageId", msgVo.getMessageId());
- msgMap.put("subject", msgVo.getSubject());
- msgMap.put("sentDate", DateUtils.getSentDateFullString(msgVo.getMsg().getSentDate())
- +"("+DateUtils.getSentDateWeekString(msgVo.getMsg().getSentDate())+")");
- Address []toAddress=msgVo.getMsg().getRecipients(RecipientType.TO);
- if(null!=toAddress && toAddress.length>0){
- StringBuilder addressStr=new StringBuilder("");
- for(Address address:toAddress){
- addressStr.append(address.toString()).append(";");
- }
- addressStr.deleteCharAt(addressStr.length()-1);
- msgMap.put("receiver", addressStr.toString());
- }else{
- msgMap.put("receiver", "(无)");
- }
- if(!msgVo.getMsg().getContentType().trim().equals("multipart/mixed")){
- msgMap.put("content",getHtmlContent(msgVo));
- }else{
- //这里对附件的解析还没完全实现
- Multipart mp = (Multipart) msgVo.getMsg().getContent();
- for(int i=0;i<mp.getCount();i++){
- Part part=mp.getBodyPart(i);
- if(part.getContent() instanceof Multipart){//此条件可能不成立
- Multipart p=(Multipart) part.getContent();
- for(int j=0;j<p.getCount();j++){
- if(p.getBodyPart(j).getContentType().startsWith("text/plain")){
- msgMap.put("content",p.getBodyPart(j).getContent()+"");
- }else{
- msgMap.put("content",p.getBodyPart(j).getContent()+"");
- }
- }
- }
- if(null !=part.getDisposition()){//如果有附件
- InputStream is=part.getInputStream();
- FileOutputStream fos=new FileOutputStream(part.getFileName());
- int readData=0;
- while((readData=is.read())!=-1){
- fos.write(readData);
- }
- is.close();
- fos.close();
- }
- }
- }
- }
- request.setAttribute("msgMap", msgMap);
- return SUCCESS;
- }
- private String getHtmlContent(MessageVo msgVo) throws IOException, MessagingException{
- InputStream is=msgVo.getMsg().getDataHandler().getInputStream();
- BufferedReader br=new BufferedReader(new InputStreamReader(is));
- StringBuilder sb=new StringBuilder("");
- String input=null;
- while(null !=(input=br.readLine())){
- sb.append(input);
- }
- return sb.toString();
- }
- private boolean isMessageListNull(List<MessageVo> messageVoList){
- boolean isListNull=false;
- if(null ==messageVoList || messageVoList.size()<=0){
- isListNull=true;
- }
- return isListNull;
- }
- public void setServletRequest(HttpServletRequest arg0) {
- this.request=arg0;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>com.labci.javamail.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encode</param-name>
- <param-value>gbk</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <session-config>
- <session-timeout>1440</session-timeout>
- </session-config>
- </web-app>
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.action.extension" value="do" />
- <constant name="struts.devMode" value="false" />
- <package name="mail" extends="struts-default">
- <action name="login" class="com.labci.javamail.action.LoginMailAction">
- <result name="success">mailList.jsp</result>
- <result name="error">index.jsp</result>
- </action>
- <action name="mailList" class="com.labci.javamail.action.MailListAction">
- <result name="success">mailList.jsp</result>
- </action>
- <action name="viewMail" class="com.labci.javamail.action.ViewMailAction">
- <result name="success">viewMail.jsp</result>
- </action>
- </package>
- </struts>
- <%@ page language="java" pageEncoding="gbk"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>登录网易邮箱</title>
- </head>
- <body>
- <form action="login.do" method="post">
- 请使用163邮箱账号进行登录<br/>
- 用户名:<input type="text" name="username"/><br/>
- 密 码:<input type="password" name="password"/><br/>
- <input type="submit" value="登录"/>
- <input type="reset" value="重置"/>
- </form>
- </body>
- </html>
- <%@ page language="java" pageEncoding="gbk"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>收件箱</title>
- </head>
- <body>
- <table>
- <c:choose>
- <c:when test="${!empty notAvailableEmail}">
- <tr>
- <td>
- <c:out value="${notAvailableEmail}"/>
- </td>
- </tr>
- </c:when>
- <c:otherwise>
- <tr>
- <td>共有<b>${recordsMap['totalCount']}</b>封邮件,当前第<b>${recordsMap['currentPage']}</b>页,
- 每页显示<b>${recordsMap['pageSize']}</b>封,共有<b>${recordsMap['totalPage']}</b>页</td>
- </tr>
- <tr><td><hr/></td></tr>
- <tr>
- <td>
- <input type="checkbox" name="selectAll"/> <input type="button" name="deleteMsg" value="删除">
- </td>
- </tr>
- <tr><td colspan="4"><b><hr/></b></td></tr>
- <c:forEach items="${recordsMap['recordsList']}" var="mailVo">
- <tr>
- <td><input type="checkbox" name="messageId" value="${mailVo.messageId}"/><b>${mailVo.personal}</b>${mailVo.from}</td>
- <td>
- <c:choose>
- <c:when test="${mailVo.unRead}">
- <b><a href="viewMail.do?messageId=${mailVo.messageId}&pageNo=${recordsMap['currentPage']}" mce_href="viewMail.do?messageId=${mailVo.messageId}&pageNo=${recordsMap['currentPage']}">${mailVo.subject}</a></b>
- </c:when>
- <c:otherwise>
- <a href="viewMail.do?messageId=${mailVo.messageId}&pageNo=${recordsMap['currentPage']}" mce_href="viewMail.do?messageId=${mailVo.messageId}&pageNo=${recordsMap['currentPage']}">${mailVo.subject}</a>
- </c:otherwise>
- </c:choose>
- </td>
- <td>
- <c:if test="${mailVo.hasAttach}">
- <img src="images/attach.jpg" mce_src="images/attach.jpg" title="附件"/>
- </c:if>
- </td>
- <td>
- ${mailVo.sentDate}
- </td>
- </tr>
- </c:forEach>
- <tr>
- <td>
- <c:if test="${recordsMap['hasNext']}"><a href="mailList.do?pageNo=${recordsMap['currentPage']+1}" mce_href="mailList.do?pageNo=${recordsMap['currentPage']+1}">下一页</a></c:if>
- <c:if test="${recordsMap['hasPrev']}"><a href="mailList.do?pageNo=${recordsMap['currentPage']-1}" mce_href="mailList.do?pageNo=${recordsMap['currentPage']-1}">上一页</a></c:if>
- </td>
- </tr>
- </c:otherwise>
- </c:choose>
- </table>
- </body>
- </html>
- <%@ page language="java" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>查看邮件</title>
- </head>
- <body>
- <table>
- <tr>
- <td><b>${msgMap['subject']}</b></td>
- </tr>
- <tr>
- <td>发件人:<b>${msgMap['personal']}</b>${msgMap['from']}</td>
- </tr>
- <tr>
- <td>时间:${msgMap['sentDate']}</td>
- </tr>
- <tr>
- <td>收件人:${msgMap['receiver']}</td>
- </tr>
- <tr>
- <td>
- <hr/>
- </td>
- </tr>
- <tr>
- <td>${msgMap['content']}</td>
- </tr>
- <tr>
- <td>
- <c:if test="${!empty msgMap['attachNameList']}">
- <c:forEach items="${msgMap['attachNameList']}" var="attach">
- ${attach}
- </c:forEach>
- </c:if>
- </td>
- </tr>
- </table>
- </body>
- </html>