"找不到主要课程。该计划将退出"

时间:2017-12-05 05:05:14

标签: java eclipse

我正在尝试使用java6测试在java中发送电子邮件。代码是这样的:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class Sending_email {

final String senderEmail = "ttttt@gmail.com";
final String senderPassword = "123456";
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "587";
String receiverEmail = null;
String emailSubject = null;
String emailBody = null;

public Sending_email(String receiverEmail, String Subject, String message) {
    this.receiverEmail = receiverEmail;
    this.emailSubject = Subject;
    this.emailBody = message;

    Properties props = new Properties();
    props.put("mail.smtp.user", senderEmail);
    props.put("mail.smtp.host", emailSMTPserver);
    props.put("mail.smtp.port", emailServerPort);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", emailServerPort);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    //SecurityManager security = System.getSecurityManager();

    try {
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);

        Message msg = new MimeMessage(session);
        msg.setText(emailBody);
        msg.setSubject(emailSubject);
        msg.setFrom(new InternetAddress(senderEmail));
        msg.addRecipient(Message.RecipientType.TO,
                new InternetAddress(receiverEmail));
        Transport.send(msg);
        System.out.println("send successfully");
    } catch (Exception ex) {
        System.err.println("Error occurred while sending.!");
    }

}

private class SMTPAuthenticator extends javax.mail.Authenticator {

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(senderEmail, senderPassword);
    }
}

public static void main(String[] args) {
    new Sending_email("tttt@gmail.com", "suerjanct", "erjan lsjflsjdfsldjf");
}

}

然而,Eclipse无法编译它。它说

" java.lang.UnsupportedClassVersionError:javax / mail / Authenticator:不支持的major.minor版本51.0"

51.0表示我运行jdk 1.7。但我的电脑上只安装了1.8和1.6! enter image description here

1 个答案:

答案 0 :(得分:1)

这是因为你在编译期间使用了更高的JDK并且在runtime期间降低了JDK,正如你所说的有java 1.8,尝试使用相同的代码编译代码