我的Windows 7系统没有安装smtp服务器,只有java。我想通过gmail从我的程序中发送邮件。我已经编写了一个Java程序来通过smtp连接google,并启用了telnet选项,但是出现以下错误。我尝试使用端口465和587,但没有更改。

错误:

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)


码:

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);
        }
    }
}


谁能告诉我从个人系统发送电子邮件需要做什么?请分步告诉我,并包括安装软件之类的内容,因为我必须为我的项目这样做。非常感谢您的回答。先感谢您。

最佳答案

所需的jar文件:


邮件JAVR
JAVAEE.JAR
激活罐
DNSJAVA.JAR
SENDAMAIL-TOMCAT-2.1.2.JAR


发送邮件的步骤:


使用google smtp服务器端口465
在Windows 7中启用telnet:

控制面板>程序>打开或关闭Windows功能

检查“ Telnet服务器”和“ Telnet客户端”
确保系统日期正确
当然,必须有互联网连接

关于java - 使用Java从您的计算机发送邮件有什么要求?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9645578/

10-11 22:22
查看更多