简体   繁体   中英

my program not send mail without user interaction in android

code is running but the problem is that im able to send mail only from one email id, if i try to send mail from another email id then mail do not send. when i configured own acc to send mail then it works properly but for other email acc it do not works .

public class MainActivity extends Activity {

String username="example@gmail.com";
String password="********";
String subject="registration confirmation";
String messageBody="Thanks for installing app";
String email="example1@gmail.com";


public void send(View view)
{
    sendMail(email,subject,messageBody);
}




private void sendMail(String email, String subject, String messageBody) {
    Session session = createSessionObject();

    try {
        Message message = createMessage(email, subject, messageBody, session);
        new SendMailTask().execute(message);
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

private Session createSessionObject() {

    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");

    return Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
}

private Message createMessage(String email, String subject,String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(username,username));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email,email));
    message.setSubject(subject);
    message.setText(messageBody);
    return message;
}

private class SendMailTask extends AsyncTask<Message, Void, Void> {
    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Sending mail", true, false);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressDialog.dismiss();
        Context context = getApplicationContext();
        CharSequence text = "u r successfully registered ";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

    }

    @Override
    protected Void doInBackground(Message... messages) {
        try {
            Transport.send(messages[0]);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return null;
    }
}


@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.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

the problem was the authentication of other email id's which i was trying to use for sending mails.. solution is if ur using gmail for sending mails then

  1. login your gmail id
  2. Go to Account
  3. find "Access for less secure apps" under signing in
  4. just change this option to allowed..

    Now u will be able to send mail from different gmail id's with correct username, password details..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM