老妖的博客
          現實的中沒有幾個人能夠真為對方去死,甚至山盟海誓很快就會在金錢面前變的微不足道,這才是生活。沒有永遠的愛,除了你的父母對你,當然也就沒有永遠的恨,更沒有永遠的痛,時間是最好的治療大師,它會很快撫平你心靈上累累的傷痕。很多年以后你想起來時,那些在你生命中洶涌來往的人群至多是個模糊的影子或者毫無意義的名字
          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)

          好友鏈接

          我的豆瓣

          積分與排名

          • 積分 - 221059
          • 排名 - 257

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 郁南县| 凭祥市| 娄底市| 海丰县| 葫芦岛市| 桑植县| 双峰县| 盐源县| 子长县| 三台县| 乃东县| 泰安市| 望江县| 灵寿县| 新龙县| 长子县| 铜梁县| 镇沅| 德州市| 饶阳县| 海南省| 大同县| 沭阳县| 定边县| 安国市| 乐安县| 合川市| 甘孜县| 仁布县| 沾益县| 马龙县| 保山市| 武隆县| 喜德县| 东阿县| 江都市| 金平| 卓尼县| 马山县| 修水县| 阿图什市|