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

  • Home
  • SEARCH
  • 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 8504997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:11:42+00:00 2026-06-11T02:11:42+00:00

I have the following code to send an email with multiple parts public void

  • 0

I have the following code to send an email with multiple parts

 public void sendEmail(String emailAddress, List<String> attachment) throws Exception{
    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(true);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    // message subject
    message.setSubject("Automated email from Kieran Herley about Assignments");

    message.addRecipient(Message.RecipientType.TO,
         new InternetAddress(emailAddress));

    Multipart multipart = new MimeMultipart();
    MimeBodyPart messageBodyPart = new MimeBodyPart();

    // message body
    messageBodyPart.setText("This is just a message to say your assignment has been graded.\nAttached is a file with some pointers about your assignment");
    multipart.addBodyPart(messageBodyPart);

    messageBodyPart = new MimeBodyPart();

    for (String singleFile : attachment) {
        DataSource source = new FileDataSource(singleFile);
        messageBodyPart.setDataHandler(new DataHandler(source));
        String nameOfFile = singleFile.substring(singleFile.lastIndexOf('\\') + 1);
        messageBodyPart.setFileName(nameOfFile);
        multipart.addBodyPart(messageBodyPart);
    }
    message.setContent(multipart);


    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

    transport.sendMessage(message,
        message.getRecipients(Message.RecipientType.TO));
    transport.close();
}

Everything is working fine but the problem i am having is in the for-loop

for (String singleFile : attachment) {
        DataSource source = new FileDataSource(singleFile);
        messageBodyPart.setDataHandler(new DataHandler(source));
        String nameOfFile = singleFile.substring(singleFile.lastIndexOf('\\') + 1);
        messageBodyPart.setFileName(nameOfFile);
        multipart.addBodyPart(messageBodyPart);
    }
    message.setContent(multipart);

The problem that i am having is, if the application loops through the for-loop 3 times, it will name all the files attached to the email the name of the file that was passed through the loop the third time. This is also the same with the email attachment itself, even do it is sending 3 attachments, they are all named the same.

This is because all three of the attachments are using the same variable to both name them and retrieve so for all three of them it is naming them and setting them as the last one passed through the loop.

Is there a way to set the name and attachment of each email by using the same name variable and attachment variable for all, or how would I do this?

In this case use

String singleFile --- for the attachment
String nameOfFile --- for the name of each file
  • 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-11T02:11:43+00:00Added an answer on June 11, 2026 at 2:11 am

    This looks like nothing wrong with strings in for-each loop. My concern that you always using the same instance of messageBodyPart when assign datahandler and filename to it. Try to move messageBodyPart = new MimeBodyPart() inside the loop.

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

Sidebar

Related Questions

I have the following code: public static void Send(this MailMessage email) { if (!isInitialized)
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have the following code to send an email to the users. $mes_body.=Click to
I want to send an email with an attachment using the following code (Python
I am trying to send a simple email through the following code: protected void
I have the following code to send users a verification email, which contains some
I have the following code which is attempting to send an email in background.
I have the following code which works fine, but it doesn't send the attachment
I have following class to send email global class SendConfirmation { public SendConfirmation(ApexPages.StandardController controller)
I have to written the following code to send a mail using PHP in

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.