dansouza 18 de set. de 2008
andrero 18 de set. de 2008
Muito obrigado!!!
Só postando como eu fiz e deu certo:
adicionei no meu context.xml
< Resource name = "mail/Email" auth = "Container"
type = "javax.mail.Session"
username = "<usuario>"
password = "<senha>"
mail . debug = "false"
mail . user = "<usuario>"
mail . to = "<e-mail destinatario>"
mail . transport . protocol = "smtp"
mail . mime . charset = "ISO-8859-1"
mail . smtp . host = "smtp.gmail.com"
mail . smtp . auth = "true"
mail . smtp . port = "25"
mail . smtp . ssl = "true"
mail . smtp . starttls . enable = "true"
/ >
adicionei no meu web.xml:
<resource-ref>
<res-ref-name> mail/Email</res-ref-name>
<res-type> javax.mail.Session</res-type>
<res-auth> Container</res-auth>
</resource-ref>
minha classe:
import java.util.* ;
import javax.naming.InitialContext ;
import javax.naming.Context ;
import javax.mail.* ;
import javax.mail.internet.MimeMessage ;
import javax.naming.NamingException ;
import javax.mail.internet.InternetAddress ;
public class SendEmail {
private String erro , emailAluno , mensagem ;
public SendEmail ( String e ){
emailAluno = e ;
erro = "" ;
}
public String getErro (){ return erro ;}
public void setMensagem ( String m ){
mensagem = m ;
}
public void enviaEmail (){
try {
Context ctx = new InitialContext ();
Session mailSession = ( Session ) ctx . lookup ( "java:comp/env/mail/Email" );
MimeMessage msg = new MimeMessage ( mailSession );
InternetAddress from = new InternetAddress ( emailAluno );
msg . setFrom ( from );
InternetAddress to = new InternetAddress ( mailSession . getProperty ( "mail.to" ));
msg . setRecipient ( Message . RecipientType . TO , to );
msg . setSubject ( "Solicitação Atestado, Histórico, etc." );
msg . setSentDate ( new Date ());
msg . setContent ( mensagem , "text/html" );
msg . saveChanges ();
Transport . send ( msg );
} catch ( NamingException ne ){
erro = ne . getMessage ();
} catch ( MessagingException me ){
erro = me . getMessage ();
} catch ( Exception e ){
erro = e . getMessage ();
}
}
}
Valeu mesmo!!