此次示例有点仿照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>
相关推荐
JavaMail 是一个开源的 Java 库,用于在 Java 应用程序中实现电子邮件的发送和接收。这个库封装了多种邮件通信协议,如 IMAP、POP3 和 SMTP,简化了与邮件服务器的交互过程。在设计基于 JavaMail 的电子邮件系统时,...
javaMail示例javaMail示例javaMail示例javaMail示例javaMail示例javaMail示例,我自己做的,保证运行成功,里面jar包都有,只需导入即可,把邮箱设置一下就可以了。
《基于Java Mail的电子邮件收发系统》是一篇深入探讨如何使用JavaMail API构建电子邮件系统的研究型学位论文。本文将详细阐述该系统的设计理念、技术选型、需求分析以及实现细节。 1. 研究目的和意义: 研究的目的...
JavaMail 是一个强大的开源库,用于在 Java 应用程序中发送和接收电子邮件。这个工具类集合涵盖了多种邮件服务的发送功能,使开发者能够轻松处理复杂的邮件操作。在这个"javamail-完全封装-各种邮件都可以发-...
在JavaMail电子邮件系统中,Tomcat是运行jsp和JavaMail相关服务的平台。开发者需要将编译后的JavaMail系统部署到Tomcat服务器上,以便通过Web访问和使用电子邮件功能。 在使用JavaMail API时,会涉及到两个关键的...
javamail-1_4_5则是JavaMail的特定版本,JavaMail是一个开源的Java库,用于处理电子邮件。它提供了丰富的API,可以实现发送、接收、读取和管理电子邮件。JavaMail不仅支持SMTP、POP3和IMAP协议,还支持安全的SSL/TLS...
在Web开发中,发送电子邮件是一项常见的任务,这通常涉及到与邮件服务器进行交互并构建符合SMTP协议的邮件消息。Apache Commons Email和JavaMail是两个广泛使用的Java库,专门用于处理电子邮件的发送。这两个库分别...
【标题】:基于JavaMail的电子邮件收发系统设计说明 【描述】:本文档详细介绍了如何使用JavaMail技术设计和实现一个电子邮件收发系统,涵盖了系统设计的目标、国内外发展趋势、技术选型、需求分析、详细设计以及功能...
标题中的三个压缩包文件分别是"jaf-1_1-fr.zip"、"javamail-1_4.zip"和"xml-security-bin-1_2_1.zip",这些文件是针对Java应用程序,尤其是Web服务环境如Tomcat或JBoss部署Axis2时所需的组件。现在,我们将深入探讨...
JavaMail 是一个强大的开源库,专门用于在Java应用程序中实现电子邮件的发送和接收。它提供了对各种邮件协议的支持,如 Simple Mail Transfer Protocol (SMTP)、Post Office Protocol (POP3) 和 Internet Message ...
JavaMail 是一个强大的开源库,专门用于在Java应用程序中实现电子邮件的发送和接收。它支持多种邮件协议,如SMTP(简单邮件传输协议)用于发送邮件,IMAP(因特网消息访问协议)和POP3(邮局协议)用于接收邮件。在...
JavaMail API是Sun Microsystems提供的一套用于发送、接收和处理电子邮件的标准接口集合。它建立在JavaBeans的基础上,提供了丰富的特性来支持各种邮件协议(如SMTP、POP3、IMAP等)。JavaMail不仅支持文本邮件,还...
基于JavaMail的电子邮件系统设计与实现,主要涉及以下几个关键知识点: 1. **JavaMail API**: JavaMail API是Java平台上的一个开源库,它提供了用于处理邮件消息的接口和类。开发者可以通过它来创建、读取、发送和...
很好的邮件处理代码,1.5版本,作为build path放在eclipse里面,就可以用啦! oracle官方推的,很好!jdk1.6版本以上用起来很方便。
这个示例将详细介绍如何在Java应用程序中使用JavaMail发送邮件。 首先,我们需要引入JavaMail相关的依赖。如果你使用的是Maven项目,可以在pom.xml文件中添加以下依赖: ```xml <groupId>com.sun.mail</groupId> ...
### JAVA Web程序设计-3期(KC006) 13-1-2应用JavaMail组件发送邮件的操作步骤 #### 知识点概述 在本篇文档中,我们将深入探讨如何利用JavaMail组件实现电子邮件的发送功能。通过具体的学习步骤和实践案例,帮助...
JavaMail 是一个强大的开源库,用于在 Java 应用程序中处理电子邮件的发送与接收。在本文中,我们将深入探讨如何使用 JavaMail 收取邮件,包括了解 POP3 和 IMAP 协议以及如何通过 JavaMail API 实现邮件的接收。 ...
JavaMail 是一个开源库,它提供了在Java应用程序中发送和接收电子邮件的能力。这个"基于Javamail的邮件收发系统_project"显然是一款利用JavaMail API实现的邮件系统,可以帮助用户方便地处理电子邮件的发送、接收和...
【基于JMS的WEB邮件客户端系统(JAVAMAIL版)】是一种使用JavaMail API和Java消息服务(JMS)实现的Web应用程序,它允许用户通过Web界面发送、接收和管理电子邮件。这个系统的核心特点在于利用了JMS进行异步通信,提高...