簡體   English   中英

發送消息導致異常

[英]Sending message result in an exception

我正在嘗試使用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

我的問題是,在將任何文件作為附件之前,我已經在上面的代碼中使用了條件,那么為什么會出現該異常?

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

具體來說,說if (file != null || file != " ")與說if (true) if (file != null || file != " ")是相同的,因為您使用了OR運算符,並且由於file不能具有值“”並且同時將null之一這些條件將評估為真,從而使整個表達式為真。

順便說一句, file != " "是錯誤的格式。 在嘗試進行相等性測試時,應始終使用equals()方法,而不要使用==!=運算符。

嵌套的例外:

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()的參數是什么。

訣竅是嵌套異常:

引起原因:java.io.FileNotFoundException:C:\\ Program Files \\ Apache Software Foundation \\ Tomcat 6.0(拒絕訪問)

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

此外,您的測試:

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

不會測試文件是否實際存在,只會測試名稱是否不為null或不是特定的字符串文字。 更好的方法是:

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

通常,在Java中使用==進行字符串比較是錯誤的做法

有關更多有意義的信息,您可以將發送包裝在try / catch塊中,以在此時打印出更多信息:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM