Send Email from android aplication

Quickly share your Android Code Snippets here...

Send Email from android aplication

Postby swathi » Tue May 12, 2009 7:55 am

I have tried with the following code ,but i got an error message"NO applications can perform this action".


Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,
subject.getText().toString());
sendIntent.putExtra(Intent.EXTRA_TEXT,
body.getText().toString());
sendIntent.setType("text/plain");

startActivity(Intent.createChooser(sendIntent, "MySendMail"));

Can anybody tell me, the solution for this ?

I have tried to send an email using SMTP server (Gmail), but i got an error
"java.lang.verifyError:javax.mail.internet.Mimemessage."
swathi
Freshman
Freshman
 
Posts: 4
Joined: Tue May 12, 2009 7:39 am

Top

Postby SlumberMachine » Fri May 15, 2009 6:53 pm

First make sure that you have set up your email application, If you are trying this on the emulator there are some issues getting it to work correctly, but it does seem to work on devices.

Also you need to play around with the message type:
Try setting it to ".setType("text/csv");"
there are other types to set it to. I was able to get it to start an email using k9 and that type.
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Sending Email from android application

Postby swathi » Mon May 18, 2009 12:01 pm

Hello thank you 4 ur reply.

I have tried by setting setContentType("text/csv") ,but it shows same error. Can u please send me the code for sending email using k9.
swathi
Freshman
Freshman
 
Posts: 4
Joined: Tue May 12, 2009 7:39 am

Postby SlumberMachine » Mon May 18, 2009 7:38 pm

Well here is my code, it's pretty much the same as yours, but it works. It's not running great yet, I can't get the "to email address", "cc", and "bcc" fields to populate.

Also, again, this code did not work until after I set up the email program and got it property running first. I'm not 100% sure, but I don't think programs get intent receivers until after they are set up with valid information.

Code: Select all
                 Intent msg=new Intent(Intent.ACTION_SEND);
            msg.putExtra(Intent.EXTRA_EMAIL ,"testto@test.net");
            msg.putExtra(Intent.EXTRA_SUBJECT, "Here is the subject for the email");
                 //this next line adds an attachment, but I'm having some issues with the file location
            msg.putExtra(Intent.EXTRA_STREAM, Uri.parse ("file://" + Environment.getExternalStorageDirectory() + "test.txt"));
            msg.putExtra(Intent.EXTRA_BCC, "testbcc@test.net");
            msg.putExtra(Intent.EXTRA_CC, "testcc@test.net");
                 //This next line puts in the body of the message
            msg.putExtra(Intent.EXTRA_TEXT,"Attached is your file ");
            msg.setType("text/csv");
                 //Another type to try
            //msg.setType("message/rfc822"); 
            startActivity(Intent.createChooser(msg, "Send Email")); 
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Postby Skeniver » Fri Sep 25, 2009 4:08 pm

Hey SlumberMachine

I was wondering if you managed to get your email app code working? I have been trying the same thing, and it seems to populate the body, but I also can't get the address to populate...

Would be great if you could help me out

Thanks in advance,
Neil
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Postby SlumberMachine » Tue Sep 29, 2009 6:40 am

I still am unable to get the "to" and "cc" fields to populate. I tried all sorts of ways of doing this, I pretty much gave up on it and just require the users to manually type it in which is a big pain in the ...

So I need to find a solution for this at some point in the near future. Please let me know if you make any progress.

Thanks!
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Postby Skeniver » Tue Sep 29, 2009 9:21 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void sendEmail(String[] address, String subject, String msg) {
  2.  
  3.         Intent send = new Intent(Intent.ACTION_SEND);
  4.  
  5.         send.putExtra(Intent.EXTRA_EMAIL, address);
  6.  
  7.         send.putExtra(Intent.EXTRA_SUBJECT, subject);
  8.  
  9.         send.putExtra(Intent.EXTRA_TEXT, msg);
  10.  
  11.         send.setType("text/plain");
  12.  
  13.         startActivity(Intent.createChooser(send, "MySendMail"));
  14.  
  15.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Hey Slumber

The above code does work for me. It brings up the 'Choose which app to use' type dialog, and on choosing gmail, it has a mail filled in with the correct details... but I don't have a CC field (and haven't tried with one either)

It seemed a little odd, and just worked after some tinkering... hope it helps any

Neil
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

YAY IT WORKS!!!

Postby SlumberMachine » Fri Oct 09, 2009 10:00 am

So, I tried the code you posted Skeniver and the reason I wasn't able to auto populate the to,cc,bcc fields was because you have to use a string array (String[]) for the address, I guess so that you can put multiple addresses. It will only take this. Thanks a bunch! I hope this helps others out as well.
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests