Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 5037 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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.

Posted

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...
Posted

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.

This topic is 5037 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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