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: https://www.youtube.com/watch?v=UNPFWCNMJPU
When I first ran it, I thought it worked because I got this email:
However, when trying to execute for the second time, it did not work. I sent the application to a friend of mine, he told me that tested on his cell phone and it worked.
I believe it’s setting up my Gmail account or it might be some information missing from my Loginactivity algorithm.
I accept suggestions.
Google’s SMTP port settings are from here:
http://www.serversmtp.com/pt-pt/servidor-smtp-gmail-configuracao
I tried both activated and disabled
goes there where it says "If it was you" on the link and releases the login for less secure applications
– Adir Kuhn
take a look at my post, I tried it two ways and it didn’t work.
– wladyband
I haven’t done with app yet, but I’ve worked with Javamail, and I had the same problem, for me it was enough to allow access as in the image above that worked, something else checks your anti virus @wladyband
– Wellington Avelino