Note that there are some explanatory texts on larger screens.

plurals
  1. POcan't send mail using android code
    primarykey
    data
    text
    <p>I am trying to send a mail within application. I'm not gonna pop up any interactions with user. I have two java codes.</p> <p><code>MainActivity.java</code></p> <pre><code>package com.example.test; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { public static String EXTRA_MESSAGE = "com.example.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } public void sendMessage(View view) { try { test sender = new test(); sender.send(); Toast.makeText(getApplicationContext(), "Message Sent!!! :)", Toast.LENGTH_LONG).show(); } catch (Exception e) { //Log.e("SendMail", e.getMessage(), e); } } public void exit(View view) { System.exit(0); } } </code></pre> <p><code>test.java</code></p> <pre><code>package com.example.test; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import android.widget.Toast; public class test extends MainActivity { public void send() { String host = "smtp.gmail.com"; String from = "MYUSERNAME"; String pass = "MYPASS"; try { Properties props = new Properties(); 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"); String[] to = {"ADDRESSTOSEND@gmail.com"}; // added this line Session session = Session.getDefaultInstance(props, null); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); InternetAddress[] toAddress = new InternetAddress[to.length]; // To get the array of addresses for( int i=0; i &lt; to.length; i++ ) { // changed from a while loop toAddress[i] = new InternetAddress(to[i]); } System.out.println(Message.RecipientType.TO); for( int i=0; i &lt; toAddress.length; i++) { // changed from a while loop message.addRecipient(Message.RecipientType.TO, toAddress[i]); } message.setSubject("This is subject"); message.setText("Welcome to your mail"); Transport transport = session.getTransport("smtp"); transport.connect(host, from, pass); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch(Exception e) { Toast.makeText(getApplicationContext(), "Error" + e, Toast.LENGTH_LONG).show(); } } } </code></pre> <p>I can't send mail when i press the send button. I have invoked the sendMessage() using xml onClick.</p> <p>There are NO ERRORS in eclipse. The project compiles, installs but i don't receive the mail. When i try with just the java using <code>javac</code> and run the test.java, it works FINE. I get mail in my account.</p> <p>Thanks for helping me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload