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

          javamail發送附件 源碼 [轉貼 2006-02-26 23:50:45 ] 發表者: 蒙面豬頭

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

           

          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";//郵件服務器的SMTP服務  我注冊的是www.tom.com信箱服務器
              String from = "caoshibin@tom.com";//從哪個信箱發送 該信箱一般必須為郵件服務器提供注冊的信箱
              String to = "andisun@163.com";//發送給誰
              String fileAttachment = "D:\\123123.txt";//要發送附件的位置
              System.setProperty("mail.smtp.auth","true");//這句話必須加 否則服務器驗證一般都會提示驗證失敗 這個讓我苦惱了不少時間
              
          // 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");//郵件正文內容

              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");//發信服務器,自己信箱的用戶名,密碼
                      transport.sendMessage(message, message.getAllRecipients());
                      transport.close();

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

          }


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


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


          網站導航:
           
          主站蜘蛛池模板: 建始县| 宁陕县| 雅江县| 米泉市| 墨玉县| 包头市| 淄博市| 凤庆县| 青海省| 唐山市| 广宗县| 林州市| 交城县| 那曲县| 广德县| 霞浦县| 玛沁县| 衡南县| 山西省| 乌鲁木齐市| 玉门市| 开封市| 壶关县| 舟曲县| 榆树市| 抚州市| 思茅市| 西青区| 呼伦贝尔市| 绿春县| 沽源县| 鄂伦春自治旗| 尼木县| 刚察县| 玛沁县| 女性| 平凉市| 海南省| 宜良县| 鹤壁市| 金华市|