作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
本项目为基于SSM的邮件收发管理系统;
用户角色包含以下功能:
用户登录,写信给好友,查看收件箱,查看已发送的邮件,草稿箱查看,通讯录设置,个人资料管理等功能。
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.数据库:MySql 5.7版本;
6.是否Maven项目:否;
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+CSS+JavaScript+jQuery
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中config/db.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/xx 登录
- package com.email.controller.email;
-
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.multipart.MultipartFile;
-
- import com.email.po.Email;
- import com.email.po.EmailId;
- import com.email.po.Save;
- import com.email.po.User;
- import com.email.service.EmailService;
- import com.email.service.SaveService;
- import com.email.service.UserService;
-
- import sun.security.provider.MD5;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.sql.SQLException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.List;
- import java.util.UUID;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
-
- @Controller
- @RequestMapping("/email")
- public class EmailController {
-
- @Autowired
- private EmailService emailService;
-
- @Autowired
- private SaveService saveService;
-
- @Autowired
- private UserService userService;
-
- @RequestMapping(value="/upload/{type}",method=RequestMethod.POST)
- public String upLoad(Email email,Save save,String userid,HttpServletRequest request,HttpSession session,@PathVariable Integer type,
- @RequestParam(value="file") MultipartFile pic) {
- User u = (User)session.getAttribute("user");
- if(u==null)
- return "login";
- //检查收件人地址是否为空
- if(email.getAddresserId()==null||email.getAddresserId().trim().equals("")) {
- request.setAttribute("error", "请输入收件人邮箱");
- return "send";
- }
- //检查收件人地址是否存在
- boolean flag = userService.checkItExist(userid);
- if(flag==true) {
- request.setAttribute("error", "该收件人不存在!");
- return "send";
- }
- if(email.getTitle()==null||email.getTitle().trim().equals("")) {
- request.setAttribute("error", "主题填写不完整!");
- return "send";
- }
- String originalFileName;
- String path;
- if(pic==null||pic.getSize()==0) {
- originalFileName = "无附件";
- path = "";
- }else {
- //获取文件的原始名称
- originalFileName = pic.getOriginalFilename();
- //上传文件,返回的path是存放在服务器中的文件路径
- path = upLoadFile(pic);
- //所有检验完毕后把email信息填写完整之后添加到数据库
- }
- if(type==0) {
- Date date=new Date();
- email.setAddresseeId(u.getUserid());
- email.setTime(date);
- email.setFilename(originalFileName);
- email.setFilepath(path);
- //获取ID
- int id = emailService.getTheLastId();
- email.setId(id);
- email.setReaded(0);
- emailService.addMail(email);
- }else {
- Date date=new Date();
- save.setAddresseeId(u.getUserid());
- save.setTime(date);
- save.setFilename(originalFileName);
- save.setFilepath(path);
- //获取ID
- int id = saveService.getTheLastId();
- save.setId(id);
- save.setReaded(0);
- saveService.addSave(save);
- }
- request.setAttribute("success", "操作成功!");
- request.setAttribute("email", null);
- request.setAttribute("save", null);
- return "send";
- }
-
- //附件上传方法
- public String upLoadFile(MultipartFile file){
- String path = null;
- //首先获取文件原始名称,目的在于获取上传文件的后缀名
- String originalName = file.getOriginalFilename();
- //获取文件后缀名,要求是png、jpg、gif、jpeg
- String suffix = originalName.substring(originalName.lastIndexOf('.'));
- if(suffix.equalsIgnoreCase(".bat")||suffix.equalsIgnoreCase(".exe")){
- return null;
- }
- //创建新的文件名
- String newFileName = UUID.randomUUID()+originalName.substring(originalName.lastIndexOf('.'));
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- File f;
- //设置路径
- //创建文件夹路径,创建的文件夹路径有级数限制。注意
- path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)+"/file/"+sdf.format(new Date())+"\\";
- File pat = new File(path);
- pat.mkdirs();
- path = path+newFileName;
- f = new File(path);
- try {
- file.transferTo(f);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "/file/"+sdf.format(new Date())+"/"+newFileName;
- }
-
- /*
- * 该方法用来查看我的收件箱
- *
- * */
- @RequestMapping("/myReceive/{userid}")
- public String myReceive(HttpSession session,HttpServletRequest request,@PathVariable String userid) {
- //检查用户是否已经注销,或者浏览器已清除用户缓存,如果清除,则需要重新登录
- User u = (User)session.getAttribute("user");
- if(u==null)
- return "login";
- System.out.println(userid);
- //获取指定收件人的所有邮件
- List
list = emailService.selectByErId(userid+".com"); - List
finalList = new ArrayList(); - for (Email email : list) {
- if(email.getReaded()!=2) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { - public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
-
- });
- request.setAttribute("emails", finalList);
- return "receiveEmail";
- }
-
- /*
- * 该方法完成邮件查看的功能
- *
- * */
- @RequestMapping(value="/look/{id}",method=RequestMethod.GET)
- public String look(@PathVariable int id,HttpSession session,HttpServletRequest request) {
- //检查用户是否已经注销,或者浏览器已清除用户缓存,如果清除,则需要重新登录
- User u = (User)session.getAttribute("user");
- if(u==null)
- return "login";
-
- Email email = emailService.getEmailById(id);
- request.setAttribute("email", email);
-
- return "email_info";
- }
-
- /*
- * 完成下载附件的功能。也就是当用户查看邮件时可以点击下载
- *
- * */
- @RequestMapping(value="/downloadFile/{id}",method=RequestMethod.GET)
- public void downloadFile(HttpServletRequest request,HttpServletResponse response,@PathVariable int id) throws IOException {
- Email email=emailService.getEmailById(id);
- File file = new File(Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)
- +"/"+email.getFilepath());
- System.out.println(file.getAbsolutePath());
- //如果文件不存在
- if(!file.exists()){
- response.setCharacterEncoding("UTF-8");
- //则向前台报告文件已被删除
- response.getWriter().println("");
- return;
- }
- //处理文件名,用真实的文件名导出
- String realname=email.getFilename();
- //设置响应头,控制浏览器下载该文件
- response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
- //读取要下载的文件,保存到文件输入流
- FileInputStream in = new FileInputStream(file);
- //创建输出流
- OutputStream out = response.getOutputStream();
- //创建缓冲区
- byte buffer[] = new byte[1024];
- int len = 0;
- //循环将输入流中的内容读取到缓冲区当中
- while((len=in.read(buffer))>0){
- //输出缓冲区的内容到浏览器,实现文件下载
- out.write(buffer, 0, len);
- }
-
- }
-
- //按条件查询满足条件的邮件
- /*
- * 1.发件人
- * 2.标题
- * 3.未读、已读
- *
- * */
- @RequestMapping("/select/{userid}")
- public String select(Email email,HttpServletRequest request,@PathVariable String userid) {
- List
list = emailService.selectByOptions(email,(userid+".com")); - request.setAttribute("emails", list);
- return "receiveEmail";
- }
-
- //查看自己的发件箱,userid是自己的邮箱账号
- @RequestMapping("/send/{userid}")
- public String send(HttpServletRequest request,@PathVariable String userid) {
- //获取自己以往所有发送过的邮件,查询条件是根据自己的邮箱账号。
- List
list = emailService.selectByEeId(userid+".com"); - for (Email email : list) {
- System.out.println(email.getTitle());
- }
- List
finalList = new ArrayList(); - for (Email email : list) {
- if(email.getReaded()!=3&&email.getReaded()!=4) {
- finalList.add(email);
- }
- }
- request.setAttribute("emails", finalList);
- return "sendEmails";
- }
-
- /*
- * 该方法用来删除指定的(收件)邮件,
- * 这里有要注意的地方,因为邮件是一份的,且是发件人和收件人共享的,如果直接执行删除数据库中的操作,那么势必会影响另一方,
- * 这里的处理方法是利用邮件的readed字段来进行处理,readed字段值为0时表示未读,1表示已读,2表示收件人删除,3表示发件人
- * 删除且邮件未读,4表示发件人删除且邮件已读
- * */
- @RequestMapping("reDelete/{id}")
- public String reDelete(HttpServletRequest request,@PathVariable String id) {
- //调用service里的delete方法。
- emailService.reDelete(Integer.parseInt(id));
- //获取指定收件人的所有邮件
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
list = emailService.selectByErId(userid); - List
finalList = new ArrayList(); - for (Email email : list) {
- if(email.getReaded()!=2) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { - public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
- });
- request.setAttribute("emails", finalList);
- return "receiveEmail";
- }
-
- /*
- * 该方法用来删除指定的(发件)邮件
- * */
- @RequestMapping("seDelete/{id}")
- public String seDelete(HttpServletRequest request,@PathVariable int id) {
- //调用service里的delete方法。
- emailService.sedelete(id);
- //获取指定发件人的所有邮件
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
list = emailService.selectByEeId(userid); - List
finalList = new ArrayList(); - for (Email email : list) {
- if(email.getReaded()!=3&&email.getReaded()!=4) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { -
- public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
-
- });
- request.setAttribute("emails", finalList);
- return "sendEmails";
- }
-
- //设置邮件为已读
- @RequestMapping("/setReaded/{id}")
- public String setReaded(HttpServletRequest request,@PathVariable int id) {
- emailService.setEmailReaded(id);
- //获取指定收件人的所有邮件
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
list = emailService.selectByErId(userid); - List
finalList = new ArrayList(); - for (Email email : list) {
- if(email.getReaded()!=2) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { - public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
-
- });
- request.setAttribute("emails", finalList);
- return "receiveEmail";
- }
-
- //批量删除收件箱
- @RequestMapping("/reListdel")
- public String ids(HttpServletRequest request,EmailId ids) {
- List
list = ids.getId(); - emailService.listDelete(list);
- //获取指定收件人的所有邮件
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
alllist = emailService.selectByErId(userid); - List
finalList = new ArrayList(); - for (Email email : alllist) {
- if(email.getReaded()!=2) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { - public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
- });
- request.setAttribute("emails", finalList);
- return "receiveEmail";
- }
- //批量删除发件箱
- @RequestMapping("/seListdel")
- public String seListdel(HttpServletRequest request,EmailId ids) {
- List
list = ids.getId(); - emailService.seListDelete(list);
- //获取指定发件人的所有邮件
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
alllist = emailService.selectByEeId(userid); - List
finalList = new ArrayList(); - for (Email email : alllist) {
- if(email.getReaded()!=3&&email.getReaded()!=4) {
- finalList.add(email);
- }
- }
- //按已读和未读来排序,未读的优先显示
- finalList.sort(new Comparator
() { - public int compare(Email o1, Email o2) {
- return o1.getReaded()-o2.getReaded();
- }
- });
- request.setAttribute("emails", finalList);
- return "sendEmails";
- }
- //转发路由
- @RequestMapping("/transmit/{id}")
- public String transmit(HttpServletRequest request,@PathVariable int id) {
- request.setAttribute("id", id);
- return "transmit";
- }
- //转发邮件
- @RequestMapping("/toTrans")
- public String toTrans(HttpServletRequest request,int id,String userid) {
- Email email = emailService.getEmailById(id);
- email.setAddresserId(userid);
- int lastId = emailService.getTheLastId();
- email.setId(lastId);
- email.setReaded(0);
- emailService.addMail(email);
- request.setAttribute("msg", "转发成功!");
- return "transmit";
- }
- }
- package com.email.controller.friend;
-
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
-
- import org.apache.poi.hslf.dev.SlideAndNotesAtomListing;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import com.email.po.Email;
- import com.email.po.Friend;
- import com.email.po.User;
- import com.email.service.FriendService;
- import com.email.service.UserService;
-
- @Controller
- @RequestMapping("/friend")
- public class FriendController {
-
- @Autowired
- private FriendService friendService;
-
- @Autowired
- private UserService userService;
-
- //查看我的通讯录
- @RequestMapping("/myfriend/{userid}")
- public String myfriend(HttpServletRequest request,@PathVariable String userid) {
- List
list = friendService.getAllFriends(userid+".com"); - request.setAttribute("friends", list);
- return "friend";
- }
-
- //添加朋友
- @RequestMapping("/add")
- public String add(HttpServletRequest request,HttpSession session,String friendId) {
- User user = (User)session.getAttribute("user");
- List
lists = friendService.getAllFriends(user.getUserid()); - for (Friend friend : lists) {
- if(friend.getFriendId().equals(friendId)) {
- request.setAttribute("error", "该用户已经存在通信录中!请勿重新添加");
- List
list = friendService.getAllFriends(user.getUserid()); - request.setAttribute("friends", list);
- return "friend";
- }
- }
- Friend friend = new Friend();
- friend.setUserid(user.getUserid());
- friend.setFriendId(friendId);
- User fri = userService.getUserByUserid(friendId);
- if(fri==null) {
- List
list = friendService.getAllFriends(user.getUserid()); - request.setAttribute("error", "您要添加的用户不存在!");
- request.setAttribute("friends", list);
- return "friend";
- }
- friend.setFriendName(fri.getUsername());
- friendService.add(friend);
- List
list = friendService.getAllFriends(user.getUserid()); - request.setAttribute("success", "添加成功!");
- request.setAttribute("friends", list);
- return "friend";
- }
-
- //删除通讯录好友
- @RequestMapping("/delete/{friendId}")
- public String delete(HttpServletRequest request,HttpSession session,@PathVariable String friendId) {
- User user = (User)session.getAttribute("user");
- friendService.delete(friendId+".com");
- List
list = friendService.getAllFriends(user.getUserid()); - request.setAttribute("friends", list);
- return "friend";
- }
-
- //给朋友写信,带着朋友信息跳转到写信页面
- @RequestMapping("/write/{friendId}")
- public String write(HttpServletRequest request,@PathVariable String friendId) {
- Email email = new Email();
- email.setAddresserId(friendId+".com");
- request.setAttribute("save", email);
- return "send";
- }
- }
- package com.email.controller.save;
-
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import com.email.mapper.SaveMapper;
- import com.email.po.Save;
- import com.email.po.User;
- import com.email.service.SaveService;
-
- @Controller
- @RequestMapping("/save")
- public class SaveController {
-
- @Autowired
- private SaveService saveService;
- @Autowired
- private SaveMapper saveMapper;
-
- //查看我的草稿箱
- @RequestMapping("/mySave/{userid}")
- public String mySave(HttpServletRequest request,@PathVariable String userid) {
- List
list = saveService.selectByEE(userid+".com"); - request.setAttribute("saves", list);
- return "save";
- }
-
- //查看草稿箱具体邮件内容
- @RequestMapping("/look/{id}")
- public String look(HttpServletRequest request,@PathVariable int id) {
- Save save = saveService.selectBySaveId(id);
- request.setAttribute("save", save);
- return "send";
- }
-
-
-
- //查看草稿箱具体邮件内容
- @RequestMapping("/reDelete/{id}")
- public String reDelete(HttpServletRequest request,@PathVariable int id) {
- saveMapper.deleteByPrimaryKey(id);
- User currentUser = (User)request.getSession().getAttribute("user");
- String userid = currentUser.getUserid();
- List
list = saveService.selectByEE(userid); - request.setAttribute("saves", list);
- return "save";
- }
- }
如果也想学习本系统,下面领取。关注并回复:162ssm