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 7737077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:54:21+00:00 2026-06-01T07:54:21+00:00

I have 2 files in the directory bar for testing. from the code below,

  • 0

I have 2 files in the directory “bar” for testing. from the code below, it is suppose to send all files in that directory as email attachments. The problem is that when I send them, I get duplicates of one file in my email. I did have it working correctly once before for testing, but I dont remember what I might have changed.

Does anyone recognize what might be wrong with my code or why I instead of sending all the files in the directory, I get one file multiple times in my email?

Here is my code:

        multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        File f = new File("C:\\foo\\bar");
        File[] attachments = f.listFiles();

         //email with attachments (if any)
        for(int i = 0; i < f.listFiles().length - 1; i++){
            DataSource fileDataSource = new FileDataSource(attachments[i]);
            messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
            messageBodyPart.setFileName(attachments[i].getName());
            multipart.addBodyPart(messageBodyPart);
            }

        message.setContent(multipart);
        message.setSentDate(new Date());

        Transport.send(message);
  • 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-06-01T07:54:22+00:00Added an answer on June 1, 2026 at 7:54 am

    You have an off-by-one error: you want the loop to be either

    for (int i = 0; i < f.listFiles().length; i++){
    

    or

    for (int i = 0; i <= f.listFiles().length -1; i++){
    

    You also have two lines that say

    multipart.addBodyPart(messageBodyPart);
    

    which is why you have the bodypart containing the first file twice.

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

Sidebar

Related Questions

Suppose I have a directory containing the files foo bar.txt foo baz.txt (each with
Suppose you have 2 files in the same directory: New File Name.txt and NewFil~1.txt
How to find all files in directory? I have HTML page in some directory.
I have a directory with files that look like this: 001_something.php 002_something_else.php 004_xyz.php 005_do_good_to_others.php
I have an application that processes files in a directory and moves them to
I have the following code to recursively search for files through a directory, which
I am trying to load all of the ruby files in a directory from
I have following files and directories in my project root directory main.py bar.py foo
I have a directory with sub-directories and files with names that start with a
I have a directory src/ that contain many .cc files and its binary. For

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.