-1

Here is the code that I am using: http://javapapers.com/android/android-email-app-with-gmail-smtp-using-javamail/

The error seems to be in the GMail.sendMail line where the transport tries to connect to gmail with a valid username and password.

I can't seem to find any sort of way around this and it's weird because about a month ago, this code worked perfectly.

DEBUG:

 04-23 21:21:41.526: I/System.out(27546): DEBUG: getProvider() returning  javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport, Sun   Microsystems, Inc.,1.4.1]
 04-23 21:21:41.526: I/System.out(27546): DEBUG SMTP: useEhlo true, useAuth true 
 04-23 21:21:41.526: I/System.out(27546): DEBUG SMTP: trying to connect to host  "smtp.gmail.com", port 587, isSSL false
 04-23 21:21:41.526: D/AndroidRuntime(27546): Shutting down VM

Logcat:

04-23 21:25:53.651: E/AndroidRuntime(28125): java.lang.IllegalStateException: Could not execute method of the activity
04-23 21:25:53.651: E/AndroidRuntime(28125): Caused by:java.lang.reflect.InvocationTargetException
04-23 21:25:53.651: E/AndroidRuntime(28125): at  android.view.View$1.onClick(View.java:3964) This is clicking a button
04-23 21:25:53.651: E/AndroidRuntime(28125): Caused by: android.os.NetworkOnMainThreadException
04-23 21:25:53.651: E/AndroidRuntime(28125): at  com.javapapers.android.androidjavamail.GMail.sendEmail(GMail.java:74)

Thanks guys!

Havelock
  • 6,913
  • 4
  • 34
  • 42
user1883614
  • 905
  • 3
  • 16
  • 30

3 Answers3

0

"NetworkOnMainThreadException" occured in your application. You MUST implement any network operions on background thread. it may make ANR if you work on MainThread like Activity, Service, etc. Please use "AsyncTask" or "IntentService" to send email.

Please look at this answer. How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Horyun Lee
  • 1,083
  • 8
  • 9
0

If you are getting this problem then you should check your api level-

Till Donut it is used to create multiple threads from 1.6-2.3 that you was using before but if now you are using 3.0 or above then it is used to create single threaded model by using AsyncTask otherwise you ll get the NetworkOnMainThreadException.

Rohit Goswami
  • 617
  • 5
  • 17
0

1.Use the getCause() method on the InvocationTargetException to retrieve the original exception.

2.From the Javadoc of Method.invoke()

Throws: InvocationTargetException - if the underlying method throws an exception.

This exception is throw if the method called threw an exception.

you got this exception due to:

  (i)List all jar files from the Eclipse Navigator mode

  (ii)Verify that all the jar files are in binary mode.

3.you can use the below code to send email from you android client.

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Authenticator;import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EMailSender {
public EMailSender(String host, final String from, final String pass, String to, String  sub, String mess) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}};
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(mess);
Transport.send(message);
}

public static void main(String arg[]) throws Exception {
if(arg.length == 5) {
StringBuilder message = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = "", subject;
System.out.print("Enter subject: ");
subject = br.readLine();
System.out.println("Enter the message (end it with a . on a single line):");
while((temp = br.readLine()) != null) {
if(temp.equals("."))
break;
message.append(temp+"\n");
}
System.out.println("Sending message...");
new EMailSender(arg[0], arg[1], arg[2], arg[3], subject, message.toString());
System.out.println("Sent the message.");
  }
else System.err.println("Usage:\njava SendTextMail <host> <port> <from> <pass> <to>");
}
}

Call Constructor Of This Class In Your Activity On Button Click.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59