package com.connor.mail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;




public class HSEmail {

	/**
	 * @param args
	 *            add by zxx ,Feb 5, 2009
	 */
//	private static LogUtils log = new LogUtils(HSEmail.class);
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
//		log.createLogFile("sendToMysql.txt");
		args=new String[]{"测试邮件","wangc@connor.net.cn","[任务名称:验证阶段;所在项目:@@11]##11hhhhh##11kkk"};
		System.out.println("邮件发送开始");
//		log.info( "arg");
//		
//		for(String artString : args) {
//			log.info(artString);
//		}
		
		if(args!=null&&args.length>=3){
			
			String s1=args[0];
			String s2=args[1];
			String s3=args[2];
			
//			s1=s1.replaceAll("@@11", " ");
//			s3=s3.replaceAll("@@11", " ");
//			s1=s1.replaceAll("##11", "\n");
//			s3=s3.replaceAll("##11", "\n");
			HSEmail de=new HSEmail();
			de.sendmail(s1,s2,s3);
			System.out.println("邮件发送成功");
			
		}else{
			return;
		}
//		log.closeLogFile();
		
	}
	
	public  void sendmail(String zt,String sjr,String nr)  {
		// TODO Auto-generated method stub
		try {
		Address[] sendAddress = null;
		String[] sendToMails = sjr.split(",");
		for(String s :sendToMails){
			System.out.println("s==============="+s);
		}
		String emailTitle = zt;
		emailTitle = emailTitle.replaceAll("###", " ");
		emailTitle = emailTitle.replaceAll("--", "&");
		String emailtext=nr;
		emailtext = emailtext.replaceAll("###", " ");
		emailtext = emailtext.replaceAll("--", "&");
		System.out.println("zt==============="+zt);
		System.out.println("nr==============="+nr);
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		Session session = Session.getInstance(props);
		session.setDebug(true);
		Message msg = new MimeMessage(session);
		//msg.set
		msg.setSubject(emailTitle);
		
		msg.setText(emailtext);

		// msg.setText("您好：\n	您收到的是PLM项目组的测试邮件，如有打扰敬请谅解！\n\n谢谢\n\n祝您工作愉快！");
		Properties props1 = new Properties();
		props1.load(HSEmail.class.getResourceAsStream("/config.properties"));
		String server = props1.getProperty("MAIL_SERVER");
		String port = props1.getProperty("MAIL_PORT");
		String from = props1.getProperty("MAIL_SENDER");
		String password = props1.getProperty("MAIL_SENDER_PWD");
		 msg.setFrom(new InternetAddress(from));
		Transport transport = session.getTransport();
		 transport.connect(server,Integer.parseInt(port),from, password);
//		 transport.connect("smtp.connor.net.cn",
//				 25,"wangc@connor.net.cn", "");


		sendAddress = new Address[sendToMails.length];
		for (int i = 0; i < sendToMails.length; i++) {
			sendAddress[i] = new InternetAddress(sendToMails[i]);
		}
		transport.sendMessage(msg, sendAddress);
		transport.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
