Ciao....prova con questo codice.... io non l'ho usato ancora ma potrebbe andare:
codice:
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator{
private PasswordAuthentication passwordAutentication = new PasswordAuthentication("nome.cognome@email.it", "password");
public MyAuthenticator(){
}
public PasswordAuthentication getPasswordAuthentication() {
return passwordAutentication;
}
}
codice:
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.MessageResources;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class EmailAction extends Action{
/**
* Oggetto properties tramite cui settare la sessione
*/
private Properties pro;
/**
* Oggetto sessione
*/
private Session sessione;
/**
* Oggetto messaggio
*/
private Message messaggio;
/**
* Indirizzo e-mail mittente
*/
private InternetAddress da;
/**
* Indirizzi e-mail destinatari
*/
private InternetAddress[] a;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String smtp = "smtp.email.it";
this.setProperties(smtp);
sendMessaggio("nome.cognome@email.it", "nome.cognome@email.it", "Oggetto", "testo messaggio", false);
return new ActionForward("home.kibuz", true);
}
/**
* Costruttore della classe che setta, nelle properties, l'smtp utilizzato
*
* La spedizione di mail con l'smtp di email.it richiede l'autenticazione quindi è necessario inserire la riga pro.put("mail.smtp.auth", "true");
*
* @param po -l'smtp utilizzato per inviare il messaggio
*/
public void setProperties(String po){
pro = System.getProperties();
pro.put("mail.smtp.host", po);
// ****************************************
****************************************
**************
pro.put("mail.smtp.auth", "true");
// ****************************************
****************************************
**************
}
/**
* Metodo che si occupa di settare la sessione
*/
private void setSessione(){
// ****************************************
****************************************
**************
javax.mail.Authenticator auth = new MyAuthenticator(); // this class return a new PasswordAuthentication(usr, pwd) object
sessione = javax.mail.Session.getInstance(pro, auth);
// ****************************************
****************************************
**************
sessione.setDebug(true);
}
/**
* Metodo che si occupa di settare, inizializzare il messaggio e di inviarlo
* @param daChi -mittente
* @param aChi -destinatari
* @param oggetto -oggetto e-mail
* @param testoMessaggio -testo dell'email
* @param autoritation -true se bisogna autorizzarsi, false altrimenti
* @throws AddressException eccezione che si può verificare se l'indirizzo e-mail
* dovesse essere non corretto
* @throws MessagingException eventuale altra eccezione
*/
public void sendMessaggio( String daChi, String aChi, String oggetto, String testoMessaggio, boolean autoritation ){
try{
setSessione();
String host = pro.getProperty("mail.smtp.host");
System.out.println( "["+host+"]" );
Transport t = sessione.getTransport( "smtp" );
messaggio = new MimeMessage(sessione);
da = new InternetAddress(daChi);
a = InternetAddress.parse(aChi);
messaggio.setFrom(da);
messaggio.setRecipients(Message.RecipientType.TO, a);
messaggio.setSubject(oggetto);
messaggio.setSentDate(new Date());
messaggio.setText(testoMessaggio);
//messaggio.setDataHandler(this.allegato);
// il controllo sull'autoritation non è necessario.
// il metodo send chiama anche il metodo connect
/*
if( autoritation ){
String log = "caterina.curti";
String psw = "kiuhyewpo";
t.connect( host, log, psw );
}else{
t.connect();
}
*/
t.send( messaggio );
t.close();
}catch( AddressException ae ){
ae.printStackTrace();
}catch( MessagingException me ){
me.printStackTrace();
}
}
}