sendEmail method in dynamically created button? - android

I dynamically created buttons and I want to send an email by clicking on these buttons, but something goes wrong. Nothing happens. :( Please help me, I am beginner.
AdapterListingOrders:
public class AdapterListingOrders extends AppCompatActivity {
private Button btnAccRejOrders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adapter_listing_orders);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btnAccRejOrders = (Button) findViewById(R.id.order_acc_rej);
btnAccRejOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
}
});
}
}
But onClick does not work. When I press the btnAccRejOrders, LogCat display:
ViewPostImeInputStage processPointer 0
ViewPostImeInputStage processPointer 1

btnAccRejOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Send email", "");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
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();
}
}
});

Related

Android Intent (or code) to open email composer in (outlook, gmail)

I would like to know how to open the Activity for composing email in gmail or outlook. By now I only know ho to be redirected to for instance gmail but I would also open the "composer" (or whatever) and add an email address. I've already tried with:
intent.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
but doesnot work
full source to the test-app
public class MainActivity extends AppCompatActivity {
private static final String GOOGLE_MAIL = "com.google.android.gm";
private static final String OUTLOOK = "com.microsoft.office.outlook";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test(GOOGLE_MAIL);
}
private void test(String mail) {
Intent intent = getPackageManager().getLaunchIntentForPackage(mail);
intent.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
startActivity(intent);
}
}
You can try this:
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {"someone#gmail.com"};
String[] CC = {"xyz#gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}

send email from android app issue with subject and bcc

hello i am trying to send an email from an android application , the below code is working fine since it is opening the email app configured on the phone but the problem is it is not taking the subject and the Bcc any ideas
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, CC.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
As you can see in the getting the address you got the text by .getText(); then you made it a string for other uses, because it was an editable text from editText. But in the other EditTextss you forgot to add .toString(); so you can use it as a string in email.
try this:
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText().toString());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody.getText().toString());
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, CC.getText().toString());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
please ask if you didn't understand
Try
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText().toString());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody.getText().toString());
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, CC.getText().toString());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
EditText.getText() return a editable you need to change it to String by applying toString();
Will work 100%
String[] to = value.toString().split(",");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/html");
i.putExtra(Intent.EXTRA_EMAIL, to);
i.putExtra(Intent.EXTRA_BCC, "array of bcc like to");
i.putExtra(Intent.EXTRA_SUBJECT, String.format(getString(R.string.share_email_subject), IN_SHARE_CAPTION));
i.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(IjoomerUtilities.prepareEmailBody(message == null ? "" : message, getSmartApplication().readSharedPreferences().getString(SP_USERNAME, "") + " " + getString(R.string.saw_this_story_on_the) + " "
+ getString(R.string.app_name) + " " + getString(R.string.thought_you_should_see_it), IN_SHARE_CAPTION, IN_SHARE_DESCRIPTION, IN_SHARE_SHARELINK, getString(R.string.try_ijoomeradvance), getString(R.string.site_url))));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
ting(getString(R.string.share_email_no_client));
}

Can't add email signature in Android

I'm trying to compose and send an email which will include a signature at the bottom
of my email content in android. I'm able to send an email but I'm not getting the way that allows me to add my own signature. Do you have any suggestion?
here's my code:
public void addListener() {
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById = (TextView) findViewById(R.id.t1);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "some#gmail.com"});
/*i.putExtra(Intent.EXTRA_CC,
new String[] { "some#gmail.com" });*/
i.putExtra(Intent.EXTRA_SUBJECT, "Android Test");
i.putExtra(Intent.EXTRA_TEXT, "Body");
//i.putExtra(Intent.EXTRA_TEXT, "signature");
try {
startActivity(Intent.createChooser(i, "Choose mail app..."));
} catch (android.content.ActivityNotFoundException ex) {
}
}
});
}
I don't think the "signature" is really a different section of an email. I think you can just append your signature to your body.
i.putExtra(Intent.EXTRA_TEXT, "Body" + "\\n" + "Signature");

Email Composer in android

In my application i want send a email to one account.in one button click event.i used the below code . this have exception
public void onClick(View v) {
sendEmail(context, new String[]{"jayampalaniraja#gmail.com"}, "Sending Email", "Test Email", "I am body");
}
private void sendEmail(Context context, String[] recipientList,
String subject, String body, String title) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try
{
context.startActivity(Intent.createChooser(emailIntent, title));
}catch(Exception e)
{
System.out.println(e);
}
}
The exception I am getting as
"03-19 19:13:19.553: I/System.out(2010): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.
Try this
private void sendEmail(Context context, String[] recipientList,
String subject, String body, String title) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
context.startActivity(Intent.createChooser(emailIntent, title));
}catch(Exception e)
{
System.out.println(e);
}
}
It's working fine. first thing to configure a email client for your emulator.enter code here
public void onClick(View v) {
// TODO Auto-generated method stub
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String aEmailList[] = { "pal#Yahoo.co.in" };
//String aEmailCCList[] = { ""};
//String aEmailBCCList[] = { "" };
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
//emailIntent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
//emailIntent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
emailIntent.setType("message/rfc822");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body.");
try
{
startActivity(emailIntent);
}catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(about.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
catch(Exception e){ System.out.println(e);}
}

Issue sending mail attachment

I am sending mail through my application mail is sent but the attached text is not sent with it.
public class Email extends Activity {
Button send;
EditText address, subject, emailtext;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
send=(Button) findViewById(R.id.btnsubmitShare);
address=(EditText) findViewById(R.id.edittexttoShare);
subject=(EditText) findViewById(R.id.edittextsubjectShare);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("application/octet-stream");
// final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
});
}
}
From this application user can share a link, although mail is sent from it but text is empty
The problem is you are doing
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
when you should make a call to toString(), so it's going to be:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText().toString());

Categories

Resources