老妖的博客
          現實的中沒有幾個人能夠真為對方去死,甚至山盟海誓很快就會在金錢面前變的微不足道,這才是生活。沒有永遠的愛,除了你的父母對你,當然也就沒有永遠的恨,更沒有永遠的痛,時間是最好的治療大師,它會很快撫平你心靈上累累的傷痕。很多年以后你想起來時,那些在你生命中洶涌來往的人群至多是個模糊的影子或者毫無意義的名字
          posts - 105,  comments - 171,  trackbacks - 0
           1 package org.tatan.mail;
           2 
           3 import javax.mail.Session;
           4 import javax.mail.MessagingException;
           5 import javax.mail.Multipart;
           6 import javax.mail.Transport;
           7 import javax.mail.internet.InternetAddress;
           8 import javax.mail.internet.MimeMessage;
           9 import javax.mail.internet.MimeBodyPart;
          10 import javax.mail.internet.MimeMultipart;
          11 import javax.activation.FileDataSource;
          12 import javax.activation.DataHandler;
          13 
          14 
          15 public class SendAttachMail {
          16     public static void sendMessage(String smtpHost,
          17                                    String from, String to,
          18                                    String subject, String messageText,
          19                                    String fileName)
          20             throws MessagingException {
          21 
          22         // Step 1:  Configure the mail session
          23         java.util.Properties props = new java.util.Properties();
          24         props.setProperty("mail.smtp.auth""true");//指定是否需要SMTP驗證
          25         props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服務器
          26         props.put("mail.transport.protocol""smtp");
          27         Session mailSession = Session.getDefaultInstance(props);
          28         mailSession.setDebug(true);//是否在控制臺顯示debug信息
          29 
          30         // Step 2:  Construct the message
          31         System.out.println("Constructing message -  from=" + from + "  to=" + to);
          32         InternetAddress fromAddress = new InternetAddress(from);
          33         InternetAddress toAddress = new InternetAddress(to);
          34 
          35         MimeMessage testMessage = new MimeMessage(mailSession);
          36         testMessage.setFrom(fromAddress);
          37         testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
          38         testMessage.setSentDate(new java.util.Date());
          39         testMessage.setSubject(subject);
          40 
          41         //  Step 3:  Create a body part to hold the "text" portion of the message
          42         System.out.println("Constructing 'text' body part");
          43         MimeBodyPart textBodyPart = new MimeBodyPart();
          44         textBodyPart.setContent(messageText,"text/html;charset=gb2312");
          45 
          46         //  Step 4:  Create a body part to hold the "file" portion of the message
          47         System.out.println("Attaching 'file' body part: " + fileName);
          48         MimeBodyPart fileBodyPart = new MimeBodyPart();
          49         FileDataSource fds = new FileDataSource("c:\\a.rar");
          50         fileBodyPart.setDataHandler(new DataHandler(fds));
          51         fileBodyPart.setFileName(fds.getName());
          52         System.out.println("Finished attaching file");
          53 
          54         // Step 5:  Create a Multipart/container and add the parts
          55         Multipart container = new MimeMultipart();
          56         container.addBodyPart(textBodyPart);
          57         container.addBodyPart(fileBodyPart);
          58 
          59         // Step 6:  Add the Multipart to the actual message
          60         testMessage.setContent(container);
          61         System.out.println("Message constructed");
          62 
          63         // Step 7:  Now send the message
          64         Transport transport = mailSession.getTransport("smtp");
          65         transport.connect(smtpHost, "webmaster""password");
          66         transport.sendMessage(testMessage, testMessage.getAllRecipients());
          67         transport.close();
          68 
          69 
          70         System.out.println("Message sent!");
          71     }
          72 
          73     public static void main(String[] args) {
          74 
          75         String fileName = "a.rar";
          76         String smtpHost = "localhost";
          77         String from = "webmaster@mymail.com";
          78         String to = "mfc42d@sohu.com";
          79         String subject = "html郵件附件測試"//subject javamail自動轉碼
          80          StringBuffer theMessage = new StringBuffer();
          81         theMessage.append("<h2><font color=red>這倒霉孩子</font></h2>");
          82         theMessage.append("<hr>");
          83         theMessage.append("<i>年年失望年年望</i>");
          84 
          85         try {
          86             SendAttachMail.sendMessage(smtpHost, from, to, subject, theMessage.toString(), fileName);
          87         }
          88         catch (javax.mail.MessagingException exc) {
          89             exc.printStackTrace();
          90         }
          91     }
          92 }
          93 
          94  
          95 
          96 
          posted on 2005-11-03 19:34 老妖 閱讀(419) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2005年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          隨筆分類(48)

          隨筆檔案(104)

          好友鏈接

          我的豆瓣

          積分與排名

          • 積分 - 221084
          • 排名 - 257

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 且末县| 化州市| 车险| 晋城| 恩施市| 灵璧县| 株洲市| 修武县| 朝阳市| 麦盖提县| 延长县| 丰都县| 清远市| 繁昌县| 彰武县| 苗栗县| 庆城县| 平潭县| 屏东市| 栾川县| 合阳县| 睢宁县| 荣成市| 察哈| 吉林省| 许昌县| 富宁县| 临城县| 龙川县| 凤庆县| 会宁县| 文水县| 沁阳市| 马尔康县| 呼图壁县| 裕民县| 孝感市| 宁波市| 高平市| 雷州市| 南通市|