Android Apache Commons电子邮件类SimpleMail未找到

时间:2016-10-15 13:59:08

标签: android email classnotfoundexception apache-commons

我想使用apache commons电子邮件发送电子邮件,这样可以轻松实现,或者应该制作电子邮件。包含所有类,但会出现此错误。

这是我的代码和logcat。

email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("myemail@gmail.com", "mypass"));
try {
    email.setFrom("mymail@gmail.com");
    email.setSubject("TestMail");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("dest@hotmail.com");
    email.send();
} catch (EmailException e) {
    e.printStackTrace();
}

OBS:电子邮件声明为:

Email email;

堆栈跟踪:

10-15 10:36:57.254 1755-1755/contasts.jao.email E/AndroidRuntime: FATAL EXCEPTION: main
        Process: contasts.jao.email, PID: 1755
        java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/mail/SimpleEmail;
        at contasts.jao.email.MainActivity.mandarEmail(MainActivity.java:54)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.mail.SimpleEmail" on path: DexPathList[[dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-support-annotations-23.4.0_d240637ab16909631b8e0522acd22a25d9696630-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-internal_impl-23.4.0_af59f0bca6bcee7449190085fe52534aee83e2f1-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.4.0_bd8c75447dc30c698da5700ab2e0aefa2fe4fe35-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_3f1746aab7883e4aa141bfb0b148adeead28e570-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.4.0_f3af541e04a191f70d73c09b1b309272e4406a2b-classes.dex", dex file "/data/data/contasts.jao.email/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.4.0_241ea31a1f8c1e8e0741e2b1a365cddd6e9449c2-classes.dex"],nativeLibraryDirectories=[/data/app/contasts.jao.email-1/lib/arm, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at contasts.jao.email.MainActivity.mandarEmail(MainActivity.java:54)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        Suppressed: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.mail.SimpleEmail" on path: DexPathList[[zip file "/data/app/contasts.jao.email-1/base.apk"],nativeLibraryDirectories=[/data/app/contasts.jao.email-1/lib/arm, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:5

我已经宣布并导入了这个类。

1 个答案:

答案 0 :(得分:0)

请更改主机名

email.setHostName("smtp.googlemail.com");

email.setHostName("smtp.gmail.com");