"Google account: blocked login attempt" - Android App

1

Take a look at my code first

LoginActivity class

package com.softblue.sendemail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

    Button btnSubmit;
    EditText TxtPara, TxtTitulo, Txtmsg;

    Session session = null;
    ProgressDialog pdialog = null;
    Context context = null;

    String rec, subject, textMessage;

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

        context = this;
        btnSubmit = (Button) findViewById(R.id.btn_submit);

        TxtPara = (EditText) findViewById(R.id.edit_para);
        TxtTitulo = (EditText) findViewById(R.id.edit_titulo);
        Txtmsg = (EditText) findViewById(R.id.edit_corpo);

        btnSubmit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        rec = TxtPara.getText().toString();
        subject = TxtTitulo.getText().toString();
        textMessage = Txtmsg.getText().toString();

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]",// aqui é para colocar o seu email
                        "senha");//aqui é para colocar a senha do seu email
            }

        });

        pdialog = ProgressDialog.show(context, " ", "enviado o email", true);
        RetrieveFeedTask task = new RetrieveFeedTask();
        task.execute();

    }

    class RetrieveFeedTask extends AsyncTask<String, Void, String> {

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

            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(
                        "[email protected]"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(rec));
                message.setSubject(subject);
                message.setContent(textMessage, "text/html; charset=utf-8");
                Transport.send(message);

            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String result) {
            pdialog.dismiss();
            TxtPara.setText("");
            TxtTitulo.setText("");
            Txtmsg.setText("");
            Toast.makeText(getApplicationContext(),
                    "mensagem enviada com sucesso", Toast.LENGTH_LONG).show();

        }

    }

}

login_activity.xml

<EditText
        android:id="@+id/edit_para"
        android:hint="Enviar Para"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edit_titulo"
        android:hint="Itulo do Email"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <EditText
        android:id="@+id/edit_corpo"
        android:hint="Corpo do Email"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:onClick="enviar"
        android:layout_height="wrap_content"
        android:text="@string/button_enviar" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.softblue.sendemail"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I'm actually following this tutorial: link

When I first ran, I thought it worked because I got this email:

However,tryingtorunasecondtimedidnotwork.Isenttheapplicationtoafriendofmine,hetoldmethathetesteditonhiscellphoneanditworked.

Ibelieveit'seithersettingupmyGmailaccountoritmaybesomemissinginformationinmyLoginActivityalgorithm.

Iacceptsuggestions.

ThegoogleSMTPportsettingsarefromhere:

link

I tried both on and off

    
asked by anonymous 04.03.2015 / 13:37

1 answer

1

I needed to, but this answer worked for +400 people ... link

    
05.03.2015 / 17:28