0

tomcatをWebサーバーとしてJavamailAPIを使用してメッセージを送信しようとしていますが、ファイルと添付ファイルなしでメッセージを送信しようとすると、次のコードで大きな例外が発生しました。添付ファイルとしてメッセージを処理しますが。

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

3 に答える 3

4

if(file!=null||file!=" ")間違っている。私はあなたが欲しいものはだと思いますif (file != null && !file.trim().isEmpty())

具体的に言うif (file != null || file != " ")if (true)、OR演算子を使用してfileいて、 ""とnullの値を同時に持つことができないため、これらの条件の1つがtrueと評価され、式全体がtrueになるためと同じです。

余談file != " "ですが、悪い形です。試行との同等性をテストするときは、 and演算子equals()ではなく、常にメソッドを使用する必要があります。 ==!=

于 2010-01-19T19:53:26.013 に答える
0

ネストされた例外:

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()また、この例外が発生した場合の引数も教えてください。

于 2010-01-19T19:56:06.023 に答える
-1

トリックはネストされた例外です:

原因: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で文字列の比較に使用することは間違っています

より意味のある情報については、sendをtry / catchブロックでラップして、その時点でより多くの情報を出力できます。

try{
    Transport.send(message);
} catch (IOException e) {
   throw new Exception("Debug: file:'" + file + "' from:'" + from + "'", e); 
}
于 2010-01-19T19:57:56.473 に答える