In allegato un esempio funzionante anche se forse un po' diverso dal tuo.... prova un po' a guardare


//
// da http://www.javacommerce.com/articles/sendingmail.htm
//

package com.xxx.yyy;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendMail {

private String smtpServer;
private String subject;
private String from;
private String[] recipients;
private String message;
private String[] filename;

public SendMail() {
this.smtpServer = "xxx.yyy.zzz.hhh";
this.subject = "prova";
this.from = "indirizzo.da@zzzzzzz.it";
recipients = new String[1];
this.recipients[0] = "indirizzo.a@zzzzzzz.it";
this.message = "ciao";
filename = new String[1];
this.filename[0] = "f:\\nome-cartella\\nome-file.txt";
}

public SendMail(String asmtpServer, String aSubject, String aFrom, String aRecipients[], String aMessage , String aFileName[]) {
this.smtpServer = asmtpServer;
this.subject = aSubject;
this.from = aFrom;
// Verifico quanti indirizzi not null o vuoti
int j=0;
for (int i = 0; i < aRecipients.length; i++) {
if ((aRecipients[i]!=null) && (!aRecipients[i].trim().equals(""))) {
j = j + 1;
}
}
recipients = new String[j];
j = 0;
for (int i = 0; i < aRecipients.length; i++) {
if ((aRecipients[i]!=null) && (!aRecipients[i].trim().equals(""))) {
this.recipients[j] = aRecipients[i];
j = j + 1;
}
}
if (j==0) {
recipients = null;
}
this.message = aMessage;
// Verifico quanti file not null o vuoti
j=0;
for (int i = 0; i < aFileName.length; i++) {
if ((aFileName[i]!=null) && (!aFileName[i].trim().equals(""))) {
j = j + 1;
}
}
filename = new String[j];
j = 0;
for (int i = 0; i < aFileName.length; i++) {
if ((aFileName[i]!=null) && (!aFileName[i].trim().equals(""))) {
this.filename[j] = aFileName[i];
j = j + 1;
}
}
}

public void postMail() throws MessagingException {
boolean debug = false;

// Se non ho destinatari esco senza fare niente
if (recipients == null) {
return;
}

//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);

// create a message
MimeMessage msg = new MimeMessage(session);

// set the from address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

// set the to address
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// set the Subject
msg.setSubject(subject);

// Create Multi-Part container
Multipart multipart = new MimeMultipart();

// Create bodyPart
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message in bodyPart
messageBodyPart.setText(message);
// Add the bodyPart
multipart.addBodyPart(messageBodyPart);

// Reference new bodyPart - atrtachement
for (int i = 0; i < filename.length; i++) {
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename[i]);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename[i]);
// Add the second bodyPart - attachement
multipart.addBodyPart(messageBodyPart);
}

// Put parts in message
msg.setContent(multipart);

// Send message
Transport.send(msg);
}

}