import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class SendMail
{
  static final String MAIL_HOST = "61.177.95.155";
  static final boolean MAIL_NEEDAUTH = true;
  static final String DEFAULT_MAIL_USER = "lioulb@126.com";
  static final String DEFAULT_MAIL_PASSWORD = ".......";
  static final String DEFAULT_FORMAT = "plain"; //纯文本
  private MimeMessage mimeMsg; //MIME邮件对象
  private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
  private Session session; //邮件会话对象
  private Properties props; //系统属性
  private boolean needAuth; //smtp是否需要认证
  private String userName; //smtp认证用户名和密码
  private String password; //smtp认证密码
  private String mailFormat = DEFAULT_FORMAT; //邮件文本格式
  public SendMail(String host,boolean needAuth,String user,String password)
  { //构造方法
    if(host==null||host.trim().equals(""))
    {
	host = MAIL_HOST;
    }
    setHost(host);
    createMimeMessage();
    setAuth(needAuth);
    if(user==null)
    {
	user = "";
    }
    if(password==null)
    {
	password = "";
    }
    setUser(user,password);
    setFrom(user);
  }
  public SendMail()
  {
    setHost(MAIL_HOST);
    createMimeMessage();
    setAuth(MAIL_NEEDAUTH);
    setUser(DEFAULT_MAIL_USER,DEFAULT_MAIL_PASSWORD);
    setFrom(DEFAULT_MAIL_USER);
  }
  private void setHost(String hostName)
  { //设置smtp的主机地址
    if(props==null)
    {
	props = System.getProperties(); //获得系统属性对象
    }
    props.put("mail.smtp.host",hostName); //设置SMTP主机
  }
  private void setAuth(boolean need)
  { //smtp认证
    if(props==null)
    {
	props = System.getProperties();
    }
    if(need)
    {
	props.put("mail.smtp.auth","true");
    }
    else
    {
	props.put("mail.smtp.auth","false");
    }
  }
  private void setUser(String userName,String password)
  { //设置smtp用户名和密码
    this.userName = userName;
    this.password = password;
  }
  private boolean createMimeMessage()
  { //生成邮件对象
    try
    {
	session = Session.getDefaultInstance(props,null); //获得邮件会话对象
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
    try
    {
	mimeMsg = new MimeMessage(session); //创建MIME邮件对象
	mp = new MimeMultipart();
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private void setMailFormat(String format)
  { //设置邮件的正文格式  plain:纯文本格式  html:html格式
    if(format==null)
    {
	format = "plain";
    }
    format = format.trim();
    if(format.equals("plain")||format.equals("html"))
    {
	this.mailFormat = "text/"+format;
    }
    else
    {
	this.mailFormat = "text/plain";
    }
  }
  public boolean sendMail(String to,String subject,String body,String format)
  { //发送不带附件,不转发的邮件
    boolean theReturn = true;
    setMailFormat(format);
    // String aLine = Time.getdate()+" "+Time.gettime()+" send: "+this.userName
    //+" "+to+" "+Common.convertToGb(subject);
    String aLine = " send: "+this.userName
	+" "+to+" "+subject;
    if(setSubject(subject)&&setBody(body)&&setTo(to))
    {
	theReturn = sendOut();
	aLine = aLine+" [Success]";
    }
    else
    {
	theReturn = false;
	aLine = aLine+" [Failed]";
    }
    return theReturn;
  }
  public boolean sendMail(String to,String subject,String body)
  {
    return sendMail(to,subject,body,DEFAULT_FORMAT);
  }
  private boolean setSubject(String mailSubject)
  { //设置邮件主题
    try
    {
	//mailSubject = Common.convertToGb(mailSubject);
	mimeMsg.setSubject(mailSubject);
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private boolean setBody(String mailBody)
  { //设置邮件正文
    try
    {
	//mailBody = Common.convertToGb(mailBody);
	BodyPart bp = new MimeBodyPart();
	bp.setContent(mailBody,this.mailFormat+";charset=GB2312"); //"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody
	mp.addBodyPart(bp);
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private boolean setFrom(String from)
  { //设置发信人地址
    try
    {
	mimeMsg.setFrom(new InternetAddress(from));
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private boolean setTo(String to)
  { //设置收信人地址
    if(to==null)
    {
	return false;
    }
    try
    {
	mimeMsg.addRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private boolean addFileAffix(String filename)
  { //添加附件
    try
    {
	BodyPart bp = new MimeBodyPart();
	FileDataSource fileds = new FileDataSource(filename);
	bp.setDataHandler(new DataHandler(fileds));
	bp.setFileName(fileds.getName());
	mp.addBodyPart(bp);
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  private boolean setCopyTo(String copyto)
  { //设置转发人地址
    if(copyto==null)
    {
	return false;
    }
    try
    {
	mimeMsg.addRecipients(Message.RecipientType.CC,
				    (Address[])InternetAddress.parse(copyto));
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  public int tryToConnect()
  { //连接邮箱  1:连接成功  0:连接失败  -1:已经连接或系统忙
    int theReturn = 0;
    // String aLine = Time.getdate()+" "+Time.gettime()+" Connect: "+this.userName
    //+" "+this.userName+" "+this.password;
    String aLine = " Connect: "+this.userName
	+" "+this.userName+" "+this.password;
    try
    {
	Session mailSession = Session.getInstance(props,null);
	Transport transport = mailSession.getTransport("smtp");
	transport.connect((String)props.get("mail.smtp.host"),this.userName,
				this.password);
	transport.close();
	theReturn = 1;
	aLine = aLine+" [Success]";
    }
    catch(MessagingException e)
    {
	e.printStackTrace();
	theReturn = 0;
    }
    catch(IllegalStateException e)
    {
	e.printStackTrace();
	theReturn = -1;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	theReturn = 0;
	aLine = aLine+" [Failed]";
    }
    return theReturn;
  }
  private boolean sendOut()
  { //发送邮件
    try
    {
	mimeMsg.setContent(mp);
	mimeMsg.saveChanges();
	Session mailSession = Session.getInstance(props,null);
	Transport transport = mailSession.getTransport("smtp");
	transport.connect((String)props.get("mail.smtp.host"),this.userName,
				this.password);
	transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
	transport.close();
	return true;
    }
    catch(Exception e)
    {
	e.printStackTrace();
	return false;
    }
  }
  public boolean changePwd(String userName,String newPwd)
  { //修改邮箱密码
    boolean theReturn = false;
    try
    {
	String commond = "passwd "+userName;
	Process process = Runtime.getRuntime().exec(commond);
	BufferedReader br = new BufferedReader(new InputStreamReader(process.
	  getInputStream()));
	PrintStream ps = new PrintStream(process.getOutputStream());
	BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
	  getErrorStream()));
	char ac[] = new char[1024];
	br1.read(ac);
	ps.println(newPwd);
	ps.flush();
	br1.read(ac);
	ps.println(newPwd);
	ps.flush();
	br1.read(ac);
	if(process.waitFor()==0)
	{
	  theReturn = true;
	}
    }
    catch(Exception e)
    {
	e.printStackTrace();
	//e.printStackTrace(System.out);
	System.out.println(e.toString());
	theReturn = false;
    }
    return theReturn;
  }
  public boolean addUser(String userName)
  { //添加邮件用户  (密码默认为空)
    boolean theReturn = false;
    try
    {
	String commond = "/usr/sbin/useradd "+userName+
	  " -g mail -d /dev/null -s /bin/false";
	Process process = Runtime.getRuntime().exec(commond);
	BufferedReader br = new BufferedReader(new InputStreamReader(process.
	  getInputStream()));
	PrintStream ps = new PrintStream(process.getOutputStream());
	BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
	  getErrorStream()));
	char ac[] = new char[1024];
	br1.read(ac);
	if(process.waitFor()==0)
	{
	  theReturn = true;
	}
    }
    catch(Exception e)
    {
	e.printStackTrace(System.out);
	theReturn = false;
    }
    return theReturn;
  }
  public boolean addUser(String userName,String pwd)
  { //添加邮件用户
    boolean theReturn = addUser(userName);
    if(theReturn)
    {
	theReturn = changePwd(userName,pwd);
	if(!theReturn)
	{ //修改密码失败
	  deleUser(userName);
	}
    }
    return theReturn;
  }
  public boolean deleUser(String userName)
  { //删除邮件用户
    boolean theReturn = false;
    try
    {
	String commond = "/usr/sbin/userdel "+userName;
	Process process = Runtime.getRuntime().exec(commond);
	BufferedReader br = new BufferedReader(new InputStreamReader(process.
	  getInputStream()));
	PrintStream ps = new PrintStream(process.getOutputStream());
	BufferedReader br1 = new BufferedReader(new InputStreamReader(process.
	  getErrorStream()));
	char ac[] = new char[1024];
	br1.read(ac);
	if(process.waitFor()==0)
	{
	  theReturn = true;
	}
    }
    catch(Exception exception)
    {
	exception.printStackTrace(System.out);
	theReturn = false;
    }
    return theReturn;
  }
   public static void main(String args[]){
     SendMail myMail=new SendMail();
     System.out.println(myMail.sendMail("oxservice@126.com","this is test","my \n test"));
   }
}
  
回复  更多评论