Envio de email com javamail e gmail

Ola turma, estou tentando criar uma classe responsavel por enviar emails e estou utilizando a ultima versao do javamail.


import java.security.Security;
import java.util.Properties;
 
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendMail {
 
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String emailMsgTxt = "Test Message Contents";
    private static final String emailSubjectTxt = "A test from gmail";
    private static final String emailFromAddress = "### LOGIN ###@gmail.com";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String[] sendTo = { "### LOGIN ###@gmail.com"};
    
    
    public static void main(String args[]) throws Exception {
 
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        new SendMail().sendSSLMessage(sendTo, emailSubjectTxt,emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
    }
 
    public void sendSSLMessage(String recipients[], String subject,
            String message, String from) throws MessagingException {
        boolean debug = true;
 
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");
 
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
 
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("### LOGIN ###", "### SENHA ###");
                    }
                });
 
        session.setDebug(debug);
 
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
 
        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);
 
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
}

so que toda vez que e ele tenta enviar retornar o erro:

DEBUG SMTP: trying to connect to host “smtp.gmail.com”, port 465, isSSL false
DEBUG SMTP: exception reading response: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
Exception in thread “main” javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at br.com.sendmail.SendMail.sendSSLMessage(SendMail.java:68)
at br.com.sendmail.SendMail.main(SendMail.java:28)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1591)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:975)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:123)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:516)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:454)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1096)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:744)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)
… 9 more
Caused by: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:251)
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:234)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:158)
at sun.security.validator.Validator.validate(Validator.java:218)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:954)
… 21 more
Caused by: java.security.cert.CertPathValidatorException: timestamp check failed
at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:139)
at sun.security.provider.certpath.PKIXCertPathValidator.doValidate(PKIXCertPathValidator.java:316)
at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:178)
at java.security.cert.CertPathValidator.validate(CertPathValidator.java:250)
at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:246)
… 28 more
Caused by: java.security.cert.CertificateNotYetValidException: NotBefore: Mon Jul 30 13:58:07 BRT 2007
at sun.security.x509.CertificateValidity.valid(CertificateValidity.java:252)
at sun.security.x509.X509CertImpl.checkValidity(X509CertImpl.java:570)
at sun.security.provider.certpath.BasicChecker.verifyTimestamp(BasicChecker.java:157)
at sun.security.provider.certpath.BasicChecker.check(BasicChecker.java:109)
at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:117)
… 32 more

alguem sabe o pq ??

Valeu

Meu modelo de envio com o gmail é o Seguinte :

Tem que ter as Bibliotecas mail.jar , activation.jar

[code]Essa é a Página de Recepção do FORM que encaminha os dados pro SERVLET e obtem o retorno se foi enviada ou não .

<%@ page contentType="text/html; charset=ISO-8859-1" %> <%
        try {
           
            String assunto = "Sugestões e Críticas ::: Ifranca";
            String para = "seuemail@gmail.com";
            String de = request.getParameter("email_email");
            String Usuario = request.getParameter("nome_email");
            String Email = request.getParameter("email_email");
            String mensagem =  request.getParameter("mensagem_email");
            String formato = "<table border='0px' ><tr><td><a href=\"http://www.ifranca.com.br\"><img src=\"http://www.ifranca.com.br/marketing/ifranca_top.jpg\"/></a></td></tr>"+
                    "<tr><td>Sugestões e Críticas...www.ifranca.com.br<br/></td></tr>"+
                    "<tr><td>De : "+de+"<br/></td></tr>"+
                    "<tr><td>Para : "+para+"<br/></td></tr>"+
                    "<tr><td>Usúario : "+Usuario+"<br/></td></tr>"+  
                    "<tr><td>Email : "+Email+"<br/></td></tr>"+    
                    "<tr><td>Mensagem : "+mensagem+"<br/></td></tr></table>"; 
           mensagem = formato;
           email.sendSimpleMail( assunto, para, de, mensagem);
    %>   
    <table width="114px"  style="color:#FFFFFF;background-color:#191919;border: 1px solid #C0C0C0" align="center">   
        <tr>   
            <td></td>   
            <td>Mensagem enviada com sucesso<td>
            
            <br/><br/><br/><br/><br/><br/><br/><br/>
        </tr>   
        <tr>   
            <td colspan="2" align="center">
                <input type="button" style="background-color: #FFFF00;font-weight:bold;border:1px solid #000000;border-style:dashed;" onclick="makeRequest29()" value="Voltar..." />
            </td>   
        </tr>   
    </table>   
    <%
    } catch (Exception e) {
    %>   
    <table width="114px" style="color:#FFFFFF;background-color:#191919;border: 1px solid #C0C0C0" align="center">   
        <tr>   
            <td></td>                                                 
            <td>   
                <p>Erro no envio da mensagem   
                <br/><br/><br/><br/><br/><br/><br/><br/>
            </td>   
        </tr>   
        <tr>   
            <td colspan="2" align="center">
                <input type="button"  style="background-color: #FFFF00;font-weight:bold;border:1px solid #000000;border-style:dashed;" onclick="makeRequest29()" value="Voltar..." />
            </td>   
        </tr>   
    </table>   
    <%
        }   
    %>  
</body>
[/code] [code] Esse é o Servlet :

package email;
import javax.mail.;
import javax.mail.internet.
;
import java.util.Properties;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.activation.*;
import com.sun.net.ssl.internal.ssl.Provider;

public class Email {

public void sendSimpleMail(String subject, String to, String from, String mensagem) throws AddressException, MessagingException {

    java.security.Security.addProvider(new Provider());

    Properties mailProps = new Properties();
    mailProps.put("mail.transport.protocol", "smtp"); // diz que utilizarei um protocolo de seguran&#65533;a   
    mailProps.put("mail.smtp.starttls.enable", "true"); // diz que utilizarei o protocolo de seguran&#65533;a TLS   
    mailProps.put("mail.smtp.host", "smtp.gmail.com"); // diz que o servidor que estou mandando &#65533; o gmail   
    mailProps.put("mail.smtp.auth", "true"); // diz que &#65533; necess&#65533;rio uma autentica&#65533;&#65533;o   
    mailProps.put("mail.smtp.user", "user@gmail.com");
    mailProps.put("mail.debug", "true");
    mailProps.put("mail.smtp.port", "465");
    mailProps.put("mail.smtp.debug", "true");
    mailProps.put("mail.mime.charset", "ISO-8859-1");
    mailProps.put("mail.smtp.socketFactory.port", "465");
    mailProps.put("mail.smtp.socketFactory.fallback", "false");
    mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    Authenticator auth = new Autenticacao();
    Session session = Session.getDefaultInstance(mailProps, auth);

    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress(from));
    message.setHeader("Disposition-Notification-To","ifrancaguia@gmail.com");
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

    message.setSubject(subject);

    message.setContent(mensagem.toString(), "text/html");

    Transport.send(message);
}

private class Autenticacao extends javax.mail.Authenticator {

    public PasswordAuthentication getPasswordAuthentication() {
        String user = "seuemail@gmail.com";
        String pwd = "suasenha";
        return new PasswordAuthentication(user, pwd);
    }
}

}

[/code]

Espero que tenha ajudado , este é o código que eu uso no meu site … sou iniciante ainda mas já aprendi um pouquinho pelo menos

cara eu copiei seu codigo trocando apenas os emails e continua dando o mesmo erro:

exception

javax.servlet.ServletException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.temp_jsp._jspService(temp_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Você habilitou o pop no Gmail ?talvez seja isso
ou talvez esteja faltando alguma biblioteca ,
verifica isso

Tente colocar essa string como seu host_smtp: gmail-smtp.l.google.com
E verifique as bibliotecas activation e mail se estão no projeto.