enviar correo desde su computadora usando java, ¿qué se requiere?

I have windows 7 system with no smtp servers installed, only java. I want to send mail from my program through gmail. I have written a java program to connect google via smtp, and have enable the telnet option, but I am getting the error below. I tried using port 465 and 587, but no change.

Error:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: not connect to SMTP host: smtp.gmail.com, port: 587;
 nested exception is:
 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at mail1.SendMailSSL.main(SendMailSSL.java:44)

Código:

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 SendMailSSL {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

       Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator()
       {
           protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication("mymail@gmail.com","mypassword");                  
           }
       } );

       try {
           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress("mymail@gmail.com"));
           message.setRecipient(Message.RecipientType.TO, new InternetAddress(
                    "to24n@gmail.com"));
           message.setSubject("Testing Subject");
           message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");
           Transport.send(message);
           System.out.println("Done");

        } catch (MessagingException e) {
           throw new RuntimeException(e);
        }
    }
}

Can anyone tell me what I need to do to send email from my personal system? Please tell me step-by-step, and include anything like installing software because I must do this for my project. Your answer is very appreciated. Thank you in advance.

preguntado el 10 de marzo de 12 a las 10:03

check if the system date in your pc is right -

A dirty (but working) solution is to change your SocketFactory by an implementation witch doesn't check the certificate chain validity. For example, you have the EasySSLProtocolSocketFactory implementation (from Apache commons-httpclient-contrib package). -

really thank to guido i made mistake that my pc had old date.. -

2 Respuestas

Required jar files:

  • MAIL.JAVR
  • JAVAEE.JAR
  • ACTIVATION.JAR
  • DNSJAVA.JAR
  • SENDAMAIL-TOMCAT-2.1.2.JAR

Steps to sending mail:

  1. Use google smtp server port 465

  2. Enable telnet in windows 7:

    Panel de control > Programas > Activar las características de Windows o desactivar

    Check "Telnet Server" AND "Telnet client"

  3. Make sure your system has an preciso datos

  4. And of course an internet connection is a must

respondido 11 mar '12, 03:03

Two years ago, I had the same problem, but I don't remember everything that helped me.

  1. I used JavaMail.
  2. In some cases you have to use 587, in some cases 465 out port.
  3. You have to allow SMTP (or POP?) in gmail settings. You have to use authorisation.

I think these posts will be helpful:

respondido 13 mar '12, 07:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.