⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanage.java

📁 电子邮件收发系统,采用各种技术进行研究开发适用大学生课程设计
💻 JAVA
字号:
package net.meybo.mail.client;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

import javax.mail.AuthenticationFailedException;
import javax.mail.Session;
import javax.mail.Store;

import net.meybo.mail.ActiveUser;
import net.meybo.mail.Config;
import net.meybo.mail.MailServer;
import net.meybo.mail.OtherMailServer;

import org.apache.log4j.Logger;

public class UserManage {
	
	private static final Logger logger = (Logger) Logger.getLogger(UserManage.class.getName());	
	public static ActiveUser login(String username, String domain,String pass,String hostIp,String ip) {
		ActiveUser ret=null;
		String mailhost = hostIp;	
		String user = username;
		String passwd = pass;
		String protocol = "pop3";
		try {
			Session mailsession = Session.getInstance(System.getProperties(),null);
			mailsession.setDebug(false);
			Store store = mailsession.getStore(protocol); // protocol为连接协议,IMAP或是POP	
			try {
				logger.debug("connect:" + mailhost + ":" + user + "@" + domain+ ":" + passwd);
				store.connect(mailhost, -1, user + "@" + domain, passwd);
			} catch (AuthenticationFailedException e1) {
				logger.debug("通过域名不能正常登录:"+e1);					
				try {
					domain="";
					store.connect(mailhost, -1, user, passwd);
				} catch (AuthenticationFailedException e2) {
					logger.info("用户"+username+"从"+hostIp+"登录失败:" + e2);
					return null;
				}
			}
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}
		ret=new ActiveUser();
		ret.setUserName(username);
		ret.setPassword(pass);
		ret.setServerDomain(domain);
		ret.setServerHost(mailhost);
		ret.setLoginTime(new Date());
		ret.setIp(ip);	
		MailServer server=Config.findServerByDomain(domain);
		if(server instanceof OtherMailServer)
		{		
		ret.setSmptpHost(((OtherMailServer)server).getSmtp());
		}
		createUser(Config.getMailPath(),username,domain);
		String path=Config.getMailPath()+"/"+domain+"/"+username+"/newbox";
		Thread thread=new Thread(new ReciveMail(username+"@"+domain,pass,mailhost,path));
		thread.start();
		return ret;
	}
	
	public static int modifyPassword(ActiveUser user,String newpassword,String serverIP,String serverPort){
		   int returnValue=-1;
		   Socket cs=null;
		   try{
		      cs = new Socket(serverIP, Integer.parseInt(serverPort));
		      final InputStream from_server= cs.getInputStream();
		      OutputStream to_server =cs.getOutputStream();		      
			  BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
		      PrintWriter send = new PrintWriter(to_server, true);
		      String recvMsg = recv.readLine();  //recive rsponse;			  
			  String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
			  System.out.println(strCOmmand);
		      send.println(strCOmmand);
		      recvMsg = recv.readLine();
		      System.out.println(recvMsg);
			  String recvPrefix=recvMsg.substring(0,2);
			  if(recvPrefix.equals("25")){
			  // recvMsg = recv.readLine(); 			  
			 strCOmmand = "MODIPASS "+user.getServerDomain()+" "+user.getUserName()+" "+newpassword;
			  send.println(strCOmmand);
			  recvMsg = recv.readLine();
			  System.out.println(recvMsg);
			  recvPrefix=recvMsg.substring(0,3);
			  if(recvPrefix.equals("250")) returnValue=0;  //成功
			  if(recvPrefix.equals("500")) returnValue=1;  //参数不正确(用户不存在)
			  if(recvPrefix.equals("501")) returnValue=2;  //修改失败,可以重试			  
			  strCOmmand = "QUIT";
			  send.println(strCOmmand);	
			  }			  
			  cs.close();
			  cs=null;
		  }
		  catch (Exception e){
		      try{
			     if(cs!=null){
				    cs.close();
					cs=null;
				 }
			  }
			  catch (Exception ee){  }
		     logger.error("修改密码错误:"+e);
		  }
		  return returnValue;
		}
	public static String getUserInfo(ActiveUser user,String field,String serverIP,String serverPort){
		  String returnValue=null;
		   Socket cs=null;
		   try{
		      cs = new Socket(serverIP, Integer.parseInt(serverPort));
		      final InputStream from_server= cs.getInputStream();
		      OutputStream to_server =cs.getOutputStream();		      
			  BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
		      PrintWriter send = new PrintWriter(to_server, true);
		      String recvMsg = recv.readLine();  //recive rsponse;			  
			  String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
			  System.out.println(strCOmmand);
		      send.println(strCOmmand);
		      recvMsg = recv.readLine();
		      System.out.println(recvMsg);
			  String recvPrefix=recvMsg.substring(0,2);
			  if(recvPrefix.equals("25")){
			  // recvMsg = recv.readLine(); 			  
			 strCOmmand = "GETUSER "+user.getServerDomain()+" "+user.getUserName()+" "+field;
			  send.println(strCOmmand);
			  recvMsg = recv.readLine();
			  System.out.println(recvMsg);
			  recvPrefix=recvMsg.substring(0,3);
			  if(recvPrefix.equals("250")) returnValue=recvMsg.substring(4);  //成功			  			  
			  strCOmmand = "QUIT";
			  send.println(strCOmmand);	
			  }			  
			  cs.close();
			  cs=null;
		  }
		  catch (Exception e){
		      try{
			     if(cs!=null){
				    cs.close();
					cs=null;
				 }
			  }
			  catch (Exception ee){  }
		     logger.error("修改密码错误:"+e);
		  }
		  return returnValue;
		}
	public static int  modifyUserInfo(ActiveUser user,String field,String value,String serverIP,String serverPort){
		  int returnValue=-1;
		   Socket cs=null;
		   try{
		      cs = new Socket(serverIP, Integer.parseInt(serverPort));
		      final InputStream from_server= cs.getInputStream();
		      OutputStream to_server =cs.getOutputStream();		      
			  BufferedReader recv = new BufferedReader(new InputStreamReader(from_server));
		      PrintWriter send = new PrintWriter(to_server, true);
		      String recvMsg = recv.readLine();  //recive rsponse;			  
			  String strCOmmand = "CHCK "+user.getServerDomain()+" "+user.getUserName()+" "+user.getPassword();
			  System.out.println(strCOmmand);
		      send.println(strCOmmand);
		      recvMsg = recv.readLine();
		      System.out.println(recvMsg);
			  String recvPrefix=recvMsg.substring(0,2);
			  if(recvPrefix.equals("25")){
			  // recvMsg = recv.readLine(); 			  
			  strCOmmand = "MODIUSER "+user.getServerDomain()+" "+user.getUserName()+" "+field+" "+value;
			  send.println(strCOmmand);
			  recvMsg = recv.readLine();
			  System.out.println(recvMsg);
			  recvPrefix=recvMsg.substring(0,3);
			  if(recvPrefix.equals("250")) returnValue=0;  //成功
			  if(recvPrefix.equals("500")) returnValue=1;  //参数不正确(用户不存在)
			  if(recvPrefix.equals("501")) returnValue=2;  //修改失败,可以重试
			  strCOmmand = "QUIT";
			  send.println(strCOmmand);	
			  }			  
			  cs.close();
			  cs=null;
		  }
		  catch (Exception e){
		      try{
			     if(cs!=null){
				    cs.close();
					cs=null;
				 }
			  }
			  catch (Exception ee){  }
		     logger.error("修改密码错误:"+e);
		  }
		  return returnValue;
		}
	public static boolean createUser(String mailPath, String name, String domain) {
		boolean ret = false;
		String Path=mailPath+"/"+domain+"/"+name;
		if (Path == null || Path.equals("")) {
			ret = false;
		} else {		
			try {
				java.io.File f = new java.io.File(Path + "/newbox");
						if(!f.exists())f.mkdirs();
						f = new java.io.File(Path + "/inbox");
						if(!f.exists())f.mkdirs();
						f = new java.io.File(Path + "/sendbox");
						if(!f.exists())f.mkdirs();
						f = new java.io.File(Path + "/delbox");						
						if(!f.exists())f.mkdirs();						
						f = new java.io.File(Path + "/files");
						if(!f.exists())f.mkdirs();	
						f = new java.io.File(Path + "/draftbox");
						if(!f.exists())f.mkdirs();	
						ret = true;					
			} catch (Exception ex) {
				logger.error(ex);
			}		
		}
		return ret;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -