0

Basically, I am trying to send an email using mailgun with below code, It compiles just fine but I am receiving below error while running .class file:

error: Unable to initialize main class EmailClient Cause by: java.lang.NoClassDefFoundError: javax/mail/Message

I downloaded the activation.jar, javax.mail.jar, mail.jar file on the same directory and set the compile parameters to javac EmailClient.java -cp .:activation.jar:javax.mail.jar:mail.jar and java EmailClient -cp .:activation.jar:javax.mail.jar:mail.jar and I still am getting the error.

My code:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailClient {
  private static final String senderEmail = "test@logicbig.com";//change with your sender email
  private static final String senderPassword = "12345678";//change with your sender password

  public static void sendAsHtml(String to, String title, String html) throws MessagingException {
  System.out.println("Sending email to " + to);

  Session session = createSession();

  //create message using session
  MimeMessage message = new MimeMessage(session);
  prepareEmailMessage(message, to, title, html);

  //sending message
  Transport.send(message);
  System.out.println("Done");
  }

  private static void prepareEmailMessage(MimeMessage message, String to, String title, String html)
      throws MessagingException {
  message.setContent(html, "text/html; charset=utf-8");
  message.setFrom(new InternetAddress(senderEmail));
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  message.setSubject(title);
  }

  private static Session createSession() {
  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");//Outgoing server requires authentication
  props.put("mail.smtp.starttls.enable", "true");//TLS must be activated
  props.put("mail.smtp.host", "smtp.1and1.com"); //Outgoing server (SMTP) - change it to your SMTP server
  props.put("mail.smtp.port", "587");//Outgoing port

  Session session = Session.getInstance(props, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(senderEmail, senderPassword);
      }
  });
  return session;
  }

  public static void main(String[] args) throws MessagingException {
  EmailClient.sendAsHtml("testreceiver10@gmail.com",
          "Test email",
          "<h2>Java Mail Example</h2><p>hi there!</p>");
  }
}
Whoami
  • 61
  • 1
  • 1
  • 6
  • You must give `java` command options **before** the class name. Values after the class name are arguments to the program, i.e. the `args` parameter to `main`. Use: `java -cp .:activation.jar:javax.mail.jar:mail.jar EmailClient` – Andreas Jan 05 '19 at 20:35
  • See this answer for description of how to run a Java program: [What does “Could not find or load main class” mean?](https://stackoverflow.com/a/18093929/5221149) – Andreas Jan 05 '19 at 20:41

0 Answers0