• Java项目:SSM邮件收发管理系统


    作者主页:夜未央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 登录

    运行截图

     

     

     

     

     

     

     

    相关代码

    Email控制器

    1. package com.email.controller.email;
    2. import org.springframework.web.bind.annotation.PathVariable;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. import org.springframework.web.bind.annotation.RequestParam;
    6. import org.springframework.web.multipart.MultipartFile;
    7. import com.email.po.Email;
    8. import com.email.po.EmailId;
    9. import com.email.po.Save;
    10. import com.email.po.User;
    11. import com.email.service.EmailService;
    12. import com.email.service.SaveService;
    13. import com.email.service.UserService;
    14. import sun.security.provider.MD5;
    15. import java.io.File;
    16. import java.io.FileInputStream;
    17. import java.io.IOException;
    18. import java.io.OutputStream;
    19. import java.io.UnsupportedEncodingException;
    20. import java.net.URLEncoder;
    21. import java.sql.SQLException;
    22. import java.text.DateFormat;
    23. import java.text.SimpleDateFormat;
    24. import java.util.ArrayList;
    25. import java.util.Comparator;
    26. import java.util.Date;
    27. import java.util.List;
    28. import java.util.UUID;
    29. import javax.servlet.http.HttpServletRequest;
    30. import javax.servlet.http.HttpServletResponse;
    31. import javax.servlet.http.HttpSession;
    32. import org.springframework.beans.factory.annotation.Autowired;
    33. import org.springframework.stereotype.Controller;
    34. @Controller
    35. @RequestMapping("/email")
    36. public class EmailController {
    37. @Autowired
    38. private EmailService emailService;
    39. @Autowired
    40. private SaveService saveService;
    41. @Autowired
    42. private UserService userService;
    43. @RequestMapping(value="/upload/{type}",method=RequestMethod.POST)
    44. public String upLoad(Email email,Save save,String userid,HttpServletRequest request,HttpSession session,@PathVariable Integer type,
    45. @RequestParam(value="file") MultipartFile pic) {
    46. User u = (User)session.getAttribute("user");
    47. if(u==null)
    48. return "login";
    49. //检查收件人地址是否为空
    50. if(email.getAddresserId()==null||email.getAddresserId().trim().equals("")) {
    51. request.setAttribute("error", "请输入收件人邮箱");
    52. return "send";
    53. }
    54. //检查收件人地址是否存在
    55. boolean flag = userService.checkItExist(userid);
    56. if(flag==true) {
    57. request.setAttribute("error", "该收件人不存在!");
    58. return "send";
    59. }
    60. if(email.getTitle()==null||email.getTitle().trim().equals("")) {
    61. request.setAttribute("error", "主题填写不完整!");
    62. return "send";
    63. }
    64. String originalFileName;
    65. String path;
    66. if(pic==null||pic.getSize()==0) {
    67. originalFileName = "无附件";
    68. path = "";
    69. }else {
    70. //获取文件的原始名称
    71. originalFileName = pic.getOriginalFilename();
    72. //上传文件,返回的path是存放在服务器中的文件路径
    73. path = upLoadFile(pic);
    74. //所有检验完毕后把email信息填写完整之后添加到数据库
    75. }
    76. if(type==0) {
    77. Date date=new Date();
    78. email.setAddresseeId(u.getUserid());
    79. email.setTime(date);
    80. email.setFilename(originalFileName);
    81. email.setFilepath(path);
    82. //获取ID
    83. int id = emailService.getTheLastId();
    84. email.setId(id);
    85. email.setReaded(0);
    86. emailService.addMail(email);
    87. }else {
    88. Date date=new Date();
    89. save.setAddresseeId(u.getUserid());
    90. save.setTime(date);
    91. save.setFilename(originalFileName);
    92. save.setFilepath(path);
    93. //获取ID
    94. int id = saveService.getTheLastId();
    95. save.setId(id);
    96. save.setReaded(0);
    97. saveService.addSave(save);
    98. }
    99. request.setAttribute("success", "操作成功!");
    100. request.setAttribute("email", null);
    101. request.setAttribute("save", null);
    102. return "send";
    103. }
    104. //附件上传方法
    105. public String upLoadFile(MultipartFile file){
    106. String path = null;
    107. //首先获取文件原始名称,目的在于获取上传文件的后缀名
    108. String originalName = file.getOriginalFilename();
    109. //获取文件后缀名,要求是png、jpg、gif、jpeg
    110. String suffix = originalName.substring(originalName.lastIndexOf('.'));
    111. if(suffix.equalsIgnoreCase(".bat")||suffix.equalsIgnoreCase(".exe")){
    112. return null;
    113. }
    114. //创建新的文件名
    115. String newFileName = UUID.randomUUID()+originalName.substring(originalName.lastIndexOf('.'));
    116. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    117. File f;
    118. //设置路径
    119. //创建文件夹路径,创建的文件夹路径有级数限制。注意
    120. path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)+"/file/"+sdf.format(new Date())+"\\";
    121. File pat = new File(path);
    122. pat.mkdirs();
    123. path = path+newFileName;
    124. f = new File(path);
    125. try {
    126. file.transferTo(f);
    127. } catch (Exception e) {
    128. e.printStackTrace();
    129. }
    130. return "/file/"+sdf.format(new Date())+"/"+newFileName;
    131. }
    132. /*
    133. * 该方法用来查看我的收件箱
    134. *
    135. * */
    136. @RequestMapping("/myReceive/{userid}")
    137. public String myReceive(HttpSession session,HttpServletRequest request,@PathVariable String userid) {
    138. //检查用户是否已经注销,或者浏览器已清除用户缓存,如果清除,则需要重新登录
    139. User u = (User)session.getAttribute("user");
    140. if(u==null)
    141. return "login";
    142. System.out.println(userid);
    143. //获取指定收件人的所有邮件
    144. List list = emailService.selectByErId(userid+".com");
    145. List finalList = new ArrayList();
    146. for (Email email : list) {
    147. if(email.getReaded()!=2) {
    148. finalList.add(email);
    149. }
    150. }
    151. //按已读和未读来排序,未读的优先显示
    152. finalList.sort(new Comparator() {
    153. public int compare(Email o1, Email o2) {
    154. return o1.getReaded()-o2.getReaded();
    155. }
    156. });
    157. request.setAttribute("emails", finalList);
    158. return "receiveEmail";
    159. }
    160. /*
    161. * 该方法完成邮件查看的功能
    162. *
    163. * */
    164. @RequestMapping(value="/look/{id}",method=RequestMethod.GET)
    165. public String look(@PathVariable int id,HttpSession session,HttpServletRequest request) {
    166. //检查用户是否已经注销,或者浏览器已清除用户缓存,如果清除,则需要重新登录
    167. User u = (User)session.getAttribute("user");
    168. if(u==null)
    169. return "login";
    170. Email email = emailService.getEmailById(id);
    171. request.setAttribute("email", email);
    172. return "email_info";
    173. }
    174. /*
    175. * 完成下载附件的功能。也就是当用户查看邮件时可以点击下载
    176. *
    177. * */
    178. @RequestMapping(value="/downloadFile/{id}",method=RequestMethod.GET)
    179. public void downloadFile(HttpServletRequest request,HttpServletResponse response,@PathVariable int id) throws IOException {
    180. Email email=emailService.getEmailById(id);
    181. File file = new File(Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)
    182. +"/"+email.getFilepath());
    183. System.out.println(file.getAbsolutePath());
    184. //如果文件不存在
    185. if(!file.exists()){
    186. response.setCharacterEncoding("UTF-8");
    187. //则向前台报告文件已被删除
    188. response.getWriter().println("");
    189. return;
    190. }
    191. //处理文件名,用真实的文件名导出
    192. String realname=email.getFilename();
    193. //设置响应头,控制浏览器下载该文件
    194. response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
    195. //读取要下载的文件,保存到文件输入流
    196. FileInputStream in = new FileInputStream(file);
    197. //创建输出流
    198. OutputStream out = response.getOutputStream();
    199. //创建缓冲区
    200. byte buffer[] = new byte[1024];
    201. int len = 0;
    202. //循环将输入流中的内容读取到缓冲区当中
    203. while((len=in.read(buffer))>0){
    204. //输出缓冲区的内容到浏览器,实现文件下载
    205. out.write(buffer, 0, len);
    206. }
    207. }
    208. //按条件查询满足条件的邮件
    209. /*
    210. * 1.发件人
    211. * 2.标题
    212. * 3.未读、已读
    213. *
    214. * */
    215. @RequestMapping("/select/{userid}")
    216. public String select(Email email,HttpServletRequest request,@PathVariable String userid) {
    217. List list = emailService.selectByOptions(email,(userid+".com"));
    218. request.setAttribute("emails", list);
    219. return "receiveEmail";
    220. }
    221. //查看自己的发件箱,userid是自己的邮箱账号
    222. @RequestMapping("/send/{userid}")
    223. public String send(HttpServletRequest request,@PathVariable String userid) {
    224. //获取自己以往所有发送过的邮件,查询条件是根据自己的邮箱账号。
    225. List list = emailService.selectByEeId(userid+".com");
    226. for (Email email : list) {
    227. System.out.println(email.getTitle());
    228. }
    229. List finalList = new ArrayList();
    230. for (Email email : list) {
    231. if(email.getReaded()!=3&&email.getReaded()!=4) {
    232. finalList.add(email);
    233. }
    234. }
    235. request.setAttribute("emails", finalList);
    236. return "sendEmails";
    237. }
    238. /*
    239. * 该方法用来删除指定的(收件)邮件,
    240. * 这里有要注意的地方,因为邮件是一份的,且是发件人和收件人共享的,如果直接执行删除数据库中的操作,那么势必会影响另一方,
    241. * 这里的处理方法是利用邮件的readed字段来进行处理,readed字段值为0时表示未读,1表示已读,2表示收件人删除,3表示发件人
    242. * 删除且邮件未读,4表示发件人删除且邮件已读
    243. * */
    244. @RequestMapping("reDelete/{id}")
    245. public String reDelete(HttpServletRequest request,@PathVariable String id) {
    246. //调用service里的delete方法。
    247. emailService.reDelete(Integer.parseInt(id));
    248. //获取指定收件人的所有邮件
    249. User currentUser = (User)request.getSession().getAttribute("user");
    250. String userid = currentUser.getUserid();
    251. List list = emailService.selectByErId(userid);
    252. List finalList = new ArrayList();
    253. for (Email email : list) {
    254. if(email.getReaded()!=2) {
    255. finalList.add(email);
    256. }
    257. }
    258. //按已读和未读来排序,未读的优先显示
    259. finalList.sort(new Comparator() {
    260. public int compare(Email o1, Email o2) {
    261. return o1.getReaded()-o2.getReaded();
    262. }
    263. });
    264. request.setAttribute("emails", finalList);
    265. return "receiveEmail";
    266. }
    267. /*
    268. * 该方法用来删除指定的(发件)邮件
    269. * */
    270. @RequestMapping("seDelete/{id}")
    271. public String seDelete(HttpServletRequest request,@PathVariable int id) {
    272. //调用service里的delete方法。
    273. emailService.sedelete(id);
    274. //获取指定发件人的所有邮件
    275. User currentUser = (User)request.getSession().getAttribute("user");
    276. String userid = currentUser.getUserid();
    277. List list = emailService.selectByEeId(userid);
    278. List finalList = new ArrayList();
    279. for (Email email : list) {
    280. if(email.getReaded()!=3&&email.getReaded()!=4) {
    281. finalList.add(email);
    282. }
    283. }
    284. //按已读和未读来排序,未读的优先显示
    285. finalList.sort(new Comparator() {
    286. public int compare(Email o1, Email o2) {
    287. return o1.getReaded()-o2.getReaded();
    288. }
    289. });
    290. request.setAttribute("emails", finalList);
    291. return "sendEmails";
    292. }
    293. //设置邮件为已读
    294. @RequestMapping("/setReaded/{id}")
    295. public String setReaded(HttpServletRequest request,@PathVariable int id) {
    296. emailService.setEmailReaded(id);
    297. //获取指定收件人的所有邮件
    298. User currentUser = (User)request.getSession().getAttribute("user");
    299. String userid = currentUser.getUserid();
    300. List list = emailService.selectByErId(userid);
    301. List finalList = new ArrayList();
    302. for (Email email : list) {
    303. if(email.getReaded()!=2) {
    304. finalList.add(email);
    305. }
    306. }
    307. //按已读和未读来排序,未读的优先显示
    308. finalList.sort(new Comparator() {
    309. public int compare(Email o1, Email o2) {
    310. return o1.getReaded()-o2.getReaded();
    311. }
    312. });
    313. request.setAttribute("emails", finalList);
    314. return "receiveEmail";
    315. }
    316. //批量删除收件箱
    317. @RequestMapping("/reListdel")
    318. public String ids(HttpServletRequest request,EmailId ids) {
    319. List list = ids.getId();
    320. emailService.listDelete(list);
    321. //获取指定收件人的所有邮件
    322. User currentUser = (User)request.getSession().getAttribute("user");
    323. String userid = currentUser.getUserid();
    324. List alllist = emailService.selectByErId(userid);
    325. List finalList = new ArrayList();
    326. for (Email email : alllist) {
    327. if(email.getReaded()!=2) {
    328. finalList.add(email);
    329. }
    330. }
    331. //按已读和未读来排序,未读的优先显示
    332. finalList.sort(new Comparator() {
    333. public int compare(Email o1, Email o2) {
    334. return o1.getReaded()-o2.getReaded();
    335. }
    336. });
    337. request.setAttribute("emails", finalList);
    338. return "receiveEmail";
    339. }
    340. //批量删除发件箱
    341. @RequestMapping("/seListdel")
    342. public String seListdel(HttpServletRequest request,EmailId ids) {
    343. List list = ids.getId();
    344. emailService.seListDelete(list);
    345. //获取指定发件人的所有邮件
    346. User currentUser = (User)request.getSession().getAttribute("user");
    347. String userid = currentUser.getUserid();
    348. List alllist = emailService.selectByEeId(userid);
    349. List finalList = new ArrayList();
    350. for (Email email : alllist) {
    351. if(email.getReaded()!=3&&email.getReaded()!=4) {
    352. finalList.add(email);
    353. }
    354. }
    355. //按已读和未读来排序,未读的优先显示
    356. finalList.sort(new Comparator() {
    357. public int compare(Email o1, Email o2) {
    358. return o1.getReaded()-o2.getReaded();
    359. }
    360. });
    361. request.setAttribute("emails", finalList);
    362. return "sendEmails";
    363. }
    364. //转发路由
    365. @RequestMapping("/transmit/{id}")
    366. public String transmit(HttpServletRequest request,@PathVariable int id) {
    367. request.setAttribute("id", id);
    368. return "transmit";
    369. }
    370. //转发邮件
    371. @RequestMapping("/toTrans")
    372. public String toTrans(HttpServletRequest request,int id,String userid) {
    373. Email email = emailService.getEmailById(id);
    374. email.setAddresserId(userid);
    375. int lastId = emailService.getTheLastId();
    376. email.setId(lastId);
    377. email.setReaded(0);
    378. emailService.addMail(email);
    379. request.setAttribute("msg", "转发成功!");
    380. return "transmit";
    381. }
    382. }

    FriendController

    1. package com.email.controller.friend;
    2. import java.util.List;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpSession;
    5. import org.apache.poi.hslf.dev.SlideAndNotesAtomListing;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Controller;
    8. import org.springframework.web.bind.annotation.PathVariable;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import com.email.po.Email;
    11. import com.email.po.Friend;
    12. import com.email.po.User;
    13. import com.email.service.FriendService;
    14. import com.email.service.UserService;
    15. @Controller
    16. @RequestMapping("/friend")
    17. public class FriendController {
    18. @Autowired
    19. private FriendService friendService;
    20. @Autowired
    21. private UserService userService;
    22. //查看我的通讯录
    23. @RequestMapping("/myfriend/{userid}")
    24. public String myfriend(HttpServletRequest request,@PathVariable String userid) {
    25. List list = friendService.getAllFriends(userid+".com");
    26. request.setAttribute("friends", list);
    27. return "friend";
    28. }
    29. //添加朋友
    30. @RequestMapping("/add")
    31. public String add(HttpServletRequest request,HttpSession session,String friendId) {
    32. User user = (User)session.getAttribute("user");
    33. List lists = friendService.getAllFriends(user.getUserid());
    34. for (Friend friend : lists) {
    35. if(friend.getFriendId().equals(friendId)) {
    36. request.setAttribute("error", "该用户已经存在通信录中!请勿重新添加");
    37. List list = friendService.getAllFriends(user.getUserid());
    38. request.setAttribute("friends", list);
    39. return "friend";
    40. }
    41. }
    42. Friend friend = new Friend();
    43. friend.setUserid(user.getUserid());
    44. friend.setFriendId(friendId);
    45. User fri = userService.getUserByUserid(friendId);
    46. if(fri==null) {
    47. List list = friendService.getAllFriends(user.getUserid());
    48. request.setAttribute("error", "您要添加的用户不存在!");
    49. request.setAttribute("friends", list);
    50. return "friend";
    51. }
    52. friend.setFriendName(fri.getUsername());
    53. friendService.add(friend);
    54. List list = friendService.getAllFriends(user.getUserid());
    55. request.setAttribute("success", "添加成功!");
    56. request.setAttribute("friends", list);
    57. return "friend";
    58. }
    59. //删除通讯录好友
    60. @RequestMapping("/delete/{friendId}")
    61. public String delete(HttpServletRequest request,HttpSession session,@PathVariable String friendId) {
    62. User user = (User)session.getAttribute("user");
    63. friendService.delete(friendId+".com");
    64. List list = friendService.getAllFriends(user.getUserid());
    65. request.setAttribute("friends", list);
    66. return "friend";
    67. }
    68. //给朋友写信,带着朋友信息跳转到写信页面
    69. @RequestMapping("/write/{friendId}")
    70. public String write(HttpServletRequest request,@PathVariable String friendId) {
    71. Email email = new Email();
    72. email.setAddresserId(friendId+".com");
    73. request.setAttribute("save", email);
    74. return "send";
    75. }
    76. }

    SaveController

    1. package com.email.controller.save;
    2. import java.util.List;
    3. import javax.servlet.http.HttpServletRequest;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import com.email.mapper.SaveMapper;
    9. import com.email.po.Save;
    10. import com.email.po.User;
    11. import com.email.service.SaveService;
    12. @Controller
    13. @RequestMapping("/save")
    14. public class SaveController {
    15. @Autowired
    16. private SaveService saveService;
    17. @Autowired
    18. private SaveMapper saveMapper;
    19. //查看我的草稿箱
    20. @RequestMapping("/mySave/{userid}")
    21. public String mySave(HttpServletRequest request,@PathVariable String userid) {
    22. List list = saveService.selectByEE(userid+".com");
    23. request.setAttribute("saves", list);
    24. return "save";
    25. }
    26. //查看草稿箱具体邮件内容
    27. @RequestMapping("/look/{id}")
    28. public String look(HttpServletRequest request,@PathVariable int id) {
    29. Save save = saveService.selectBySaveId(id);
    30. request.setAttribute("save", save);
    31. return "send";
    32. }
    33. //查看草稿箱具体邮件内容
    34. @RequestMapping("/reDelete/{id}")
    35. public String reDelete(HttpServletRequest request,@PathVariable int id) {
    36. saveMapper.deleteByPrimaryKey(id);
    37. User currentUser = (User)request.getSession().getAttribute("user");
    38. String userid = currentUser.getUserid();
    39. List list = saveService.selectByEE(userid);
    40. request.setAttribute("saves", list);
    41. return "save";
    42. }
    43. }

    如果也想学习本系统,下面领取。关注并回复:162ssm

  • 相关阅读:
    linux修改docker容器时间
    番外篇 | 利用华为2023最新Gold-YOLO中的Gatherand-Distribute对特征融合模块进行改进
    markdown pdf报错问题
    数值分析笔记(二)函数插值
    Flowable并行网关的使用
    考生必读篇 | PMP6月25日考试临近,需要注意什么?
    Python学习 day02(注意事项)
    C++ 游戏:转杯子猜球位置 2
    K8s:部署 CNI 网络组件+k8s 多master集群部署+负载均衡及Dashboard k8s仪表盘图像化展示管理
    计算机毕业设计选题推荐-大学新生报到系统-Python项目实战
  • 原文地址:https://blog.csdn.net/hanyunlong1989/article/details/126446964