Jazzy
Jazzy

Reputation: 89

Sending message result in an exception

I'm trying to send a message by using Javamail API, with tomcat as a webserver, but the following code resulted me in a big exception whenever I try to send the message without a file an an attachment. Although it works with messages as an attachment.

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";
}

Exception:

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

My question is, I've used a condition in above code before making any file as an attachment, then why I'm getting that exception?

Upvotes: 0

Views: 5495

Answers (3)

Paul Wagland
Paul Wagland

Reputation: 29116

The trick is the nested exception:

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

What this means is that you are not allowed to open the resource as a file.

Additionally, your test:

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

does not test if the file actually exists or not, it only tests if the name is not null, or is not a particular string literal. A much better way to do this would be:

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

In general, using == for string comparison in Java is the wrong thing to do.

For more meaningful information, you could wrap the send in a try/catch block to print out more info at that point:

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

Upvotes: -1

liwp
liwp

Reputation: 6926

The nested excpeption:

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

That is, you cannot access C:\Program Files\Apache Software Foundation\Tomcat 6.0. I suggest checking your file permissions on that file. You should also tell us what the arguments to send() are when you get this exception.

Upvotes: 0

Jherico
Jherico

Reputation: 29240

if(file!=null||file!=" ") is incorrect. I suspect what you want is if (file != null && !file.trim().isEmpty()).

Specifically saying if (file != null || file != " ") is the same as saying if (true) because you've used an OR operator and since file can't have a value of " " and null simultaneously one of those conditions is going to evaluate true, making the whole expression true.

As an aside, file != " " is bad form. When testing for equality with tryings you should always use the equals() method, not the == and != operators.

Upvotes: 4

Related Questions