发送电子邮件时,我使用commons-email-1.3收到以下错误 我已经下载并添加了外部jar到项目中 请帮我解决这个问题!
package mypkg;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
public class sendingmail {
public static void main(String[] args) throws Exception {
Email email = new SimpleEmail();
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("myid","mypwd")); //Here is the error
email.setDebug(false);
email.setHostName("smtp.gmail.com");
email.setFrom("me@gmail.com");
email.setSubject("Hi");
email.setMsg("This is a test mail ... :-)");
email.addTo("you@gmail.com");
email.setTLS(true);
email.send();
System.out.println("Mail sent!");
}
}
给出错误的行是
email.setAuthenticator(new DefaultAuthenticator("myid","mypwd"));
错误消息是
线程“main”中的异常java.lang.Error:未解决的编译问题:
无法解析javax.mail.Authenticator类型。它是从所需的.class文件中间接引用的 来自Email类型的方法setAuthenticator(Authenticator)指的是mypkg.mailtest.main中缺少的类型Authenticator(mailtest.java:13)
答案 0 :(得分:9)
答案 1 :(得分:4)
你的类路径中需要mail.jar和activation.jar。
答案 2 :(得分:1)
打开文件pom.xml,添加代码:
<dependencies>
<!-- http://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
</dependencies>