2010-01-19 138 views
0

我試圖通過使用Javamail API,使用tomcat作爲網絡服務器發送消息,但是,下面的代碼導致我在發生無文件消息時出現大的異常附件。雖然它作爲附件與消息一起工作。發送消息導致出現異常

public static String send(String to,String body,Stringsubject,String file,String from)throws Exception{ 

        if(file!=null||file!=" "){  

     File file1=new File(file); 
     MimeBodyPart mb=new MimeBodyPart(); 
     FileDataSource f=new FileDataSource(file1.getCanonicalPath()); 
     mb.setDataHandler(new DataHandler(f)); 
     mb.setFileName(f.getName()); 
     mm.addBodyPart(mb); 
     } 

     mb1.setText(body); 
     mm.addBodyPart(mb1); 
        message.setFrom(new InternetAddress(from)); 
     Address[] add={ new InternetAddress(to) }; 

       message.setRecipients(Message.RecipientType.TO,add); 
       message.setSubject(subject); 
       message.setContent(mm); 
      //message.setText(body); 
       Transport.send(message); 
      return "Message sent"; 
} 

例外:

javax.mail.MessagingException: IOException while sending message; 
    nested exception is: 
    java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0 (Access is denied) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:779) 
    at javax.mail.Transport.send0(Transport.java:191) 
    at javax.mail.Transport.send(Transport.java:120) 
    at foo.SendMessage.send(SendMessage.java:57) 
    at foo.Mail.doPost(Mail.java:39) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) 
    at java.lang.Thread.run(Thread.java:619) 
Caused by: java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0 (Access is denied) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:106) 
    at javax.activation.FileDataSource.getInputStream(FileDataSource.java:82) 
    at javax.activation.DataHandler.writeTo(DataHandler.java:290) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1381) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) 
    at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:452) 
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) 
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:869) 
    at javax.activation.DataHandler.writeTo(DataHandler.java:302) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1381) 
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1742) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:737) 
    ... 18 more 

我的問題是,我已經用在上面的代碼中的條件作出任何文件作爲附件,那麼爲什麼我收到異常之前?

回答

4

if(file!=null||file!=" ")不正確。我懷疑你想要的是if (file != null && !file.trim().isEmpty())

具體說if (file != null || file != " ")是等於說if (true),因爲你已經使用或操作者與自file不能有「」的值,同時空的那些條件之一是要評估真實的,使得整個表達式真正。

另外,file != " "是不好的形式。當使用tryings進行測試時,應始終使用equals()方法,而不是==!=運算符。

0

嵌套excpeption:

java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0 (Access is denied) 

也就是說,你不能訪問C:\Program Files\Apache Software Foundation\Tomcat 6.0。我建議檢查您的文件在該文件上的權限。你應該告訴我們當你得到這個異常時send()的參數是什麼。

+0

我有在文件變量一個空的空間,每當我得到的例外。 – Jazzy

+0

啊,所以這回到了Jherico對破壞if條款的評論。如果if子句是正確的,即&&而不是||,那麼你的代碼永遠不會試圖添加一個文件作爲消息的附件。當您接受「」作爲文件名時,您正試圖將當前工作目錄作爲附件添加到您的電子郵件中,並導致出現異常。 – liwp

-1

訣竅是嵌入異常:

產生的原因:java.io.FileNotFoundException:C:\ Program Files文件\ Apache軟件基金會\ Tomcat的6.0(訪問被拒絕)

什麼這意味着您不允許將資源作爲文件打開。

此外,您的測試:

if(file!=null||file!=" "){  
    File file1=new File(file); 
    … 
} 

不測試,如果該文件確實存在與否,它只是測試,如果名稱不爲空,或者不是一個特定的字符串常量。一個更好的辦法做到這一點是:

if(file!=null && !file.isEmpty()){  
    File file1=new File(file); 
    … 
} 

一般情況下,使用==在Java字符串比較the wrong thing to do

更多有意義的信息,你可以包裝在一個try/catch塊發送在這一點上打印出更多的信息:

try{ 
    Transport.send(message); 
} catch (IOException e) { 
    throw new Exception("Debug: file:'" + file + "' from:'" + from + "'", e); 
} 
+0

問題不在於安全管理員;如果虛擬機不被允許讀取文件,它會拋出SecurityException。問題是他試圖用FileInputStream打開一個不允許的目錄。 – jarnbjo

+0

@jambjo yup ...足夠公平,答案更新...雖然我不確定它是不正確的,以保證倒票... –

相關問題