자바 메 일 첨부 파일 보 내기

3552 단어 우편물email
package com.util;


import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 * 
 * @author macwhirr
 *
 */
public class SendEmailHtml {

	
	public static void sendEmailMethod(String emailTitle,String emailContent,String args[],String args2[])
	{

		EmailAttachment attachment=null;
		//    。
		//         ,      ,    。path     。
//		EmailAttachment attachment = new EmailAttachment(); 
//		try {
//			attachment.setPath(path); 
//			attachment.setDisposition(EmailAttachment.ATTACHMENT); 
//			attachment.setDescription(MimeUtility.encodeWord("  ","UTF-8",null)); 
//			attachment.setName(MimeUtility.encodeWord(filename+".xls","UTF-8",null));
//		} catch (UnsupportedEncodingException e1) {
//			// TODO Auto-generated catch block
//			attachment=null;
//			e1.printStackTrace();
//		} 


		HtmlEmail email = new HtmlEmail();    //     
		//MultiPartEmail email = new MultiPartEmail();   //     
		// SimpleEmail email = new SimpleEmail();    //     
		
         try {
//			 email.setHostName("smtp.qq.com");//        pop.126.com  ,smtp.126.com
        	 email.setHostName("smtp.exmail.qq.com");//          
//			 email.setSmtpPort(465);
			 email.setAuthentication("      ", "        ");//smtp         
			 
			 for (int i = 0; i < args.length; i++) {
				 email.addTo(args[i],"");//   
			}
			 if (args2.length!=0) {
				 for (int i = 0; i < args2.length; i++) {
					 email.addCc(args2[i], "");//     
				 }
			}
		//	 email.addTo(args[1],"");//   
		//	 email.addTo(args[2],"");//   
		//	 email.addTo(args[3],"");//   
			// email.addTo(args[4],"");//   2012-11-20 10:31:15  
			 
			 email.setFrom("      ", "      ");//   
			 
			 email.setSubject(emailTitle);//     
			 email.setCharset("UTF-8");//      
			 
			 
//			URL url=null;
//			try {
//				url = new URL("");  //    ,         
//			} catch (MalformedURLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			String whylogo = email.embed(url, "WHY logo.jpg");  //      <img src='cid:"+whylogo+"'         
			
			  //email.setHtmlMsg("");  --- email.setMsg("")
			 email.setHtmlMsg(emailContent) ;
//			 		+ "<br><img src='cid:"+whylogo+"' +" +
//			 		"width='100' height='100' /></body></html>");//     
			 
			 if(attachment!=null)
			 {
			    email.attach(attachment);    //    
			 }

			 //gmail                        25,  465
			 //email.setSSL(Boolean.TRUE); //       SSL
//			 email.setSslSmtpPort("465"); //   SSL  

			 email.send();//    
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String mail[]=new String[4];

		mail[0]="     1";
		mail[1]="     2";
		mail[2]="     3";
		mail[3]="     4";

		//  
		String ccmail[]={"[email protected]"};
		
		//    
		SendEmailHtml.sendEmailMethod("          ", "         <br/><br/>     →_→  ", mail,ccmail);
	
	}
}

좋은 웹페이지 즐겨찾기