2016-03-15 64 views
0

我的下面的Java類必須使用java郵件將文本文件內容和郵件以及附加的xls文件複製到收件人。JAVA MAIL API:無法將xls文件附加到郵件

現在我可以閱讀併發送文本文件內容但無法附加xls文件。

下面是我的代碼片段:

static void sendmail() throws IOException,  
MessagingException,AddressException,FileNotFoundException 
    { 

      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy 
HH:mm:ss"); 
      Calendar cal = Calendar.getInstance(); 
      String to1=getOrderinfo.to1; 
      String to2 = getOrderinfo.to2; 
      String to3 = getOrderinfo.to3; 
      String to4 = getOrderinfo.to4; 
      String from =getOrderinfo.from; 
      String host = getOrderinfo.host; 
      Properties properties = System.getProperties(); 
      properties.setProperty("mail.smtp.host", host); 
      Session session = Session.getDefaultInstance(properties); 
      MimeMessage message = new MimeMessage(session); 
      Multipart multipart = new MimeMultipart(); 



      String pathLogFile = "E:/car_failed_report/log.txt"; 

      try { 

        message.setFrom(new InternetAddress(from)); 
        message.addRecipient(Message.RecipientType.TO, new 
InternetAddress(to1)); 
        message.setSubject(" CAR NOT YET INTEGRATED REPORT at 
: "+dateFormat.format(cal.getTime())); 
        StringBuffer sb = new StringBuffer(); 
        FileInputStream fstream = new 
FileInputStream(pathLogFile); 
        BufferedReader br = new BufferedReader(new 
InputStreamReader(fstream)); 

        String singleLine; 
        while ((singleLine = br.readLine()) != null) 
        { 

         sb.append(singleLine + "<br>"); 
         // CarParser1.sb1.append(singleLine +"<br>"); 
        } 
        br.close(); 
        String allLines; 

        allLines = sb.toString(); 


        String allLines_html=" <html><head><title></title> 
</head>" 
          + "<body >"+allLines+"</body ></html>"; 
        message.setContent(allLines_html, "text/html; 
charset=ISO-8859-1"); 

       MimeBodyPart attachPart = new MimeBodyPart(); 
       File attachement = new File("E:\\car_failed_report 
\\failed.xls"); 
       if (attachement.exists()) { 
        attachPart.attachFile(attachement); 
       } else { 
        System.out.println("ERROR READING THE FILE"); 
        throw new FileNotFoundException(); 
       } 

       Transport.send(message); 



       System.out.println("Email Sent successfully...."); 


       System.out.println(); 

      } 
      catch(FileNotFoundException f1) 
      { 
       System.out.println("File not yet created..this is from 
mailer class"); 

       return; 
      } 
      catch (MessagingException mex) 
      { 
       System.out.println("Invalid Email Address.please provide 
a valid email id to send with"); 
       mex.printStackTrace(); 


      } 

任何一個可以幫助我附上xls文件?

在此先感謝。

+0

抱歉,但如果你alread知道的問題,解決它......我出去了。附加一個文件即使打印它也沒有閱讀它,或者設置爲正文內容......但如果你不關心我們的建議,我不能幫助。 –

+0

從本教程http://www.mkyong.com/spring/spring-sending-e-mail-with-attachment/獲取想法,並檢查是否可以發送xls文件。 – Lucky

+0

@ jordi:謝謝你寶貴的時間 –

回答

1

您使用附件創建了正文部分,但未將其添加到消息中。更換你的代碼開始message.setContent:

  MimeMultipart mp = new MimeMultipart(); 
      MimeBodyPart body = new MimeBodyPart(); 
      body.setText(allLines_html, "iso-8859-1", "html"); 
      mp.addBodyPart(body); 

      MimeBodyPart attachPart = new MimeBodyPart(); 
      File attachement = new File("E:\\car_failed_report\\failed.xls"); 
      if (attachement.exists()) { 
       attachPart.attachFile(attachement); 
       mp.addBodyPart(attachPart); 
      } else { 
       System.out.println("ERROR READING THE FILE"); 
       throw new FileNotFoundException(); 
      } 
      message.setContent(mp); 
+0

太棒了。你節省了我很多時間....它工作正常。 –