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

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

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

问题

以下是翻译好的内容:

ForgotPasswordAcivity.java

  1. public class ForgotPasswordActivity extends AppCompatActivity {
  2. EditText getNumberEditText;
  3. int generatedNumber;
  4. protected void sendEmail() {
  5. Random random = new Random();
  6. generatedNumber = random.nextInt(10000 - 1001) + 1001;
  7. new Thread(new Runnable() {
  8. @Override
  9. public void run() {
  10. try {
  11. GMailSender sender = new GMailSender("sample@gmail.com",
  12. "1234");
  13. sender.sendMail("更改密码的代码 - Stairways to Solutions", "您的代码是 " + generatedNumber,
  14. "sample@gmail.com", "user@gmail.com");
  15. } catch (Exception e) {
  16. Log.e("SendMail", e.getMessage(), e);
  17. }
  18. }
  19. }).start();
  20. }
  21. public void send(View view){
  22. sendEmail();
  23. }
  24. public void check(View view){
  25. if (getNumberEditText.getText().toString().equals(Integer.toString(generatedNumber))){
  26. Toast.makeText(this, "给出了正确的代码!", Toast.LENGTH_SHORT).show();
  27. } else {
  28. Toast.makeText(this, "请检查代码", Toast.LENGTH_SHORT).show();
  29. }
  30. }
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_forgot_password);
  35. getNumberEditText = findViewById(R.id.getNumberEditText);
  36. }
  37. }

GMailSender.java

  1. class GMailSender extends javax.mail.Authenticator {
  2. private String mailhost = "smtp.gmail.com"; // 邮件服务器的主机名,用于连接发送邮件的SMTP服务器。
  3. private String user;
  4. private String password;
  5. private Session session;
  6. static {
  7. Security.addProvider(new JSSEProvider());
  8. }
  9. public GMailSender(String user, String password) {
  10. this.user = user; // 您的SMTP用户名。对于GMail SMTP,这将是您的GMail电子邮件地址。
  11. this.password = password; // 您的SMTP密码。对于GMail SMTP,这将是您的GMail密码。
  12. Properties props = new Properties();
  13. props.setProperty("mail.transport.protocol", "smtp");
  14. props.setProperty("mail.host", mailhost);
  15. props.put("mail.smtp.auth", "true");
  16. props.put("mail.smtp.port", "465");
  17. props.put("mail.smtp.socketFactory.port", "465");
  18. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  19. props.put("mail.smtp.socketFactory.fallback", "false");
  20. props.setProperty("mail.smtp.quitwait", "false");
  21. session = Session.getDefaultInstance(props, this);
  22. }
  23. protected PasswordAuthentication getPasswordAuthentication() {
  24. return new PasswordAuthentication(user, password);
  25. }
  26. public synchronized void sendMail(String subject, String body,
  27. String sender, String recipients) throws Exception {
  28. MimeMessage message = new MimeMessage(session);
  29. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  30. message.setSender(new InternetAddress(sender));
  31. message.setSubject(subject);
  32. message.setDataHandler(handler);
  33. if (recipients.indexOf(',') > 0)
  34. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  35. else
  36. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  37. Transport.send(message);
  38. }
  39. }

JSSEProvider.java

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * @author Alexander Y. Kleymenov
  19. * @version $Revision$
  20. */
  21. import java.security.AccessController;
  22. import java.security.Provider;
  23. public final class JSSEProvider extends Provider {
  24. public JSSEProvider() {
  25. super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
  26. AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  27. public Void run() {
  28. put("SSLContext.TLS",
  29. "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
  30. put("Alg.Alias.SSLContext.TLSv1", "TLS");
  31. put("KeyManagerFactory.X509",
  32. "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
  33. put("TrustManagerFactory.X509",
  34. "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
  35. return null;
  36. }
  37. });
  38. }
  39. }

build.gradle:Project

  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion 29
  4. buildToolsVersion '28.0.3'
  5. defaultConfig {
  6. applicationId "com.luv.stairwaystosolutions"
  7. minSdkVersion 23
  8. targetSdkVersion 29
  9. versionCode 1
  10. versionName "1.0"
  11. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  12. multiDexEnabled true
  13. }
  14. dexOptions {
  15. javaMaxHeapSize "4g"
  16. }
  17. buildTypes {
  18. release {
  19. minifyEnabled true
  20. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  21. }
  22. }
  23. }
  24. dependencies {
  25. implementation 'androidx.appcompat:appcompat:1.0.0'
  26. implementation 'com.parse.bolts:bolts-tasks:1.4.0'
  27. implementation 'com.parse:parse-android:1.17.3'
  28. implementation 'androidx.multidex:multidex:2.0.0'
  29. implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  30. implementation 'com.google.android.material:material:1.0.0-beta01'
  31. implementation 'javax.mail:javax.mail-api:1.5.3'
  32. }
  33. configurations.all {
  34. resolutionStrategy.eachDependency { DependencyResolveDetails details ->
  35. def requested = details.requested
  36. if (requested.group == 'com.android.support') {
  37. if (!requested.name.startsWith("multidex")) {
  38. details.useVersion '25.3.1'
  39. }
  40. }
  41. }
  42. }

build.gradle:Module

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories {
  4. mavenCentral()
  5. jcenter()
  6. google()
  7. maven {
  8. url 'https://maven.google.com/'
  9. name 'Google'
  10. }
  11. }
  12. dependencies {
  13. classpath '
  14. <details>
  15. <summary>英文:</summary>
  16. 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.
  17. **ForgotPasswordAcivity.java**
  18. public class ForgotPasswordActivity extends AppCompatActivity {
  19. EditText getNumberEditText;
  20. int generatedNumber;
  21. protected void sendEmail() {
  22. Random random = new Random();
  23. generatedNumber = random.nextInt(10000 - 1001) + 1001;
  24. new Thread(new Runnable() {
  25. @Override
  26. public void run() {
  27. try {
  28. GMailSender sender = new GMailSender(&quot;sample@gmail.com&quot;,
  29. &quot;1234&quot;);
  30. sender.sendMail(&quot;Code for Changing Password in Stairways to Solutions&quot;, &quot;Your Code is &quot; + generatedNumber,
  31. &quot;sample@gmail.com&quot;, &quot;user@gmail.com&quot;);
  32. } catch (Exception e) {
  33. Log.e(&quot;SendMail&quot;, e.getMessage(), e);
  34. }
  35. }
  36. }).start();
  37. }
  38. public void send(View view){
  39. sendEmail();
  40. }
  41. public void check(View view){
  42. if (getNumberEditText.getText().toString().equals(Integer.toString(generatedNumber))){
  43. Toast.makeText(this, &quot;Correct Number given!&quot;, Toast.LENGTH_SHORT).show();
  44. } else {
  45. Toast.makeText(this, &quot;Check the Number&quot;, Toast.LENGTH_SHORT).show();
  46. }
  47. }
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_forgot_password);
  52. getNumberEditText = findViewById(R.id.getNumberEditText);
  53. }
  54. }
  55. **GMailSender.java**
  56. class GMailSender extends javax.mail.Authenticator {
  57. private String mailhost = &quot;smtp.gmail.com&quot;; //Hostname of the SMTP mail server which you want to connect for sending emails.
  58. private String user;
  59. private String password;
  60. private Session session;
  61. static {
  62. Security.addProvider(new JSSEProvider());
  63. }
  64. public GMailSender(String user, String password) {
  65. this.user = user; //Your SMTP username. In case of GMail SMTP this is going to be your GMail email address.
  66. this.password = password; //Your SMTP password. In case of GMail SMTP this is going to be your GMail password.
  67. Properties props = new Properties();
  68. props.setProperty(&quot;mail.transport.protocol&quot;, &quot;smtp&quot;);
  69. props.setProperty(&quot;mail.host&quot;, mailhost);
  70. props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
  71. props.put(&quot;mail.smtp.port&quot;, &quot;465&quot;);
  72. props.put(&quot;mail.smtp.socketFactory.port&quot;, &quot;465&quot;);
  73. props.put(&quot;mail.smtp.socketFactory.class&quot;, &quot;javax.net.ssl.SSLSocketFactory&quot;);
  74. props.put(&quot;mail.smtp.socketFactory.fallback&quot;, &quot;false&quot;);
  75. props.setProperty(&quot;mail.smtp.quitwait&quot;, &quot;false&quot;);
  76. session = Session.getDefaultInstance(props, this);
  77. }
  78. protected PasswordAuthentication getPasswordAuthentication() {
  79. return new PasswordAuthentication(user, password);
  80. }
  81. public synchronized void sendMail(String subject, String body,
  82. String sender, String recipients) throws Exception {
  83. MimeMessage message = new MimeMessage(session);
  84. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), &quot;text/plain&quot;));
  85. message.setSender(new InternetAddress(sender));
  86. message.setSubject(subject);
  87. message.setDataHandler(handler);
  88. if (recipients.indexOf(&#39;,&#39;) &gt; 0)
  89. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  90. else
  91. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  92. Transport.send(message);
  93. }
  94. }
  95. **JSSEProvider.java**
  96. /*
  97. * Licensed to the Apache Software Foundation (ASF) under one or more
  98. * contributor license agreements. See the NOTICE file distributed with
  99. * this work for additional information regarding copyright ownership.
  100. * The ASF licenses this file to You under the Apache License, Version 2.0
  101. * (the &quot;License&quot;); you may not use this file except in compliance with
  102. * the License. You may obtain a copy of the License at
  103. *
  104. * http://www.apache.org/licenses/LICENSE-2.0
  105. *
  106. * Unless required by applicable law or agreed to in writing, software
  107. * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
  108. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  109. * See the License for the specific language governing permissions and
  110. * limitations under the License.
  111. */
  112. /*
  113. * @author Alexander Y. Kleymenov
  114. * @version $Revision$
  115. */
  116. import java.security.AccessController;
  117. import java.security.Provider;
  118. public final class JSSEProvider extends Provider {
  119. public JSSEProvider() {
  120. super(&quot;HarmonyJSSE&quot;, 1.0, &quot;Harmony JSSE Provider&quot;);
  121. AccessController.doPrivileged(new java.security.PrivilegedAction&lt;Void&gt;() {
  122. public Void run() {
  123. put(&quot;SSLContext.TLS&quot;,
  124. &quot;org.apache.harmony.xnet.provider.jsse.SSLContextImpl&quot;);
  125. put(&quot;Alg.Alias.SSLContext.TLSv1&quot;, &quot;TLS&quot;);
  126. put(&quot;KeyManagerFactory.X509&quot;,
  127. &quot;org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl&quot;);
  128. put(&quot;TrustManagerFactory.X509&quot;,
  129. &quot;org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl&quot;);
  130. return null;
  131. }
  132. });
  133. }
  134. }
  135. build.gradle:Project
  136. apply plugin: &#39;com.android.application&#39;
  137. android {
  138. compileSdkVersion 29
  139. buildToolsVersion &#39;28.0.3&#39;
  140. defaultConfig {
  141. applicationId &quot;com.luv.stairwaystosolutions&quot;
  142. minSdkVersion 23
  143. targetSdkVersion 29
  144. versionCode 1
  145. versionName &quot;1.0&quot;
  146. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  147. multiDexEnabled true
  148. }
  149. dexOptions {
  150. javaMaxHeapSize &quot;4g&quot;
  151. }
  152. buildTypes {
  153. release {
  154. minifyEnabled true
  155. proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
  156. }
  157. }
  158. }
  159. dependencies {
  160. implementation &#39;androidx.appcompat:appcompat:1.0.0&#39;
  161. implementation &#39;com.parse.bolts:bolts-tasks:1.4.0&#39;
  162. implementation &#39;com.parse:parse-android:1.17.3&#39;
  163. implementation &#39;androidx.multidex:multidex:2.0.0&#39;
  164. implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
  165. implementation &#39;com.google.android.material:material:1.0.0-beta01&#39;
  166. implementation &#39;javax.mail:javax.mail-api:1.5.3&#39;
  167. }
  168. configurations.all {
  169. resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
  170. def requested = details.requested
  171. if (requested.group == &#39;com.android.support&#39;) {
  172. if (!requested.name.startsWith(&quot;multidex&quot;)) {
  173. details.useVersion &#39;25.3.1&#39;
  174. }
  175. }
  176. }
  177. }
  178. build.gradle:Module
  179. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  180. buildscript {
  181. repositories {
  182. mavenCentral()
  183. jcenter()
  184. google()
  185. maven {
  186. url &#39;https://maven.google.com/&#39;
  187. name &#39;Google&#39;
  188. }
  189. }
  190. dependencies {
  191. classpath &#39;com.android.tools.build:gradle:3.5.4&#39;
  192. }
  193. }
  194. allprojects {
  195. repositories {
  196. mavenCentral()
  197. maven {
  198. url &#39;https://maven.google.com/&#39;
  199. name &#39;Google&#39;
  200. }
  201. }
  202. }
  203. ext {
  204. compileSdkVersion = 27
  205. buildToolsVersion = &quot;27.0.3&quot;
  206. minSdkVersion = 14
  207. targetSdkVersion = 23
  208. }
  209. I have added the activation.jar, additional.jar and mail.jar in libs folder.
  210. [![libs Folder][1]][1]
  211. Error :
  212. 2020-09-21 19:13:36.868 16758-16809/com.luv.stairwaystosolutions E/AndroidRuntime: FATAL EXCEPTION: Thread-2
  213. Process: com.luv.stairwaystosolutions, PID: 16758
  214. java.lang.NoClassDefFoundError: Failed resolution of: Lcom/sun/mail/util/MailLogger;
  215. at javax.mail.Session.initLogger(Session.java:226)
  216. at javax.mail.Session.&lt;init&gt;(Session.java:210)
  217. at javax.mail.Session.getDefaultInstance(Session.java:321)
  218. at com.luv.stairwaystosolutions.GMailSender.&lt;init&gt;(GMailSender.java:38)
  219. at com.luv.stairwaystosolutions.ForgotPasswordActivity$1.run(ForgotPasswordActivity.java:33)
  220. at java.lang.Thread.run(Thread.java:761)
  221. 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]]
  222. at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  223. at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
  224. at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
  225. at javax.mail.Session.initLogger(Session.java:226)&#160;
  226. at javax.mail.Session.&lt;init&gt;(Session.java:210)&#160;
  227. at javax.mail.Session.getDefaultInstance(Session.java:321)&#160;
  228. at com.luv.stairwaystosolutions.GMailSender.&lt;init&gt;(GMailSender.java:38)&#160;
  229. at com.luv.stairwaystosolutions.ForgotPasswordActivity$1.run(ForgotPasswordActivity.java:33)&#160;
  230. at java.lang.Thread.run(Thread.java:761)&#160;
  231. This is the error line :
  232. session = Session.getDefaultInstance(props, this);
  233. Thank you in advance for help.
  234. [1]: https://i.stack.imgur.com/Gf5Jd.png
  235. </details>
  236. # 答案1
  237. **得分**: 1
  238. **build.gradle:Project**
  239. ```gradle
  240. apply plugin: 'com.android.application'
  241. android {
  242. compileSdkVersion 29
  243. buildToolsVersion '28.0.3'
  244. defaultConfig {
  245. applicationId "com.luv.stairwaystosolutions"
  246. minSdkVersion 23
  247. targetSdkVersion 29
  248. versionCode 1
  249. versionName "1.0"
  250. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  251. multiDexEnabled true
  252. }
  253. dexOptions {
  254. javaMaxHeapSize "4g"
  255. }
  256. buildTypes {
  257. release {
  258. minifyEnabled true
  259. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  260. }
  261. }
  262. }
  263. dependencies {
  264. implementation 'androidx.appcompat:appcompat:1.0.0'
  265. implementation 'com.parse.bolts:bolts-tasks:1.4.0'
  266. implementation 'com.parse:parse-android:1.17.3'
  267. implementation 'androidx.multidex:multidex:2.0.0'
  268. implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  269. implementation 'com.google.android.material:material:1.0.0-beta01'
  270. implementation 'javax.mail:javax.mail-api:1.5.3'
  271. implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
  272. }
  273. configurations.all {
  274. resolutionStrategy.eachDependency { DependencyResolveDetails details ->
  275. def requested = details.requested
  276. if (requested.group == 'com.android.support') {
  277. if (!requested.name.startsWith("multidex")) {
  278. details.useVersion '25.3.1'
  279. }
  280. }
  281. }
  282. }
英文:

build.gradle:Project

  1. apply plugin: &#39;com.android.application&#39;
  2. android {
  3. compileSdkVersion 29
  4. buildToolsVersion &#39;28.0.3&#39;
  5. defaultConfig {
  6. applicationId &quot;com.luv.stairwaystosolutions&quot;
  7. minSdkVersion 23
  8. targetSdkVersion 29
  9. versionCode 1
  10. versionName &quot;1.0&quot;
  11. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  12. multiDexEnabled true
  13. }
  14. dexOptions {
  15. javaMaxHeapSize &quot;4g&quot;
  16. }
  17. buildTypes {
  18. release {
  19. minifyEnabled true
  20. proguardFiles getDefaultProguardFile(&#39;proguard-android.txt&#39;), &#39;proguard-rules.pro&#39;
  21. }
  22. }
  23. }
  24. dependencies {
  25. implementation &#39;androidx.appcompat:appcompat:1.0.0&#39;
  26. implementation &#39;com.parse.bolts:bolts-tasks:1.4.0&#39;
  27. implementation &#39;com.parse:parse-android:1.17.3&#39;
  28. implementation &#39;androidx.multidex:multidex:2.0.0&#39;
  29. implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
  30. implementation &#39;com.google.android.material:material:1.0.0-beta01&#39;
  31. implementation &#39;javax.mail:javax.mail-api:1.5.3&#39;
  32. implementation group: &#39;com.sun.mail&#39;, name: &#39;javax.mail&#39;, version: &#39;1.6.2&#39;
  33. }
  34. configurations.all {
  35. resolutionStrategy.eachDependency { DependencyResolveDetails details -&gt;
  36. def requested = details.requested
  37. if (requested.group == &#39;com.android.support&#39;) {
  38. if (!requested.name.startsWith(&quot;multidex&quot;)) {
  39. details.useVersion &#39;25.3.1&#39;
  40. }
  41. }
  42. }
  43. }

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:

确定