Wednesday, December 17, 2008

Android SendMail - Send Email Programmatically

SendMail is really an excellent email program found on most Unix systems. I just borrowed the name for my tutorial on sending email programmatically from an Android phone.
Long ago, when I worked on the Palm OS, one could create an email message programmatically, and then place it in the Outbox of the Email application. Once the PDA was synced with the PC, the email would be sent. I was looking for a similar way to send email from the Android phone. After searching the google groups and scouring websites and android docs, I managed to piece together a simple example.

18 comments:

DILEEP SINGH said...

Thanks a lot Sunit.Again u ahve done a wonderful job.But this code is not working on android emulator(ver1.0r2).Eventhough email client is dere.From emulator using email client i can send mail but using code i am not able to do .Message is coming"No applications can perform this action".
Please tell whats the reason.?
We hope so that u will be keep on writting this type of tutorial .
Thanks a lot.
Dileep

Unknown said...

hi Sunit.


I have seen your Email sending example..

But can you please tell me, where that email message ill go.. how it will go to the entered receptient....

please let me know these details...

Cheers,
Rajendrakumar

Sunit said...

Raj,
Basically when you use the code I put up in the tutorial, when you click send, you get the defaul mail client 'Compose message' screen. Here the To, Subject and Body fields are prepopulated automatically. In my case, the GMail compose message opens up.

Try out the code and you will see that you can send email. I dont know if this works on the emulator though.

Jonapin said...

hi, im send mail, buy with format html, (intent.extra_text,<"html">), only text in mail, nothing html, help me thanks

Salman said...

Thanks for ur code..........
This code works but the email body and email subject are not copied to the android built in email intent.........

lucj said...

Hi,
thanks for your code. I still not manage to have it working on Android emulator...
Stupid question... does this allow to send an email internally (i mean without having email client poping ?).
thanks a lot,
Luc

Unknown said...

Hi Sunit,
thanks for the tutorial - it worked fine for me.
Except maybe that like one of the above I am actually looking for something that lets me send HTML-formatted mail or attach HTML documents. Obviously using setType("text/html") instead of setType("text/plain") does not do the job. Looks like my email programs (Googlemail & K9) don't support it. K9 doesen't even read the receipient correctly. I haven't found anything for adding attachments either. Have you got any hints ?
Cheers Thomas

Unknown said...
This comment has been removed by the author.
Jonapin said...

El Webview aun no soporta formato HTML, tendremos que esperar, yo dedique bastante tiempo en la investigacion y pruebas y creanme no funciona

Unknown said...

Hi Sunit,

This code is not working on Emulator it is giving this Exception

08-31 13:37:55.597: INFO/InetAddress(636): Unknown host mms.msg.eng.t-mobile.com, throwing UnknownHostException
08-31 13:37:55.607: ERROR/SendTransaction(636): java.io.IOException: Cannot establish route for http://mms.msg.eng.t-mobile.com/mms/wapenc: Unknown host
08-31 13:37:55.607: ERROR/SendTransaction(636): at com.android.mms.transaction.Transaction.ensureRouteToHost(Transaction.java:197)
08-31 13:37:55.607: ERROR/SendTransaction(636): at com.android.mms.transaction.Transaction.sendPdu(Transaction.java:142)
08-31 13:37:55.607: ERROR/SendTransaction(636): at com.android.mms.transaction.SendTransaction.run(SendTransaction.java:108)
08-31 13:37:55.607: ERROR/SendTransaction(636): at java.lang.Thread.run(Thread.java:1058)
08-31 13:37:55.626: ERROR/SendTransaction(636): Delivery failed.

Please let me know if you can help. I am trying to send this email to my Yahoo Mail.

Unknown said...

Hey Sunit,

This is fine, I guess, if you just want to prepopulate the standard eMail form; What I need is to send an email WITHOUT the additional step of the user hitting SEND again (from the email client).

Can you do that?

Sunit said...

Sorry folks, I have not been able to do any Android development since a long time. I am not in step with the latest SDK. Also my code was to demonstrate how to send email. I did not explore too much with rich HTML email, email attachments, etc.

Anonymous said...

Hi, nice tutorial, works good but. I need an app to get EMail atachment. Could you explain how to manage this?

Would be great =)

Many Greets

Unknown said...

Hi I have followed your tutorial and would like to ask a simple question.

How can i make the body text area bigger, at the moment it is just 1 line in height, how can i make it 5 lines in height? Hope you can help,

Thanks, Lucy

Pravin Pawar said...

please help me for attache the multiple images from gallery to email.

preethi said...

its working fine..
inorder to work this application in the emulator..jus open the email application which comes with the android sdk ..and sync ur login details..inorder to create an account..now that ur said to b logged in....
now open our email application and write to and sub and click on view send ..
and as we are using create chooser it'll ask for email and msg
use email inorder to send mail
and check out the mail ....

Anonymous said...

Your can promote your blog by using Internet marketing strategy, because these useful information is great resourceful to lots of android peoples.

Android app developer

R said...

Hi,

thank you for the post!

How about reading sent emails programatically? That's what I need to do.