Skip to end of metadata
Go to start of metadata

This code snippet can be used to trigger email with multiple attachments to multiple receivers.

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
* *********** Java Email with Multiple Attachments to Multiple Recipients *****************************************
* @author Mandeep Virk    / Yogesh Varma    (mandeep.virk@architectsap.com / yogesh.varma@architectsap.com)
* *****************************************************************************************************
*/

String      hostName = "Put your Host Name";
String      mailFrom = "Put Sender's Address";
String[]   mailTo   =  "Put Multiple Recipient's Address";
String      portNo   = "Put Port Number";

Properties props = System.getProperties();
Session session  = Session.getDefaultInstance(props,null);

try{

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(mailFrom));

message.setSubject("Put Your Text Here");

// Email to Multiple Recipients 

InternetAddress[] mailToMultiple = new InternetAddress[mailTo.length];

for( int i=0; i<mailTo.length; i++ ){
mailToMultiple[i] = new InternetAddress(mailTo[i]);
}
message.setRecipients(Message.RecipientType.TO,mailToMultiple );

// For Multiple Attachments

MimeMultipart multipart = new MimeMultipart();
IWDResource resource = null;
String filePath = "Enter Your File Path Here";

for(int i=0; i<wdContext.node<NodeName>().size(); i++){
    BodyPart attachment_bodypart = new MimeBodyPart();     
    if(wdContext.node<NodeName>().get<NodeName>ElementAt( i ).getResource() != null)
    { // Here getResource() is Value Attribute of Resource Type 
resource = wdContext.node<NodeName>().get<NodeName>ElementAt( i ).getResource();
String fileName = resource.getResourceName();
if ((filePath != "") || (filePath != null))

Unknown macro: {if(new File(filePath).exists()){ File f = new File(filePath + fileName) ; attachment_bodypart.setFileName( fileName ); attachment_bodypart.setDataHandler( new DataHandler( new FileDataSource( filePath + fileName))); multipart.addBodyPart( attachment_bodypart ); }

        } resource = null; } }

//  Email body

BodyPart message_bodypart = new MimeBodyPart();
message_bodypart.setContent("Put Your Message Here", "text/html");
multipart.addBodyPart( message_bodypart); message.setContent( multipart);

// Transport Message

Transport transport = session.getTransport("smtp");
transport.connect( host,portNo,"Put Email Id for Authentication","Password");
transport.sendMessage( message, message.getAllRecipients()); transport.close(); }

catch (AddressException e) {

} catch (MessagingException e){

}

NOTE :  This code can also be used for sending email to single recipient with only one attachment.User just need to remove FOR loops from respective sections of code.