简体   繁体   中英

sending mail from your computer using java what required?

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)

Code:

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.

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:

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:

    Control panel > Programs > Turn windows features on or off

    Check "Telnet Server" AND "Telnet client"

  3. Make sure your system has an accurate date

  4. And of course an internet connection is a must

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM