0

I m trying to test sending email in java using java6. The code is this:

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

}

However, Eclipse can't compile it. It says

"java.lang.UnsupportedClassVersionError: javax/mail/Authenticator : Unsupported major.minor version 51.0"

51.0 means I run jdk 1.7. but I only have 1.8 and 1.6 installed on my computer! enter image description here

ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • 1
    Possible duplicate of [How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version](https://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi) – ProgrammersBlock Dec 05 '17 at 05:09

1 Answers1

1

This is because you have used higher JDK during compile time and lower JDK during runtime, as you said you have java 1.8, try to compile the code using the same

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
  • hmm, this is strange! I put in my project settings to run & compile using java6 explicitly. How do i check if it compiles using java6 not8? – ERJAN Dec 05 '17 at 05:10
  • the funny thing, the eclipse now runs itself on java8, but this should not be a problem, cuz for project settings i put jre6 everywhere... – ERJAN Dec 05 '17 at 05:13
  • https://stackoverflow.com/questions/9302687/how-can-i-tell-eclipse-to-compile-and-build-a-project-with-a-different-jre-versi – Vishnu T S Dec 05 '17 at 05:27