title: sending email without user interaction android
body: <p>I was trying to send email without user interaction. every thing worked until the part for sending. </p>
  
  <p>This is the sendemail function in the Gmailsender class that I am using </p>
  
  <pre><code>public void sendEmail() throws MessagingException 
  {
      Log.i("check","start");
  
  String host = "smtp.gmail.com";
  String from = "blabla@gmail.com"; //sender email, this is our website email
  String pass = "blablabla"; //password of sender email
  
  Properties props = System.getProperties();
  props.put("mail.smtp.starttls.enable", "true"); // added this line
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.user", from);
  props.put("mail.smtp.password", pass);
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.auth", "true");
  Log.i("check","done pops ");
  
  
  //creating session
  Session session = Session.getDefaultInstance(props, null);
  MimeMessage message = new MimeMessage(session);
  message.setFrom(new InternetAddress(from));
  Log.i("check","done sessions ");
  
  InternetAddress toAddress;     
      toAddress = new InternetAddress(to);    
      message.addRecipient(Message.RecipientType.TO, toAddress);
      Log.i("check","add recipante ");
  
  message.setSubject(subject);
  message.setText("This is my app");
  
  
  Log.i("check","transport");
  
  Transport transport = session.getTransport("smtp");
  
  //connecting..
  Log.i("check","connecting");
  transport.connect(host, from, pass);
  //sending...
  Log.i("check","wana send");
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
  Log.i("check","sent");
  </code></pre>
  
  <p>}</p>
  
  <p>The log "wanna send" doesn't appear..</p>
  
  <p>The main activity that I am creating an object from the gmailSender has the following code: </p>
  
  <pre><code> send = (Button) this.findViewById(R.id.button1);
      send.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              // TODO Auto-generated method stub
              try {   
                  GMailSender sender = new GMailSender("blabla@gmail.com", "blablabla");
                  Log.e("check", "test 1");
                  sender.sendEmail() ;
                  Log.e(DEBUG_TAG, "test 2");  
              } catch (Exception e) {   
                  Log.e(DEBUG_TAG, "test 3");                  } 
  </code></pre>
  
  <p>I am getting the "test 3" log which means that the exception happened.. </p>
  
  <p>The result is that the email is not sent and the app is stuck... I dont know what is the wrong..</p>
  
  <p>NOTE: I added the internet permission + I added the jar files </p>
  
  <p>The logcat is </p>
  
  <pre><code>12-25 12:55:00.774: E/check(1350): test 1
  12-25 12:55:00.774: I/check(1350): start
  12-25 12:55:00.774: I/check(1350): done pops 
  12-25 12:55:00.774: I/check(1350): done sessions 
  12-25 12:55:00.774: I/check(1350): add recipante 
  12-25 12:55:00.779: I/check(1350): transport
  12-25 12:55:00.789: I/check(1350): connecting
  12-25 12:55:00.789: E/check(1350): test 3
  </code></pre>
  
  <p>this is the whole logcat for the check tag :</p>
  
  <pre><code>12-25 13:20:18.698: E/check(5547): test 1
  12-25 13:20:18.698: I/check(5547): start
  12-25 13:20:18.698: I/check(5547): done pops 
  12-25 13:20:18.723: I/check(5547): done sessions 
  12-25 13:20:18.723: I/check(5547): add recipante 
  12-25 13:20:18.723: I/check(5547): transport
  12-25 13:20:18.733: I/check(5547): connecting
  12-25 13:20:18.758: E/check(5547): [Ljava.lang.StackTraceElement;@4193bb00
  12-25 13:20:18.758: E/check(5547): android.os.NetworkOnMainThreadException
  12-25 13:20:18.758: E/check(5547):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
  12-25 13:20:18.758: E/check(5547):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
  12-25 13:20:18.758: E/check(5547):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
  12-25 13:20:18.758: E/check(5547):  at java.net.InetAddress.getByName(InetAddress.java:295)
  12-25 13:20:18.758: E/check(5547):  at java.net.InetSocketAddress.&lt;init&gt;(InetSocketAddress.java:105)
  12-25 13:20:18.758: E/check(5547):  at java.net.InetSocketAddress.&lt;init&gt;(InetSocketAddress.java:90)
  12-25 13:20:18.758: E/check(5547):  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
  12-25 13:20:18.758: E/check(5547):  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
  12-25 13:20:18.758: E/check(5547):  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
  12-25 13:20:18.758: E/check(5547):  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
  12-25 13:20:18.758: E/check(5547):  at javax.mail.Service.connect(Service.java:288)
  12-25 13:20:18.758: E/check(5547):  at javax.mail.Service.connect(Service.java:169)
  12-25 13:20:18.758: E/check(5547):  at com.example.sendmail.GMailSender.sendEmail(GMailSender.java:100)
  12-25 13:20:18.758: E/check(5547):  at com.example.sendmail.MailSenderActivity$1.onClick(MailSenderActivity.java:31)
  12-25 13:20:18.758: E/check(5547):  at android.view.View.performClick(View.java:3627)
  12-25 13:20:18.758: E/check(5547):  at android.view.View$PerformClick.run(View.java:14329)
  12-25 13:20:18.758: E/check(5547):  at android.os.Handler.handleCallback(Handler.java:605)
  12-25 13:20:18.758: E/check(5547):  at android.os.Handler.dispatchMessage(Handler.java:92)
  12-25 13:20:18.758: E/check(5547):  at android.os.Looper.loop(Looper.java:137)
  12-25 13:20:18.758: E/check(5547):  at android.app.ActivityThread.main(ActivityThread.java:4511)
  12-25 13:20:18.758: E/check(5547):  at java.lang.reflect.Method.invokeNative(Native Method)
  12-25 13:20:18.758: E/check(5547):  at java.lang.reflect.Method.invoke(Method.java:511)
  12-25 13:20:18.758: E/check(5547):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
  12-25 13:20:18.758: E/check(5547):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
  12-25 13:20:18.758: E/check(5547):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>