UserManage.java
Upload User: gtz2001
Upload Date: 2016-12-29
Package Size: 2489k
Code Size: 8k
Category:

WEB Mail

Development Platform:

Java

  1. package net.meybo.mail.client;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.util.Date;
  9. import javax.mail.AuthenticationFailedException;
  10. import javax.mail.Session;
  11. import javax.mail.Store;
  12. import net.meybo.mail.ActiveUser;
  13. import net.meybo.mail.Config;
  14. import net.meybo.mail.MailServer;
  15. import net.meybo.mail.OtherMailServer;
  16. import org.apache.log4j.Logger;
  17. public class UserManage {
  18. private static final Logger logger = (Logger) Logger.getLogger(UserManage.class.getName());
  19. public static ActiveUser login(String username, String domain,String pass,String hostIp,String ip) {
  20. ActiveUser ret=null;
  21. String mailhost = hostIp;
  22. String user = username;
  23. String passwd = pass;
  24. String protocol = "pop3";
  25. try {
  26. Session mailsession = Session.getInstance(System.getProperties(),null);
  27. mailsession.setDebug(false);
  28. Store store = mailsession.getStore(protocol); // protocol为连接协议,IMAP或是POP
  29. try {
  30. logger.debug("connect:" + mailhost + ":" + user + "@" + domain+ ":" + passwd);
  31. store.connect(mailhost, -1, user + "@" + domain, passwd);
  32. } catch (AuthenticationFailedException e1) {
  33. logger.debug("通过域名不能正常登录:"+e1);
  34. try {
  35. domain="";
  36. store.connect(mailhost, -1, user, passwd);
  37. } catch (AuthenticationFailedException e2) {
  38. logger.info("用户"+username+"从"+hostIp+"登录失败:" + e2);
  39. return null;
  40. }
  41. }
  42. } catch (Exception ex) {
  43. logger.error(ex);
  44. return null;
  45. }
  46. ret=new ActiveUser();
  47. ret.setUserName(username);
  48. ret.setPassword(pass);
  49. ret.setServerDomain(domain);
  50. ret.setServerHost(mailhost);
  51. ret.setLoginTime(new Date());
  52. ret.setIp(ip);
  53. MailServer server=Config.findServerByDomain(domain);
  54. if(server instanceof OtherMailServer)
  55. {
  56. ret.setSmptpHost(((OtherMailServer)server).getSmtp());
  57. }
  58. createUser(Config.getMailPath(),username,domain);
  59. String path=Config.getMailPath()+"/"+domain+"/"+username+"/newbox";
  60. Thread thread=new Thread(new ReciveMail(username+"@"+domain,pass,mailhost,path));
  61. thread.start();
  62. return ret;
  63. }
  64. public static int modifyPassword(ActiveUser user,String newpassword,String serverIP,String serverPort){
  65.    int returnValue=-1;
  66.    Socket cs=null;
  67.    try{
  68.       cs = new Socket(serverIP, Integer.parseInt(serverPort));
  69.       final InputStream from_server= cs.getInputStream();
  70.       OutputStream to_server =cs.getOutputStream();       
  71.   BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
  72.       PrintWriter send = new PrintWriter(to_server, true);
  73.       String recvMsg = recv.readLine();  //recive rsponse;   
  74.   String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
  75.   System.out.println(strCOmmand);
  76.       send.println(strCOmmand);
  77.       recvMsg = recv.readLine();
  78.       System.out.println(recvMsg);
  79.   String recvPrefix=recvMsg.substring(0,2);
  80.   if(recvPrefix.equals("25")){
  81.   // recvMsg = recv.readLine();    
  82.  strCOmmand = "MODIPASS "+user.getServerDomain()+" "+user.getUserName()+" "+newpassword;
  83.   send.println(strCOmmand);
  84.   recvMsg = recv.readLine();
  85.   System.out.println(recvMsg);
  86.   recvPrefix=recvMsg.substring(0,3);
  87.   if(recvPrefix.equals("250")) returnValue=0;  //成功
  88.   if(recvPrefix.equals("500")) returnValue=1;  //参数不正确(用户不存在)
  89.   if(recvPrefix.equals("501")) returnValue=2;  //修改失败,可以重试   
  90.   strCOmmand = "QUIT";
  91.   send.println(strCOmmand);
  92.   }   
  93.   cs.close();
  94.   cs=null;
  95.   }
  96.   catch (Exception e){
  97.       try{
  98.      if(cs!=null){
  99.     cs.close();
  100. cs=null;
  101.  }
  102.   }
  103.   catch (Exception ee){  }
  104.      logger.error("修改密码错误:"+e);
  105.   }
  106.   return returnValue;
  107. }
  108. public static String getUserInfo(ActiveUser user,String field,String serverIP,String serverPort){
  109.   String returnValue=null;
  110.    Socket cs=null;
  111.    try{
  112.       cs = new Socket(serverIP, Integer.parseInt(serverPort));
  113.       final InputStream from_server= cs.getInputStream();
  114.       OutputStream to_server =cs.getOutputStream();       
  115.   BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
  116.       PrintWriter send = new PrintWriter(to_server, true);
  117.       String recvMsg = recv.readLine();  //recive rsponse;   
  118.   String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
  119.   System.out.println(strCOmmand);
  120.       send.println(strCOmmand);
  121.       recvMsg = recv.readLine();
  122.       System.out.println(recvMsg);
  123.   String recvPrefix=recvMsg.substring(0,2);
  124.   if(recvPrefix.equals("25")){
  125.   // recvMsg = recv.readLine();    
  126.  strCOmmand = "GETUSER "+user.getServerDomain()+" "+user.getUserName()+" "+field;
  127.   send.println(strCOmmand);
  128.   recvMsg = recv.readLine();
  129.   System.out.println(recvMsg);
  130.   recvPrefix=recvMsg.substring(0,3);
  131.   if(recvPrefix.equals("250")) returnValue=recvMsg.substring(4);  //成功      
  132.   strCOmmand = "QUIT";
  133.   send.println(strCOmmand);
  134.   }   
  135.   cs.close();
  136.   cs=null;
  137.   }
  138.   catch (Exception e){
  139.       try{
  140.      if(cs!=null){
  141.     cs.close();
  142. cs=null;
  143.  }
  144.   }
  145.   catch (Exception ee){  }
  146.      logger.error("修改密码错误:"+e);
  147.   }
  148.   return returnValue;
  149. }
  150. public static int  modifyUserInfo(ActiveUser user,String field,String value,String serverIP,String serverPort){
  151.   int returnValue=-1;
  152.    Socket cs=null;
  153.    try{
  154.       cs = new Socket(serverIP, Integer.parseInt(serverPort));
  155.       final InputStream from_server= cs.getInputStream();
  156.       OutputStream to_server =cs.getOutputStream();       
  157.   BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
  158.       PrintWriter send = new PrintWriter(to_server, true);
  159.       String recvMsg = recv.readLine();  //recive rsponse;   
  160.   String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
  161.   System.out.println(strCOmmand);
  162.       send.println(strCOmmand);
  163.       recvMsg = recv.readLine();
  164.       System.out.println(recvMsg);
  165.   String recvPrefix=recvMsg.substring(0,2);
  166.   if(recvPrefix.equals("25")){
  167.   // recvMsg = recv.readLine();    
  168.   strCOmmand = "MODIUSER "+user.getServerDomain()+" "+user.getUserName()+" "+field+" "+value;
  169.   send.println(strCOmmand);
  170.   recvMsg = recv.readLine();
  171.   System.out.println(recvMsg);
  172.   recvPrefix=recvMsg.substring(0,3);
  173.   if(recvPrefix.equals("250")) returnValue=0;  //成功
  174.   if(recvPrefix.equals("500")) returnValue=1;  //参数不正确(用户不存在)
  175.   if(recvPrefix.equals("501")) returnValue=2;  //修改失败,可以重试
  176.   strCOmmand = "QUIT";
  177.   send.println(strCOmmand);
  178.   }   
  179.   cs.close();
  180.   cs=null;
  181.   }
  182.   catch (Exception e){
  183.       try{
  184.      if(cs!=null){
  185.     cs.close();
  186. cs=null;
  187.  }
  188.   }
  189.   catch (Exception ee){  }
  190.      logger.error("修改密码错误:"+e);
  191.   }
  192.   return returnValue;
  193. }
  194. public static boolean createUser(String mailPath, String name, String domain) {
  195. boolean ret = false;
  196. String Path=mailPath+"/"+domain+"/"+name;
  197. if (Path == null || Path.equals("")) {
  198. ret = false;
  199. } else {
  200. try {
  201. java.io.File f = new java.io.File(Path + "/newbox");
  202. if(!f.exists())f.mkdirs();
  203. f = new java.io.File(Path + "/inbox");
  204. if(!f.exists())f.mkdirs();
  205. f = new java.io.File(Path + "/sendbox");
  206. if(!f.exists())f.mkdirs();
  207. f = new java.io.File(Path + "/delbox");
  208. if(!f.exists())f.mkdirs();
  209. f = new java.io.File(Path + "/files");
  210. if(!f.exists())f.mkdirs();
  211. f = new java.io.File(Path + "/draftbox");
  212. if(!f.exists())f.mkdirs();
  213. ret = true;
  214. } catch (Exception ex) {
  215. logger.error(ex);
  216. }
  217. }
  218. return ret;
  219. }
  220. /**
  221.  * @param args
  222.  */
  223. public static void main(String[] args) {
  224. // TODO Auto-generated method stub
  225. }
  226. }