Java 电子邮件不会通过点击android上的gmail smtp服务器发送

Java 电子邮件不会通过点击android上的gmail smtp服务器发送,java,android,android-studio,smtp,jakarta-mail,Java,Android,Android Studio,Smtp,Jakarta Mail,你好,我正在开发一个应用程序,用户可以通过填写一些信息注册 我想把用户名、密码和随机生成的代码从我自己的电子邮件发送到他指定的电子邮件地址 我不是在使用意图来完成这个操作,而是在点击按钮时直接发送邮件 为此,我正在使用gmail smtp服务器配置 我的代码向我显示成功消息,并根据我的应用程序移动到下一个活动,但电子邮件不再发送 这是我的密码 //On button click send email to user signUpBtn.setOnClickListener(new View.On

你好,我正在开发一个应用程序,用户可以通过填写一些信息注册

我想把用户名、密码和随机生成的代码从我自己的电子邮件发送到他指定的电子邮件地址

我不是在使用意图来完成这个操作,而是在点击按钮时直接发送邮件

为此,我正在使用gmail smtp服务器配置

我的代码向我显示成功消息,并根据我的应用程序移动到下一个活动,但电子邮件不再发送

这是我的密码

//On button click send email to user
signUpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkValidation()){
                    sendpinCodeToMail();
                }


//send user email without opening an intent
    //send user details to given email address without opening an intent
    public void sendpinCodeToMail(){
        Properties properties=new Properties();
        properties.put("mail.smtp.host","smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port","465");
        properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth","true");
        properties.put("mail.smtp.port","465");

        session=javax.mail.Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("myEmailAddress","myGooglePassword");
            }
        });
        retrivefeedTask task=new retrivefeedTask();
        task.execute();

    }
}
    });


//And here is the class where i am sending email in the background

//class to handle data to send in background email
     public class retrivefeedTask extends AsyncTask<String,Void,String> {

        @Override
        protected String doInBackground(String... strings) {

            Random random=new Random();
            int randomNo=random.nextInt(1111);
            String Subject = "Verify Your MyLife Account";
            String _Massage="Your My Life Verification Code is '"+randomNo+"' \n " +
                    "Your email address is " + email+
                    "\n Your password is "+password ;
            ///
            SharedPreferences sharedPreferences=getSharedPreferences("pinCode",MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("pin",""+randomNo);
            editor.apply();

            ///
            try {
                Message message= new MimeMessage(session);
                message.addFrom(new InternetAddress[]{new InternetAddress("My Life")});
                InternetAddress[] iAdressArray = InternetAddress.parse(email);
                message.setRecipient(Message.RecipientType.TO,new InternetAddress(email));
                message.setSubject(Subject);
                message.setContent(_Massage,"text/html;charset=utf-8");
                Transport.send(message);


                Log.d("Mail ","Send");
                // Toast.makeText(context,"Mail send",Toast.LENGTH_LONG).show();
            }catch (Exception e){
                Log.d("Mail ","Not Send"+"\n Error :"+e);
                //  Toast.makeText(getApplicationContext(),"error:"+e.toString(),Toast.LENGTH_LONG).show();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
           // progressDialog.dismiss();
            Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getApplicationContext(),CodeVerificationActivity.class);
            intent.putExtra("name",nameEt.getText().toString());
            intent.putExtra("email",emailEt.getText().toString());
            intent.putExtra("password",passwordEt.getText().toString());
            intent.putExtra("phone",phoneEt.getText().toString());
            startActivity(intent);
            finish();

            super.onPostExecute(s);
        }
    }
//在按钮上单击向用户发送电子邮件
signUpBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(checkValidation()){
sendpinCodeToMail();
}
//发送用户电子邮件而不打开意图
//将用户详细信息发送到给定的电子邮件地址,而无需打开意图
public void sendpinCodeToMail(){
属性=新属性();
properties.put(“mail.smtp.host”、“smtp.gmail.com”);
properties.put(“mail.smtp.socketFactory.port”,“465”);
properties.put(“mail.smtp.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
properties.put(“mail.smtp.auth”、“true”);
properties.put(“mail.smtp.port”,“465”);
session=javax.mail.session.getDefaultInstance(属性,新验证器(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码验证(“myEmailAddress”、“myGooglePassword”);
}
});
RetrieveFeedTask任务=新的RetrieveFeedTask();
task.execute();
}
}
});
//这是我在后台发送电子邮件的课程
//类来处理要在后台电子邮件中发送的数据
公共类RetrieveFeedTask扩展了AsyncTask{
@凌驾
受保护的字符串背景(字符串…字符串){
随机=新随机();
int randomNo=random.nextInt(1111);
String Subject=“验证您的MyLife帐户”;
字符串“按摩=“您的我的生命验证码是”“+randomNo+”\n”+
“您的电子邮件地址是”+电子邮件+
“\n您的密码是”+密码;
///
SharedReferences SharedReferences=GetSharedReferences(“pinCode”,MODE_PRIVATE);
SharedReferences.Editor=SharedReferences.edit();
编辑器.putString(“pin”和“+no”);
editor.apply();
///
试一试{
Message Message=新的mimessage(会话);
message.addFrom(newinternetaddress[]{newinternetaddress(“我的生活”)});
InternetAddress[]iAdressArray=InternetAddress.parse(电子邮件);
message.setRecipient(message.RecipientType.TO,新的Internet地址(电子邮件));
message.setSubject(主题);
message.setContent(_,“text/html;charset=utf-8”);
传输。发送(消息);
日志d(“邮件”、“发送”);
//Toast.makeText(上下文,“邮件发送”,Toast.LENGTH_LONG).show();
}捕获(例外e){
Log.d(“邮件”,“不发送”+”\n错误:+e);
//Toast.makeText(getApplicationContext(),“错误:+e.toString(),Toast.LENGTH_LONG).show();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
//progressDialog.disclose();
Toast.makeText(getApplicationContext(),“Success”,Toast.LENGTH\u SHORT.show();
Intent Intent=新意图(getApplicationContext(),CodeVerificationActivity.class);
intent.putExtra(“name”,namet.getText().toString());
intent.putExtra(“email”,emailEt.getText().toString());
intent.putExtra(“密码”,passwordEt.getText().toString());
intent.putExtra(“phone”,phoneEt.getText().toString());
星触觉(意向);
完成();
super.onPostExecute(s);
}
}

要防止未经授权访问您的电子邮件,您需要登录到Gmail.com上的帐户,然后打开另一个选项卡,进入不太安全的应用程序设置,然后选择“打开”。Aj Styles Loss Security for my email address(我的电子邮件地址)已经打开。修复这些问题,然后发布。