无法通过java发送电子邮件
每次我试着运行我的代码时都会收到同样的错误消息,我只是不知道它到底出了什么问题。这是我第一次尝试编写通过Gmail使用java发送电子邮件的代码,下面是我一直收到的错误代码:无法通过java发送电子邮件,java,email,gmail,Java,Email,Gmail,每次我试着运行我的代码时都会收到同样的错误消息,我只是不知道它到底出了什么问题。这是我第一次尝试编写通过Gmail使用java发送电子邮件的代码,下面是我一直收到的错误代码: Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator at emailTest.javaMail.main(javaMail.java:5) Caused by: java.lang.ClassNotF
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
我试着在网上查找视频来帮助我解决这个问题,但这真的很难,因为我真的不知道问题是什么
这是我的密码:
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(包含要运行的主要方法):
我100%确定我输入的发件人电子邮件和密码也正确。尝试使用以下方法:
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");
}
}
你在用maven吗?请张贴您的pom,如果您阅读后,我认为我的文件依赖性可能是错误的,虽然我甚至不知道如何更改或查看它。我想我会回去的google@rhowell我使用eclipse编写了这段代码,下载了javax.mail.jar,作为外部库或其他东西添加到项目中,并开始编写代码。没有做任何其他事情希望它只是输入错误
message.setFrom(新的InternetAddress(“myAccountEmail”)
否则myAccountEmail
不应在quotes@RahulAgrawal哦,妈的,我甚至都没注意到括号,让我很快把它们取下来,看看会发生什么谢谢你,我会试一试,几分钟内回复你,需要尽快为家人准备好晚餐
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");
}
}