Reputation: 623
has everyone know how to sending an email from android device like mailto: in PHP?? i really need that for my lockscreen application.. it will send a email (email must be registered before) to the client when his/her forgot his/her password. what should i do?? any idea?? thanks..
package com.application.outgoingemail;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class EmailService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startid) {
//Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
sendEmail();
}
@Override
public void onDestroy() {
}
public void sendEmail()
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(EmailService.this,ex.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(EmailService.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
/*try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (Exception ex) {
Toast.makeText(EmailService.this,ex.getMessage(), 10).show();
//Toast.makeText(EmailService.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}*/
}
}
and on the mainForm :
package com.application.outgoingemail;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button Button1,Button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button1=(Button)findViewById(R.id.button1);
Button1.setOnClickListener(this);
Button2=(Button)findViewById(R.id.button2);
Button2.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
startService(new Intent(main.this, EmailService.class));
break;
case R.id.button2:
stopService(new Intent(main.this, EmailService.class));
break;
default:
break;
}
}
}
Upvotes: 0
Views: 1703
Reputation: 22076
you can do by this way ::
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Upvotes: 3