package myBean.mail;

import javax.mail.*;  
import javax.mail.internet.*;  
import java.util.*;
import java.rmi.*;
import javax.activation.*;
import java.io.*;
import myBean.mail.*;

public class MailManager {
	
	//比如说有一个邮件帐号: smtpuser@xxx.com
	//POP3_HOST_NAME和SMTP_HOST_NAME分别是这邮件地址的pop3和smtp服务器DNS
	//则SMTP_AUTH_USER ="smtpuser", SMTP_AUTH_PWD就是该帐号的密码

	private  final String POP3_HOST_NAME = "pop3.xxx.com";
	private  final String SMTP_HOST_NAME = "smtp.xxx.com";
	private  final String SMTP_AUTH_USER = "smtpuser";
	private  final String SMTP_AUTH_PWD  = "*****";
	
	
	private Authenticator auth = new Authenticator(){
		public PasswordAuthentication getPasswordAuthentication(){
			return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
		}
	};
	
	public void sendMail(String toAddr, String subject, 
					String body, String fromAddr, String contentType) {
		try {  
			Properties props = new Properties();  
			
			//指定SMTP服务器，邮件通过它来投递
			props.put("mail.smtp.host", SMTP_HOST_NAME); 
			props.put("mail.smtp.auth", "true");
			
			Session session = Session.getDefaultInstance(props, auth);  
			Message msg = new MimeMessage(session);  
			
			//指定发信人
			msg.setFrom(new InternetAddress(fromAddr));

			//指定收件人
			//InternetAddress[] tos = {new InternetAddress(toAddr)};  
			//msg.setRecipients(Message.RecipientType.TO,tos);
			  
			//指定收件人，多人时用逗号分隔 
			InternetAddress[] tos =InternetAddress.parse(toAddr);  
			msg.setRecipients(Message.RecipientType.TO,tos);
			  
			//标题
			msg.setSubject(subject); 
			
			//内容
			msg.setText(body); 
			
			//发送时间
			msg.setSentDate(new Date());
			
			//内容类型Content-type
			//普通文本为text/plain,html格式为text/html
			msg.setContent(body, contentType);
			
			//发送
			Transport.send(msg);  
			
		} catch(Exception e){  
			System.out.println(e);  
		}  
	}
	
	public void sendMailWithAttatchment(String toAddr, String subject, String body,
								String fromAddr, String contentType, String []fileList) {
		try {  
			Properties props = new Properties();  
			
			//指定SMTP服务器，邮件通过它来投递
			props.put("mail.smtp.host", SMTP_HOST_NAME); 
			props.put("mail.smtp.auth", "true");

			Session session = Session.getDefaultInstance(props, auth);  
			Message msg = new MimeMessage(session);  
			
			//指定发信人
			msg.setFrom(new InternetAddress(fromAddr));
			  
			//指定收件人
			//InternetAddress[] tos = {new InternetAddress(toAddr)};  
			//msg.setRecipients(Message.RecipientType.TO,tos);

			//指定收件人，多人时用逗号分隔 
			InternetAddress[] tos =InternetAddress.parse(toAddr);  
			msg.setRecipients(Message.RecipientType.TO,tos);
			  
			//标题
			msg.setSubject(subject); 
			
			//发送时间
			msg.setSentDate(new Date());
			
			Multipart multipart = new MimeMultipart();
			MimeBodyPart bodyPart = new MimeBodyPart();
			//内容
			bodyPart.setText(body);
			//Content-type
			bodyPart.setContent(body, contentType);
			multipart.addBodyPart(bodyPart);
			
			for(int i=0; i<fileList.length; ++i) {
				bodyPart = new MimeBodyPart();
				File f = new File(fileList[i]);
				DataSource source = new FileDataSource(f);
				bodyPart.setDataHandler(new DataHandler(source));
				bodyPart.setFileName(f.getName());
				
				multipart.addBodyPart(bodyPart);
			}
			
			msg.setContent(multipart);

			//发送
			Transport.send(msg);  
			
		} catch(Exception e){  
			System.out.println(e);  
		}  
	}

	public Mails getMails() {
		Mails mails=null;
		try {  
			//Properties props = System.getProperties();
			Properties props = new Properties(); 			
			props.put("mail.pop3.host", SMTP_HOST_NAME); 
			props.put("mail.pop3.auth", "true");

			Session session = Session.getDefaultInstance(props, auth);
			
			Store store = session.getStore("pop3");
			store.connect();
			
			Folder inbox = store.getFolder("INBOX"); 
			mails = new Mails(inbox);
			store.close();
						
		} catch(Exception e){
			System.out.println(e); 
		}
		
		return mails;
	}
}