modifica l'esempio in modo che venga aggiunto un part per ogni file che vuoi allegare..., da me questa funziona
codice:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class HtmlImageExample {
public static void main (String args[]) throws Exception {
String host = "tuo.smtp.com";
String from = "sender@tuosito.com";
String to = "repicient@altrosito.com";
/*
* queste le ho aggiunte per comodità,
* vedrai tu come generare o pescare
* i percorsi dei file da allegare */
String basePath = "C:/Documents and Settings/Andrea/Desktop/";
String[] file = {basePath+"oldpic.jpg", basePath+"nemo.jpg"};
String[] cids = {"picone", "pictwo"};
/* anche i cid li puoi generare a caso */
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject("Final");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the HTML content, be sure it references the attachment
String htmlText = "<H1>Hello</H1>";
for (int i = 0; i < cids.length; i++) {
htmlText+="
<img src=\"cid:"+cids[i]+"\" /></p>\n";
}
// Set the content of the body part
messageBodyPart.setContent(htmlText, "text/html");
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Add body part to multipart
multipart.addBodyPart(messageBodyPart);
DataSource fds;
for (int i= 0; i < file.length; i++) {
// Create part for the image
messageBodyPart = new MimeBodyPart();
// Fetch the image and associate to part
fds = new FileDataSource(file[i]);
messageBodyPart.setDataHandler(new DataHandler(fds));
// Add a header to connect to the HTML
messageBodyPart.setHeader("Content-ID","<"+cids[i]+">");
// Add part to multi-part
multipart.addBodyPart(messageBodyPart);
}
// Associate multi-part with message
message.setContent(multipart);
// Send message
Transport.send(message);
}
}