Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 559489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:14:35+00:00 2026-05-13T12:14:35+00:00

I’m trying to send a message by using Javamail API, with tomcat as a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:14:36+00:00Added an answer on May 13, 2026 at 12:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.