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 Demo1 {

	/**
	 * @param args
	 *            add by zxx ,Feb 5, 2009
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
////		
		args=new String[]{"ffff@@11hhhh","wangc@connor.net.cn","[任务名称:验证阶段;所在项目:@@11]##11hhhhh##11kkk"};
		System.out.println("邮件发送开始");
		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");
			Demo1 de=new Demo1();
//			de.sendmail(s2,s1,s3);
			de.sendmail(s1,s2,s3);
			System.out.println("邮件发送成功");
			
		}else{
			return;
		}
		
		
	}
	
	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;
		String emailtext=nr;
		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祝您工作愉快！");
		 msg.setFrom(new InternetAddress("plmadmin@ks.shdlgroup.com"));
		Transport transport = session.getTransport();
//		 transport.connect("smtp.exmail.qq.com",
//		 25,"kunyu.liang@wfjt.com", "15958502268Lky");
		 transport.connect("mail.shdlgroup.com",
				 25,"plmadmin@ks.shdlgroup.com", "shdl@PA");


		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();
		}
	}
}
