See More

package com.kevin.mail; import java.io.File; import java.io.IOException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class Demo1 { public static void main(String[] args) throws Exception { test1(); test2(); } //·¢ËÍÆÕͨÓʼþ public static void test1() { //µÃµ½Session Properties prop = new Properties(); //ÉèÖ÷þÎñÆ÷Ö÷»úÃû prop.setProperty("mail.host", "smtp.qq.com"); //ÉèÖÃÐèÒªÈÏÖ¤ prop.setProperty("mail.smtp.auth", "true"); Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication pass = new PasswordAuthentication("[email protected]","Direct19931209"); return pass; } }; Session session = Session.getInstance(prop,auth); //´´½¨MimeMessage MimeMessage msg = new MimeMessage(session); try { //ÉèÖ÷¢¼þÈËÐÅÏ¢ msg.setFrom(new InternetAddress("[email protected]")); //ÉèÖÃÊÕ¼þÈËÐÅÏ¢ msg.setRecipients(RecipientType.TO, "[email protected]"); //ÉèÖó­ËÍ msg.setRecipients(RecipientType.CC, "[email protected]"); //ÉèÖÃÃØËÍ msg.setRecipients(RecipientType.BCC,"[email protected]"); msg.setSubject("ÕâÊÇÀ´×ÔKevinBlandyµÄ²âÊÔÓʼþ");//ÉèÖñêÌâ msg.setContent("Õâ¾ÍÊÇÒ»·âÀ¬»øÓʼþ,¹þ¹þ","text/html;charset=utf-8");//ÉèÖÃÕýÎÄÒÔ¼°ÀàÐÍ,±àÂë //·¢ËÍÓʼþ Transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); } } //·¢ËÍ´øÓи½¼þµÄÓʼþ public static void test2() throws IOException { //µÃµ½Session Properties prop = new Properties(); //ÉèÖ÷þÎñÆ÷Ö÷»úÃû prop.setProperty("mail.host", "smtp.qq.com"); //ÉèÖÃÐèÒªÈÏÖ¤ prop.setProperty("mail.smtp.auth", "true"); Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication pass = new PasswordAuthentication("[email protected]","Direct19931209"); return pass; } }; Session session = Session.getInstance(prop,auth); //´´½¨MimeMessage MimeMessage msg = new MimeMessage(session); try { //ÉèÖ÷¢¼þÈËÐÅÏ¢ msg.setFrom(new InternetAddress("[email protected]")); //ÉèÖÃÊÕ¼þÈËÐÅÏ¢ msg.setRecipients(RecipientType.TO, "[email protected]"); //ÉèÖó­ËÍ msg.setRecipients(RecipientType.CC, "[email protected]"); //ÉèÖÃÃØËÍ msg.setRecipients(RecipientType.BCC,"[email protected]"); msg.setSubject("ÕâÊÇÀ´×ÔKevinBlandyµÄ²âÊÔÓʼþ");//ÉèÖñêÌâ /***********************°üº¬¸½¼þ**************************************/ /** * µ±·¢ËͰüº¬¸½¼þµÄÓʼþʱ,ÓʼþÌå¾ÍΪ¶à²¿¼þÐÎʽ * */ //´´½¨¶à²¿¼þÖ÷Ìâ MimeMultipart list = new MimeMultipart(); //´´½¨MimeBodyPart MimeBodyPart part1 = new MimeBodyPart(); //ÉèÖò¿¼þµÄÄÚÈÝ part1.setContent("ÕâÊÇÒ»·â´øÓи½¼þµÄÓʼþ","text/html;charset=utf-8"); //°Ñ²¿Ö÷Ì岿¼þÌí¼Óµ½¼¯ºÏÖÐ list.addBodyPart(part1); //ÔÚ´´½¨Ò»¸öMimeBodyPart MimeBodyPart part2 = new MimeBodyPart(); //ÉèÖò¿¼þµÄÄÚÈÝ --- ¸½¼þÎļþ part2.attachFile(new File("D:\\test.txt")); //ÏÔʾÔÚ¸½¼þÉÏ,²¢ÇÒ´¦ÀíÃû³ÆÂÒÂëÎÊÌâ part2.setFileName(MimeUtility.encodeText("ÎļþÃû³Æ.txt")); //ÔٴΰÑÖ÷Ö÷Ì岿¼þ,Ìí¼Óµ½¼¯ºÏÖÐ list.addBodyPart(part2); //ÉèÖøøÓʼþ×÷ΪÓʼþµÄÄÚÈÝ msg.setContent(list); /******************************************************************/ //·¢ËÍÓʼþ Transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); } } }