1.Create a Class Mail_Utils
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class Mail_Utils {
/**
* @param address
* @param subject
* @param emailbody
* @param activity
* Function to send a e-mail.
*/
public static void sendEmail(String address,String subject,String emailbody,Activity activity){
if(!address.trim().equalsIgnoreCase("")){
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, address);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody);
activity.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
else{
Toast.makeText(activity, "Please enter an email address..", Toast.LENGTH_LONG).show();
}
}
}
2.Use this in your MainActivity
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mail_Utils.sendEmail("nikhil.v@xyz.com", "Test", "Hiii",MainActivity.this);
}
}
Now, are able to Send a Mail in Android programmatically.
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
public class Mail_Utils {
/**
* @param address
* @param subject
* @param emailbody
* @param activity
* Function to send a e-mail.
*/
public static void sendEmail(String address,String subject,String emailbody,Activity activity){
if(!address.trim().equalsIgnoreCase("")){
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, address);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody);
activity.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
else{
Toast.makeText(activity, "Please enter an email address..", Toast.LENGTH_LONG).show();
}
}
}
2.Use this in your MainActivity
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mail_Utils.sendEmail("nikhil.v@xyz.com", "Test", "Hiii",MainActivity.this);
}
}
Now, are able to Send a Mail in Android programmatically.
No comments:
Post a Comment