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

Createing connction with 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 {

Athanticating the usser and passsword

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

Createing message

        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();
        }
    });
}

}

This My Build:

Executing tasks: [:app:assembleDebug] in project C:\Users\apk

Task :app:preBuild UP-TO-DATE Task :app:preDebugBuild UP-TO-DATE Task :app:compileDebugAidl NO-SOURCE Task :app:compileDebugRenderscript NO-SOURCE Task :app:generateDebugBuildConfig UP-TO-DATE Task :app:javaPreCompileDebug UP-TO-DATE Task :app:checkDebugAarMetadata UP-TO-DATE Task :app:generateDebugResValues UP-TO-DATE Task :app:generateDebugResources UP-TO-DATE Task :app:processDebugGoogleServices UP-TO-DATE Task :app:mergeDebugResources UP-TO-DATE Task :app:createDebugCompatibleScreenManifests UP-TO-DATE Task :app:extractDeepLinksDebug UP-TO-DATE Task :app:processDebugMainManifest UP-TO-DATE Task :app:processDebugManifest UP-TO-DATE Task :app:processDebugManifestForPackage UP-TO-DATE Task :app:processDebugResources UP-TO-DATE

Task :app:compileDebugJavaWithJavac Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details.

Task :app:compileDebugSources Task :app:mergeDebugNativeDebugMetadata NO-SOURCE Task :app:mergeDebugShaders UP-TO-DATE Task :app:compileDebugShaders NO-SOURCE Task :app:generateDebugAssets UP-TO-DATE Task :app:mergeDebugAssets UP-TO-DATE Task :app:compressDebugAssets UP-TO-DATE Task :app:processDebugJavaRes NO-SOURCE Task :app:mergeDebugJavaResource UP-TO-DATE Task :app:checkDebugDuplicateClasses UP-TO-DATE Task :app:desugarDebugFileDependencies UP-TO-DATE Task :app:mergeExtDexDebug UP-TO-DATE Task :app:mergeLibDexDebug UP-TO-DATE Task :app:dexBuilderDebug Task :app:mergeDebugJniLibFolders UP-TO-DATE Task :app:mergeDebugNativeLibs NO-SOURCE Task :app:stripDebugDebugSymbols NO-SOURCE Task :app:validateSigningDebug UP-TO-DATE Task :app:writeDebugAppMetadata UP-TO-DATE Task :app:writeDebugSigningConfigVersions UP-TO-DATE Task :app:mergeProjectDexDebug Task :app:packageDebug Task :app:assembleDebug

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 10s 28 actionable tasks: 4 executed, 24 up-to-date

Build Analyzer results available

Steve Vinoski
  • 19,572
  • 3
  • 28
  • 41
Murali
  • 1
  • Not posting the stacktrace of your NPE will not prevent you from having it closed as such... besides that, using `javax` classes on Android generally tends to be a whole lot more problematic, than it would be any useful. – Martin Zeitler Sep 29 '21 at 12:49

0 Answers0