我正在尝试使用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"
答案 0 :(得分:1)
这是因为你在编译期间使用了更高的JDK
并且在runtime
期间降低了JDK,正如你所说的有java 1.8,尝试使用相同的代码编译代码