隨筆 - 9, 文章 - 1, 評論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          javamail發(fā)送附件 源碼 [轉(zhuǎn)貼 2006-02-26 23:50:45 ] 發(fā)表者: 蒙面豬頭

          在網(wǎng)上找了好多例子進(jìn)行實驗發(fā)現(xiàn)大多都不好用 因為現(xiàn)在的郵件服務(wù)器很少有不需要驗證的 而網(wǎng)上提供的代碼例子大都用的是無需驗證郵件服務(wù)器提供的方法 下面是我一個測試無誤的例子供大家參考

           

          package untitled4;

          import java.util.Properties;
          import javax.mail.*;
          import javax.mail.internet.*;
          import javax.activation.*;

           
          class AttachExamplea6 {
            
          public static void main (String args[])
                
          throws Exception {
              String host 
          = "smtp.tom.com";//郵件服務(wù)器的SMTP服務(wù)  我注冊的是www.tom.com信箱服務(wù)器
              String from = "caoshibin@tom.com";//從哪個信箱發(fā)送 該信箱一般必須為郵件服務(wù)器提供注冊的信箱
              String to = "andisun@163.com";//發(fā)送給誰
              String fileAttachment = "D:\\123123.txt";//要發(fā)送附件的位置
              System.setProperty("mail.smtp.auth","true");//這句話必須加 否則服務(wù)器驗證一般都會提示驗證失敗 這個讓我苦惱了不少時間
              
          // Get system properties
              Properties props = System.getProperties();

              
          // Setup mail server
              props.put("mail.smtp.host", host);

              
          // Get session
              Session session =
                Session.getInstance(props, 
          null);

              
          // Define message
              MimeMessage message =
                
          new MimeMessage(session);
              message.setFrom(
                
          new InternetAddress(from));
              message.addRecipient(
                Message.RecipientType.TO,
                
          new InternetAddress(to));
              message.setSubject(
                
          "Hello JavaMail Attachment");

              
          // create the message part
              MimeBodyPart messageBodyPart =
                
          new MimeBodyPart();

              
          //fill message
              messageBodyPart.setText("Hi");//郵件正文內(nèi)容

              Multipart multipart 
          = new MimeMultipart();
              multipart.addBodyPart(messageBodyPart);

              
          // Part two is attachment
              messageBodyPart = new MimeBodyPart();
              DataSource source 
          =
                
          new FileDataSource(fileAttachment);
              messageBodyPart.setDataHandler(
                
          new DataHandler(source));
              messageBodyPart.setFileName(fileAttachment);
              multipart.addBodyPart(messageBodyPart);

              
          // Put parts in message
              message.setContent(multipart);
              message.saveChanges();
              Transport transport 
          = session.getTransport("smtp");
                     
          // transport.connect();
                      transport.connect(host, 25"caoshibin""wangyss");//發(fā)信服務(wù)器,自己信箱的用戶名,密碼
                      transport.sendMessage(message, message.getAllRecipients());
                      transport.close();

              
          // Send the message
           
          //   Transport.send( message );
            }

          }


          posted on 2007-04-06 15:20 趙貴陽 閱讀(434) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 汝城县| 井陉县| 宁夏| 正阳县| 岳阳市| 新龙县| 台州市| 崇阳县| 临朐县| 汤原县| 贵德县| 乌苏市| 彭泽县| 西丰县| 汉沽区| 公安县| 大余县| 永顺县| 宜阳县| 扎囊县| 卓尼县| 清远市| 大田县| 旅游| 深泽县| 皮山县| 金塔县| 陕西省| 潮州市| 新干县| 宁德市| 会理县| 繁峙县| 大宁县| 闻喜县| 西和县| 和硕县| 襄垣县| 焉耆| 韶山市| 广南县|