Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Multiple attachments in email

Featured Replies

THe following code is an adaptation of the standard stuff supplied with ScriptMaster. I adapted it to yield a second attachment:

import javax.mail.*;

import javax.mail.internet.*;



Properties props = new Properties();

props.setProperty("mail.smtp.host", smtpHost);



MimeMessage msg = new MimeMessage(Session.getInstance(props));

Multipart content = new MimeMultipart();

// first the message body

MimeBodyPart bodyPart = new MimeBodyPart();

bodyPart.setText(body, "UTF-8");

content.addBodyPart(bodyPart);

// then the 1st attachment

MimeBodyPart attachmentPart = new MimeBodyPart();

attachmentPart.attachFile(attachmentPath);

content.addBodyPart(attachmentPart);

// then the 2nd attachment

MimeBodyPart attachmentPart2 = new MimeBodyPart();

attachmentPart2.attachFile(attachmentPath2);

content.addBodyPart(attachmentPart2);

msg.setContent(content);

msg.setSubject(subject);

msg.setFrom(new InternetAddress(from));

msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

Transport.send(msg);

return true

This is just a brute force method as I have added the second attachment as another argument for the function.

What I want to do is provide for a variable number of attachments, between 0 and n, but sadly my Groovy/Java skills are non-existant. Can anyone show how to modify this code using a conditional loop structure to correctly add the variable number of attachments. Assume that the filepaths are in a return delimeted list.

Many thanks.

Hello,

If the parameter field "attachmentPath" contains a return delimited list of file paths, invoking the Groovy "each" method or a `for' loop should do the trick!


import javax.mail.*

import javax.mail.internet.*



Properties props = new Properties()

props.setProperty "mail.smtp.host", smtpHost



msg = new MimeMessage(Session.getInstance(props))

content = new MimeMultipart()



// body

bodyPart = new MimeBodyPart()

bodyPart.setText body, "UTF-8"

content.addBodyPart bodyPart



// attachments

if (attachmentPath != null){

  // Groovy «each» method

  attachmentPath.tokenize("\n").each{it ->

    attachmentPart = new MimeBodyPart()

    attachmentPart.attachFile it

    content.addBodyPart attachmentPart

  }



  /*

  // Groovy «for» method

  for (it in attachmentPath.tokenize("\n")) {

    attachmentPart = new MimeBodyPart()

    attachmentPart.attachFile it

    content.addBodyPart attachmentPart

  }

  */



  /*

  // à la Java

  // build an ArrayList

  count = fileArray.size()

  // ArrayLists are 0 based

  for(int i=0;i<count;i++){

    attachmentPart = new MimeBodyPart()

    attachmentPart.attachFile fileArray[i]

    content.addBodyPart attachmentPart

  }

  */

}

msg.content = content

msg.subject = subject

msg.from = new InternetAddress(from)

msg.setRecipient Message.RecipientType.TO, new InternetAddress(to)

Transport.send msg

return true

Clem

  • 2 weeks later...
  • Author

Brilliant Clem - works a treat. Used the "for" method. Cheers! :) :) :)

I've also managed to get multiple recipients using a java array method (from samples on the net), but couldn't seem to apply it to the attachments.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.