java.lang.ClassNotFoundException: 在路径上未找到类 “com.sun.mail.util.MailLogger”。

huangapple go评论97阅读模式
英文:

java.lang.ClassNotFoundException: Didn't find class "com.sun.mail.util.MailLogger" on path:

问题

以下是翻译好的内容:

ForgotPasswordAcivity.java

public class ForgotPasswordActivity extends AppCompatActivity {

    EditText getNumberEditText;

    int generatedNumber;

    protected void sendEmail() {
        
        Random random = new Random();
        generatedNumber = random.nextInt(10000 - 1001) + 1001;

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    GMailSender sender = new GMailSender("sample@gmail.com",
                            "1234");
                    sender.sendMail("更改密码的代码 - Stairways to Solutions", "您的代码是 " + generatedNumber,
                            "sample@gmail.com", "user@gmail.com");
                } catch (Exception e) {
                    Log.e("SendMail", e.getMessage(), e);
                }
            }

        }).start();
    }

    public void send(View view){

        sendEmail();

    }

    public void check(View view){

        if (getNumberEditText.getText().toString().equals(Integer.toString(generatedNumber))){

            Toast.makeText(this, "给出了正确的代码!", Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, "请检查代码", Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        getNumberEditText = findViewById(R.id.getNumberEditText);
    }
}

GMailSender.java

class GMailSender extends javax.mail.Authenticator {
    private String mailhost = "smtp.gmail.com"; // 邮件服务器的主机名,用于连接发送邮件的SMTP服务器。
    private String user;
    private String password;
    private Session session;

    static {
        Security.addProvider(new JSSEProvider());
    }

    public GMailSender(String user, String password) {
        this.user = user; // 您的SMTP用户名。对于GMail SMTP,这将是您的GMail电子邮件地址。
        this.password = password; // 您的SMTP密码。对于GMail SMTP,这将是您的GMail密码。

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body,
                                      String sender, String recipients) throws Exception {
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);

        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

        Transport.send(message);
    }
}

JSSEProvider.java

/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*
 * @author Alexander Y. Kleymenov
 * @version $Revision$
 */
import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}

build.gradle:Project

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.luv.stairwaystosolutions"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    buildTypes {
        release {
            minifyEnabled true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.parse.bolts:bolts-tasks:1.4.0'
    implementation 'com.parse:parse-android:1.17.3'
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'javax.mail:javax.mail-api:1.5.3'
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.1'
            }
        }
    }
}

build.gradle:Module

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
        jcenter()
        google()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath '

<details>
<summary>英文:</summary>

I am making an app in which an email will be sent to user&#39;s gmail id when a button is clicked. This is the following code of needed classes.

**ForgotPasswordAcivity.java**

        public class ForgotPasswordActivity extends AppCompatActivity {
    
        EditText getNumberEditText;
    
        int generatedNumber;
    
        protected void sendEmail() {
            
            Random random = new Random();
            generatedNumber = random.nextInt(10000 - 1001) + 1001;
    
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        GMailSender sender = new GMailSender(&quot;sample@gmail.com&quot;,
                                &quot;1234&quot;);
                        sender.sendMail(&quot;Code for Changing Password in Stairways to Solutions&quot;, &quot;Your Code is &quot; + generatedNumber,
                                &quot;sample@gmail.com&quot;, &quot;user@gmail.com&quot;);
                    } catch (Exception e) {
                        Log.e(&quot;SendMail&quot;, e.getMessage(), e);
                    }
                }
    
            }).start();
        }
    
        public void send(View view){
    
            sendEmail();
    
        }
    
        public void check(View view){
    
            if (getNumberEditText.getText().toString().equals(Integer.toString(generatedNumber))){
    
                Toast.makeText(this, &quot;Correct Number given!&quot;, Toast.LENGTH_SHORT).show();
    
            } else {
    
                Toast.makeText(this, &quot;Check the Number&quot;, Toast.LENGTH_SHORT).show();
    
            }
    
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_forgot_password);
    
            getNumberEditText = findViewById(R.id.getNumberEditText);
        }
    }

**GMailSender.java**

        class GMailSender extends javax.mail.Authenticator {
        private String mailhost = &quot;smtp.gmail.com&quot;; //Hostname of the SMTP mail server which you want to connect for sending emails.
        private String user;
        private String password;
        private Session session;
    
        static {
            Security.addProvider(new JSSEProvider());
        }
    
        public GMailSender(String user, String password) {
            this.user = user; //Your SMTP username. In case of GMail SMTP this is going to be your GMail email address.
            this.password = password; //Your SMTP password. In case of GMail SMTP this is going to be your GMail password.
    
            Properties props = new Properties();
            props.setProperty(&quot;mail.transport.protocol&quot;, &quot;smtp&quot;);
            props.setProperty(&quot;mail.host&quot;, mailhost);
            props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
            props.put(&quot;mail.smtp.port&quot;, &quot;465&quot;);
            props.put(&quot;mail.smtp.socketFactory.port&quot;, &quot;465&quot;);
            props.put(&quot;mail.smtp.socketFactory.class&quot;, &quot;javax.net.ssl.SSLSocketFactory&quot;);
            props.put(&quot;mail.smtp.socketFactory.fallback&quot;, &quot;false&quot;);
            props.setProperty(&quot;mail.smtp.quitwait&quot;, &quot;false&quot;);
    
            session = Session.getDefaultInstance(props, this);
        }
    
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    
        public synchronized void sendMail(String subject, String body,
                                          String sender, String recipients) throws Exception {
            MimeMessage message = new MimeMessage(session);
            DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), &quot;text/plain&quot;));
            message.setSender(new InternetAddress(sender));
            message.setSubject(subject);
            message.setDataHandler(handler);
    
            if (recipients.indexOf(&#39;,&#39;) &gt; 0)
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
            else
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
    
            Transport.send(message);
        }
    }

**JSSEProvider.java**

        /*
     *  Licensed to the Apache Software Foundation (ASF) under one or more
     *  contributor license agreements.  See the NOTICE file distributed with
     *  this work for additional information regarding copyright ownership.
     *  The ASF licenses this file to You under the Apache License, Version 2.0
     *  (the &quot;License&quot;); you may not use this file except in compliance with
     *  the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     *  Unless required by applicable law or agreed to in writing, software
     *  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *  See the License for the specific language governing permissions and
     *  limitations under the License.
     */
    
    /*
     * @author Alexander Y. Kleymenov
     * @version $Revision$
     */
    import java.security.AccessController;
    import java.security.Provider;
    
    public final class JSSEProvider extends Provider {
    
        public JSSEProvider() {
            super(&quot;HarmonyJSSE&quot;, 1.0, &quot;Harmony JSSE Provider&quot;);
            AccessController.doPrivileged(new java.security.PrivilegedAction&lt;Void&gt;() {
                public Void run() {
                    put(&quot;SSLContext.TLS&quot;,
                            &quot;org.apache.harmony.xnet.provider.jsse.SSLContextImpl&quot;);
                    put(&quot;Alg.Alias.SSLContext.TLSv1&quot;, &quot;TLS&quot;);
                    put(&quot;KeyManagerFactory.X509&quot;,
                            &quot;org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl&quot;);
                    put(&quot;TrustManagerFactory.X509&quot;,
                            &quot;org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl&quot;);
                    return null;
                }
            });
        }
    }

build.gradle:Project

   

     apply plugin: &#39;com.android.application&#39;
    
    android {
        compileSdkVersion 29
        buildToolsVersion &#39;28.0.3&#39;
    
        defaultConfig {
            applicationId &quot;com.luv.stairwaystosolutions&quot;
            minSdkVersion 23
            targetSdkVersion 29
            versionCode 1
            versionName &quot;1.0&quot;
            testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
            multiDexEnabled true
        }
        dexOptions {
            javaMaxHeapSize &quot;4g&quot;
        }
        buildTypes {
            release {
                minifyEnabled true
    
                proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
            }
        }
    }
    
    dependencies {
        implementation &#39;androidx.appcompat:appcompat:1.0.0&#39;
        implementation &#39;com.parse.bolts:bolts-tasks:1.4.0&#39;
        implementation &#39;com.parse:parse-android:1.17.3&#39;
        implementation &#39;androidx.multidex:multidex:2.0.0&#39;
        implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
        implementation &#39;com.google.android.material:material:1.0.0-beta01&#39;
        implementation &#39;javax.mail:javax.mail-api:1.5.3&#39;
    }
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
            def requested = details.requested
            if (requested.group == &#39;com.android.support&#39;) {
                if (!requested.name.startsWith(&quot;multidex&quot;)) {
                    details.useVersion &#39;25.3.1&#39;
                }
            }
        }
    }

build.gradle:Module

   

     // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        repositories {
            mavenCentral()
            jcenter()
            google()
            maven {
                url &#39;https://maven.google.com/&#39;
                name &#39;Google&#39;
            }
        }
        dependencies {
            classpath &#39;com.android.tools.build:gradle:3.5.4&#39;
        }
    }
    
    allprojects {
        repositories {
            mavenCentral()
            maven {
                url &#39;https://maven.google.com/&#39;
                name &#39;Google&#39;
            }
        }
    }
    
    ext {
        compileSdkVersion = 27
        buildToolsVersion = &quot;27.0.3&quot;
    
        minSdkVersion = 14
        targetSdkVersion = 23
    }

I have added the activation.jar, additional.jar and mail.jar in libs folder.

[![libs Folder][1]][1]

Error :

    2020-09-21 19:13:36.868 16758-16809/com.luv.stairwaystosolutions E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: com.luv.stairwaystosolutions, PID: 16758
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/sun/mail/util/MailLogger;
        at javax.mail.Session.initLogger(Session.java:226)
        at javax.mail.Session.&lt;init&gt;(Session.java:210)
        at javax.mail.Session.getDefaultInstance(Session.java:321)
        at com.luv.stairwaystosolutions.GMailSender.&lt;init&gt;(GMailSender.java:38)
        at com.luv.stairwaystosolutions.ForgotPasswordActivity$1.run(ForgotPasswordActivity.java:33)
        at java.lang.Thread.run(Thread.java:761)
     Caused by: java.lang.ClassNotFoundException: Didn&#39;t find class &quot;com.sun.mail.util.MailLogger&quot; on path: DexPathList[[zip file &quot;/data/app/com.luv.stairwaystosolutions-1/base.apk&quot;],nativeLibraryDirectories=[/data/app/com.luv.stairwaystosolutions-1/lib/x86_64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at javax.mail.Session.initLogger(Session.java:226)&#160;
        at javax.mail.Session.&lt;init&gt;(Session.java:210)&#160;
        at javax.mail.Session.getDefaultInstance(Session.java:321)&#160;
        at com.luv.stairwaystosolutions.GMailSender.&lt;init&gt;(GMailSender.java:38)&#160;
        at com.luv.stairwaystosolutions.ForgotPasswordActivity$1.run(ForgotPasswordActivity.java:33)&#160;
        at java.lang.Thread.run(Thread.java:761)&#160;

This is the error line :

    session = Session.getDefaultInstance(props, this);


Thank you in advance for help.


  [1]: https://i.stack.imgur.com/Gf5Jd.png

</details>


# 答案1
**得分**: 1

**build.gradle:Project**

```gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.luv.stairwaystosolutions"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    buildTypes {
        release {
            minifyEnabled true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.parse.bolts:bolts-tasks:1.4.0'
    implementation 'com.parse:parse-android:1.17.3'
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'javax.mail:javax.mail-api:1.5.3'
    implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2' 
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.1'
            }
        }
    }
}
英文:

build.gradle:Project

apply plugin: &#39;com.android.application&#39;
android {
compileSdkVersion 29
buildToolsVersion &#39;28.0.3&#39;
defaultConfig {
applicationId &quot;com.luv.stairwaystosolutions&quot;
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName &quot;1.0&quot;
testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
multiDexEnabled true
}
dexOptions {
javaMaxHeapSize &quot;4g&quot;
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
}
}
}
dependencies {
implementation &#39;androidx.appcompat:appcompat:1.0.0&#39;
implementation &#39;com.parse.bolts:bolts-tasks:1.4.0&#39;
implementation &#39;com.parse:parse-android:1.17.3&#39;
implementation &#39;androidx.multidex:multidex:2.0.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
implementation &#39;com.google.android.material:material:1.0.0-beta01&#39;
implementation &#39;javax.mail:javax.mail-api:1.5.3&#39;
implementation group: &#39;com.sun.mail&#39;, name: &#39;javax.mail&#39;, version: &#39;1.6.2&#39; 
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
def requested = details.requested
if (requested.group == &#39;com.android.support&#39;) {
if (!requested.name.startsWith(&quot;multidex&quot;)) {
details.useVersion &#39;25.3.1&#39;
}
}
}
}

huangapple
  • 本文由 发表于 2020年9月21日 21:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63993643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定