我不断每次我尝试运行我的代码和IM只是失去了对什么是错用它的时候收到此相同的错误消息。这是我第一次尝试编写将通过Java通过Gmail发送电子邮件的代码,这是我一直收到的错误代码:
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尝试在线查找视频以帮助我解决问题,但这非常困难,因为我真的不知道问题出在哪里。
这是我的代码:
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;
}
}
这是javaMail.java(包含要运行的主要方法):
package emailTest;
public class javaMail {
public static void main(String[] args) throws Exception {
JavaMailUtil.sendMail("removed_for_obvious_reasons_acc2");
}
}
我100%确保即时输入发件人的电子邮件和密码也正确。
答案 0 :(得分:0)
尝试使用此:
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");
}
}