发送消息会导致异常

时间:2010-01-19 19:49:12

标签: java exception javamail

我正在尝试使用Javamail API发送消息,并将tomcat作为Web服务器,但是当我尝试发送没有文件和附件的消息时,以下代码导致了一个很大的异常。虽然它可以作为附件使用邮件。

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

我的问题是,在将任何文件作为附件之前,我在上面的代码中使用了一个条件,那么为什么我会得到那个例外?

3 个答案:

答案 0 :(得分:4)

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

具体说if (file != null || file != " ")与说if (true)相同,因为您使用了OR运算符,因为file不能具有值“”并且同时为null条件将评估为真,使整个表达式成立。

顺便说一句,file != " "是不好的形式。在尝试使用试用版进行相等性测试时,您应始终使用equals()方法,而不是==!=运算符。

答案 1 :(得分: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()的参数是什么。

答案 2 :(得分:-1)

技巧是嵌套异常:

  

引起:java.io.FileNotFoundException:C:\ Program Files \ Apache Software Foundation \ 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

有关更有意义的信息,您可以将send包装在try / catch块中以打印出更多信息:

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