Ciao ,ho una classe java che invia mail è questo funziona, ma adesso servirebbe anche l'invio di allegati, visto che sono nuova di java ho provato ad aggiungere il codice per l'invio di allegati ma non funziona ve lo posto:
package it.lispa.rap.service.stato;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.text.DateFormat;
public class SendEmail
{
private String host;
private String emailTo;
private String emailFrom;
private String messaggio;
private String oggetto;
private int port;
public String getHost( )
{
return host;
}
public void setHost(String value )
{
this.host = value;
}
public String getEmailTo( )
{
return emailTo;
}
public void setEmailTo(String value )
{
this.emailTo = value;
}
public String getEmailFrom( )
{
return emailFrom;
}
public void setEmailFrom(String value )
{
this.emailFrom = value;
}
public String getMessaggio( )
{
return messaggio;
}
public void setMessaggio(String value )
{
this.messaggio = value;
}
public void setOggetto(String value )
{
this.oggetto = value;
}
public void setPort(int value )
{
this.port = value;
}
public boolean send( ) //throws Exception
{
/*
* se nel file web.xml il primo carattere dell'host è N
* allora significa che ho disattivato l'invio delle email
*/
if (this.host.charAt(0)=='N')
{
return true;
}
Properties props = new Properties();
props.put("mail.smtp.host", this.host);
props.put("mail.debug", "false");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
try
{
Multipart multipart = new MimeMultipart();
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.emailFrom));
InternetAddress[] address = {new InternetAddress(this.emailTo)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(this.oggetto);
msg.setSentDate(new Date());
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(this.messaggio);
////Invio allegato
// crea l'allegato Word
DataSource source = new FileDataSource( "c:/Documento.doc" );
BodyPart messageBodyPart2 = new MimeBodyPart();
msg.setDataHandler( new DataHandler(source) );
msg.setFileName( "Documento.doc" );
// aggiunge le parti all'oggetto multipart
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart( messageBodyPart2 );
// imposta come contenuto del messaggio l'oggetto multipart
msg.setContent(multipart);
////fine invio allegato
Transport.send(msg);
return true;
}
catch (MessagingException mex)
{
// System.out.println("\n--Exception handling in SendEmail.java");
mex.printStackTrace();
Exception ex = mex;
do
{
if (ex instanceof SendFailedException)
{
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null)
{
System.out.println(" ** Invalid Addresses");
if (invalid != null)
{
for (int i = 0; i < invalid.length; i++)
System.out.println(" " + invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null)
{
// System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null)
{
for (int i = 0; i < validUnsent.length; i++)
System.out.println(" "+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null)
{
// System.out.println(" ** ValidSent Addresses");
if (validSent != null)
{
for (int i = 0; i < validSent.length; i++)
System.out.println(" "+validSent[i]);
}
}
}
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
return false;
}
}
}