0

I keep getting this same error message every time i try to run my code and im just lost on whats wrong with it. This is my first time trying to write code that will send a email using java via Gmail, here is the error code i keep receiving:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
    at emailTest.javaMail.main(javaMail.java:5)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 1 more

iv tried looking up for videos online to help me solve the proble, but its really hard since i dont really know what the problem is.

Here is my code:

JavaMailUtil.Java:

package emailTest;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailUtil {
    public static void sendMail(String recepient) throws Exception {
        System.out.println("preping to send email");
        Properties properties = new Properties();

        properties.put("mail.smtp.auth", "true");
        properties.put("mail.sntp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        String myAccountEmail = "removed_for_obvious_reasons;
        String myAccountPass = "removed_for_obvious_reasons";

        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(myAccountEmail, myAccountPass);
            }
        });

        Message message = prepareMessage(session, myAccountEmail, recepient);

        Transport.send(message);
        System.out.println("Message sent succesfully.");
    }

    private static Message prepareMessage(Session session, String myAccountEmail, String recepient) {
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("myAccountEmail"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
            message.setSubject("My first email from java");
            message.setText("Hey There, \n Look at my Email");
            return message;
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

and here is javaMail.java (contains main method to run):

package emailTest;

public class javaMail {
    public static void main(String[] args) throws Exception {
        JavaMailUtil.sendMail("removed_for_obvious_reasons_acc2");
    }
}

im 100% sure im typing in the senders email and password correct too.

1 Answers1

0

Try using this:

public static void main(String[] args) {

    String contentText = "";

    String mailWrapper = "";

    // Recipient's email ID needs to be mentioned.
    final String to = "some@gmail.com";

    // Sender's email ID needs to be mentioned
    final String from = "to@gmail.com";

    String password = "pasword";

    // Assuming you are sending email from through gmails smtp
    String host = "smtp.gmail.com";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", "465");
    properties.put("mail.smtp.ssl.enable", "true");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.debug", "false");

    // Get the Session object.// and pass username and password
    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(from, password);

        }

    });

    // Used to debug SMTP issues
    session.setDebug(false);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set Subject: header field
        message.setSubject("Your Subject goes here");

        // Now set the actual message
        message.setText("This is actual message");

        System.out.println("SENDING MAIL...");

        // Send message
        Transport.send(message);

        System.out.println("EMAIL SENT SUCCESSFULLY.");
    } catch (MessagingException mex) {
        System.out.println("ERROR IN SENDING EMAIL");
    }
}
Rahul Agrawal
  • 623
  • 1
  • 5
  • 18
  • thank you i will give it a shot and get back to you within a couple of minutes, need to get dinner ready for the family real quick – Huseyin Yesiler Mar 12 '20 at 15:13
  • I'm getting errors with your example code: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials cx4sm10465798pjb.53 - gsmtp. I believe it's because my gmail acct is set for 2-factor authorization. Any thoughts? – Ed S Jul 11 '21 at 20:15
  • yeah it wont work for 2-factor authentication. You have to disable it – Rahul Agrawal Jul 12 '21 at 04:53