首页 > 解决方案 > 这是我在 android studio 中发送 otp 的代码但是当我调用 fn() otp 时应用程序崩溃

问题描述

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



 public void otpview(View w) {
    otp();

}


public void otp() {

otp = (int) (Math.random() * (9999 - 1000 + 1) + 1000);

String to = "senderemail@gmail.com";

  Properties properties = new Properties();
try {

创建与 smtp 的连接

  properties.put("mail.smtp.auth", "true");
  properties.put("mail.smtp.starttls.enable", "true");
  properties.put("mail.smtp.host", "smtp.gmail.com");
  properties.put("mail.smtp.port", "587");

 }catch (Exception e){
    e.printStackTrace();
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(OTPActivity.this, "At Properties", Toast.LENGTH_LONG).show();
        }
    });

}
try {

验证用户名和密码

    Session s = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("frommail@gmail.com","password");
        }
    });
    try {

创建消息

        Message message = new MimeMessage(s);
        message.setFrom(new InternetAddress("frommail@gmail.com", "Name"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(" One Time Password");
        message.setText("Your OTP Is:" + otp + "Thank You For US");
        Transport.send(message);
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(OTPActivity.this, "Mail sent successfully", 
               Toast.LENGTH_LONG).show();
            }
        });
       
    } catch (Exception e) {

        e.printStackTrace();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(OTPActivity.this, e.getClass() + " : " + e.getMessage(), 
                  Toast.LENGTH_LONG).show();
            }
        });
    }
}catch (Exception e){
    e.printStackTrace();
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(OTPActivity.this, "At Login", Toast.LENGTH_LONG).show();
        }
    });
}

}

这是我的构建:

执行任务:项目 C:\Users\apk 中的 [:app:assembleDebug]

任务 :app:preBuild 最新任务 :app:preDebugBuild 最新任务 :app:compileDebugAidl NO-SOURCE 任务 :app:compileDebugRenderscript NO-SOURCE 任务 :app:generateDebugBuildConfig 最新任务 :app: javaPreCompileDebug 最新任务 :app:checkDebugAarMetadata 最新任务 :app:generateDebugResValues 最新任务 :app:generateDebugResources 最新任务 :app:processDebugGoogleServices 最新任务 :app: mergeDebugResources 最新任务 :app:createDebugCompatibleScreenManifests 最新任务 :app:extractDeepLinksDebug 最新任务 :app:processDebugMainManifest 最新任务 :app:processDebugManifest 最新任务 :app: processDebugManifestForPackage 最新任务:app:processDebugResources 最新

任务 :app:compileDebugJavaWithJavac 注意:一些输入文件使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。

任务 :app:compileDebugSources 任务 :app:mergeDebugNativeDebugMetadata NO-SOURCE 任务 :app:mergeDebugShaders UP-TO-DATE 任务 :app:compileDebugShaders NO-SOURCE 任务 :app:generateDebugAssets UP-TO-DATE 任务 :app:mergeDebugAssets UP-TO-日期任务 :app:compressDebugAssets 最新任务 :app:processDebugJavaRes 无源任务 :app:mergeDebugJavaResource 最新任务 :app:checkDebugDuplicateClasses 最新任务 :app:desugarDebugFileDependencies 最新任务:app:mergeExtDexDebug 最新任务 :app:mergeLibDexDebug 最新任务 :app:dexBuilderDebug 任务 :app:mergeDebugJniLibFolders 最新任务 :app:mergeDebugNativeLibs 无源任务 :app:stripDebugDebugSymbols 无源任务 :app:validateSigningDebug 最新任务 :app:writeDebugAppMetadata 最新任务 :app:writeDebugSigningConfigVersions 最新任务 :app:mergeProjectDexDebug 任务 :app:packageDebug 任务 :app:assembleDebug

此构建中使用了已弃用的 Gradle 功能,使其与 Gradle 8.0 不兼容。使用“--warning-mode all”显示各个弃用警告。请参阅https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

在 10 秒内构建成功 28 个可操作的任务:4 个已执行,24 个是最新的

构建分析器结果可用

标签: javaandroidandroid-studioemailone-time-password

解决方案


推荐阅读