2013-02-25 70 views
3

在發送電子郵件時,我使用commons-email-1.3收到以下錯誤。
我已經下載並將外部jar添加到項目中。
請幫我解決這個問題!使用commons-email-1.3發送電子郵件時出錯

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("[email protected]"); 
      email.setSubject("Hi"); 
      email.setMsg("This is a test mail ... :-)"); 
      email.addTo("[email protected]"); 
      email.setTLS(true); 
      email.send(); 
      System.out.println("Mail sent!"); 

    } 
} 

,讓錯誤的線路

email.setAuthenticator(new DefaultAuthenticator("myid","mypwd")); 

的錯誤消息是

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The type javax.mail.Authenticator cannot be resolved. It is indirectly referenced from required .class files
The method setAuthenticator(Authenticator) from the type Email refers to the missing type Authenticator at mypkg.mailtest.main(mailtest.java:13)

+0

ERROE LINE:email.setAuthenticator(新DefaultAuthenticator ( 「本身份識別碼」, 「MYPWD」)); – H4SN 2013-02-25 08:08:32

回答

4

你需要在你的classpath兩者的mail.jar和activation.jar。

+0

從哪裏我找到activation.jar? – H4SN 2013-02-25 08:12:02

+1

現在感謝代碼作品:) – H4SN 2013-02-25 08:22:19

0

打開文件的pom.xml下載jar文件,添加代碼:

<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> 
+0

請解釋您提供的答案。 – 2017-10-05 08:48:52

+0

http://mvnrepository.com/artifact/org.apache.commons/commons-email/1.4 默認的Apache公共電子郵件。使用Maven和上面一樣。 我的英語不好。 – Minato 2017-10-05 16:21:30

+0

添加此代碼,以便它自動從互聯網下載所需的庫。這裏是使用lib:commons-email。 – Minato 2017-10-05 21:48:23

相關問題