Vous êtes sur la page 1sur 7

5/2/2014

Android Sending Email

HOME

JAVA

PHP

Python

Ruby

Perl

HTML

CSS

Javascript

MySQL

C++

UNIX

MORE...

Android Sending Email


Advertisements

Previous Page

Android Basics
Android Home Android Overview Android Environment Setup Android Architecture Application Components Hello World Example Android Resources Android Activities Android Services Android Broadcast Receivers Android Content Providers Android Fragments Android Intents/Filters

You have learnt Android Intent, which is an object carrying an intent ie. message from one to another component with-in the application or outside the application.

As such you do not need to develop your email client from scratch because they are alrea like Gmail and K9mail. But you will need to send email from your Android application, wh have to write an Activity that needs to launch an email client and sends an email using y device. For this purpose, your Activity will send an ACTION_SEND along with appropriate the Android Intent Resolver. The specified chooser gives the proper interface for the user to send your email data. Following section explains different parts of our Intent object required to send an email.

Intent Object - Action to send Email

You will use ACTION_SEND action to launch an email client installed on your Android devic is simple syntax to create an intent with ACTION_SEND action I n t e n te m a i l I n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ S E N D ) ;

Intent Object - Data/Type to send Email

To send an email you need to specify mailto: as URI using setData() method and data ty text/plain using setType() method as follows: e m a i l I n t e n t . s e t D a t a ( U r i . p a r s e ( " m a i l t o : " ) ) ; e m a i l I n t e n t . s e t T y p e ( " t e x t / p l a i n " ) ;

Android User Interface


Android UI Layouts Android UI Controls Android Event Handling Android Styles and Themes Android Custom Components

Intent Object - Extra to send Email

Android has built-in support to add TO, SUBJECT, CC, TEXT etc. fields which can be atta intent before sending the intent to a target email client. You can use following extra fields in S.N. Extra Data & Description 1 2 3 EXTRA_BCC A String[] holding e-mail addresses that should be blind carbon copied. EXTRA_CC A String[] holding e-mail addresses that should be carbon copied. EXTRA_EMAIL A String[] holding e-mail addresses that should be delivered to.

Android Advanced Concepts


Android Drag and Drop Android Notifications Location Based Services

Android Sending Email


Android Sending SMS Android Phone Calls

EXTRA_HTML_TEXT A constant String that is associated with the Intent, used with ACTION_SEND to sup alternative to EXTRA_TEXT as HTML formatted text. EXTRA_SUBJECT A constant string holding the desired subject line of a message.
1/7

http://www.tutorialspoint.com/android/android_sending_email.htm

5/2/2014

Android Sending Email

Publishing Android Application

Android Useful Examples


Android Alert Dialoges Android Animations Android Audio Capture Android AudioManager Android Auto Complete Android Best Practices Android Bluetooth Android Camera Android Clipboard Android Custom Fonts Android Data Backup Android Developer Tools Android Emulator Android Facebook Integration Android Gestures Android Google Maps Android Image Effects Android ImageSwitcher Android Internal Storage Android JetPlayer Android JSON Parser Android Linkedin Integration Android Loading Spinner Android Localization Android Login Screen Android MediaPlayer Android Multitouch Android Navigation Android Network Connection Android NFC Guide Android PHP/MySQL Android Progress Circle Android ProgressBar Android Push Notification Android RenderScript 4 5 6 2 3 1 7

EXTRA_TEXT A constant CharSequence that is associated with the Intent, used with ACTION_SEN supply the literal data to be sent.

EXTRA_TITLE A CharSequence dialog title to provide to the user when used with a ACTION_CHOO

Here is an example showing you how to assign extra data to your intent:

e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ E M A I L ,n e wS t r i n g [ ] { " r e c i p i e n t @ e x a e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ S U B J E C T ," s u b j e c to fe m a i l " ) ; e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ T E X T ," b o d yo fe m a i l " ) ;

Example

Following example shows you in practical how to use Intent object to launch Email clien Email to the given recipients.

To experiment with this example, you will need actual Mobile device equipped with lat Android OS, otherwise you will have to struggle with emulator which may not work. Se will need to have an Email client like GMail or K9mail installed on your device. Step Description

You will use Eclipse IDE to create an Android application and name it as SendEma under a package com.example.sendemaildemo. While creating this project, make Target SDK and Compile With at the latest version of Android SDK to use higher lev APIs.

Modify src/MainActivity.java file and add required code to take care of sending emai

Modify layout XML file res/layout/activity_main.xml add any GUI component if require adding a simple button to launch Email Client. Modify res/values/strings.xml to define required constant values Modify AndroidManifest.xml as shown below Run the application to launch Android emulator and verify the result of the changes aplication. modified main

Following is the content of the src/com.example.sendemaildemo/MainActivity.java. p a c k a g ec o m . e x a m p l e . s e n d e m a i l d e m o ; i m p o r ta n d r o i d . n e t . U r i ; i m p o r ta n d r o i d . o s . B u n d l e ; i m p o r ta n d r o i d . a p p . A c t i v i t y ; i m p o r ta n d r o i d . c o n t e n t . I n t e n t ; i m p o r ta n d r o i d . u t i l . L o g ; i m p o r ta n d r o i d . v i e w . M e n u ; i m p o r ta n d r o i d . v i e w . V i e w ; i m p o r ta n d r o i d . w i d g e t . B u t t o n ; i m p o r ta n d r o i d . w i d g e t . T o a s t ; p u b l i cc l a s sM a i n A c t i v i t ye x t e n d sA c t i v i t y{

act

@ O v e r r i d e p r o t e c t e dv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ; s e t C o n t e n t V i e w ( R . l a y o u t . a c t i v i t y _ m a i n ) ;
2/7

http://www.tutorialspoint.com/android/android_sending_email.htm

5/2/2014

Android Sending Email

Android RSS Reader Android Screen Cast Android SDK Manager Android Sensors Android Session Management Android Shared Preferences Android SIP Protocol Android Spelling Checker Android SQLite Database Android Support Library Android Testing Android Text to Speech Android TextureView Android Twitter Integration Android UI Deign Android UI Patterns Android UI Testing Android WebView Layout Android Wi-Fi Android Widgets Android XML Parsers

B u t t o ns t a r t B t n=( B u t t o n )f i n d V i e w B y I d ( R . i d . s e n d E m a i l ) ; s t a r t B t n . s e t O n C l i c k L i s t e n e r ( n e wV i e w . O n C l i c k L i s t e n e r ( ){ p u b l i cv o i do n C l i c k ( V i e wv i e w ){ s e n d E m a i l ( ) ; } } ) ; } p r o t e c t e dv o i ds e n d E m a i l ( ){ L o g . i ( " S e n de m a i l " ," " ) ; S t r i n g [ ]T O={ " a m r o o d . a d m i n @ g m a i l . c o m " } ; S t r i n g [ ]C C={ " m c m o h d @ g m a i l . c o m " } ; I n t e n te m a i l I n t e n t=n e wI n t e n t ( I n t e n t . A C T I O N _ S E N D ) ; e m a i l I n t e n t . s e t D a t a ( U r i . p a r s e ( " m a i l t o : " ) ) ; e m a i l I n t e n t . s e t T y p e ( " t e x t / p l a i n " ) ;

e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ E M A I L ,T O ) ; e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ C C ,C C ) ; e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ S U B J E C T ," Y o u rs u b j e c t " ) ; e m a i l I n t e n t . p u t E x t r a ( I n t e n t . E X T R A _ T E X T ," E m a i lm e s s a g eg o e sh e r e

t r y{ s t a r t A c t i v i t y ( I n t e n t . c r e a t e C h o o s e r ( e m a i l I n t e n t ," S e n dm a i l . . . f i n i s h ( ) ; L o g . i ( " F i n i s h e ds e n d i n ge m a i l . . . " ," " ) ; }c a t c h( a n d r o i d . c o n t e n t . A c t i v i t y N o t F o u n d E x c e p t i o ne x ){ T o a s t . m a k e T e x t ( M a i n A c t i v i t y . t h i s , " T h e r ei sn oe m a i lc l i e n ti n s t a l l e d . " ,T o a s t . L E N G T H _ S H O R T }

Android Useful Resources


Android Quick Guide Android Useful Resources }

} @ O v e r r i d e p u b l i cb o o l e a no n C r e a t e O p t i o n s M e n u ( M e n um e n u ){ / /I n f l a t et h em e n u ;t h i sa d d si t e m st ot h ea c t i o nb a ri fi ti s g e t M e n u I n f l a t e r ( ) . i n f l a t e ( R . m e n u . m a i n ,m e n u ) ; r e t u r nt r u e ; }

Following will be the content of res/layout/activity_main.xml file:

Selected Reading
Developer's Best Practices Effective Resume Writing Computer Glossary Who is Who

< L i n e a r L a y o u tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " f i l l _ p a r e n t " a n d r o i d : o r i e n t a t i o n = " v e r t i c a l "> < B u t t o na n d r o i d : i d = " @ + i d / s e n d E m a i l " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t " a n d r o i d : t e x t = " @ s t r i n g / c o m p o s e _ e m a i l " / > < / L i n e a r L a y o u t > Following will be the content of res/values/strings.xml to define two new constants: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < r e s o u r c e s > < s t r i n gn a m e = " a p p _ n a m e " > S e n d E m a i l D e m o < / s t r i n g > < s t r i n gn a m e = " h e l l o _ w o r l d " > H e l l ow o r l d ! < / s t r i n g > < s t r i n gn a m e = " a c t i o n _ s e t t i n g s " > S e t t i n g s < / s t r i n g >

http://www.tutorialspoint.com/android/android_sending_email.htm

3/7

5/2/2014

Android Sending Email

< s t r i n gn a m e = " c o m p o s e _ e m a i l " > C o m p o s eE m a i l < / s t r i n g > < / r e s o u r c e s > Following is the default content of AndroidManifest.xml: < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < m a n i f e s tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " p a c k a g e = " c o m . e x a m p l e . s e n d e m a i l d e m o " a n d r o i d : v e r s i o n C o d e = " 1 " a n d r o i d : v e r s i o n N a m e = " 1 . 0 "> < u s e s s d k a n d r o i d : m i n S d k V e r s i o n = " 8 " a n d r o i d : t a r g e t S d k V e r s i o n = " 1 7 "/ > < a p p l i c a t i o n a n d r o i d : a l l o w B a c k u p = " t r u e " a n d r o i d : i c o n = " @ d r a w a b l e / i c _ l a u n c h e r " a n d r o i d : l a b e l = " @ s t r i n g / a p p _ n a m e " a n d r o i d : t h e m e = " @ s t y l e / A p p T h e m e "> < a c t i v i t y a n d r o i d : n a m e = " c o m . e x a m p l e . s e n d e m a i l d e m o . M a i n A c t i v i t y " a n d r o i d : l a b e l = " @ s t r i n g / a p p _ n a m e "> < i n t e n t f i l t e r > < a c t i o na n d r o i d : n a m e = " a n d r o i d . i n t e n t . a c t i o n . M A I N "

< c a t e g o r ya n d r o i d : n a m e = " a n d r o i d . i n t e n t . c a t e g o r y . L A U N C H < / i n t e n t f i l t e r > < / a c t i v i t y > < / a p p l i c a t i o n > < / m a n i f e s t >

Let's try to run your SendEmailDemo application. I assume you have connected your ac Mobile device with your computer. To run the app from Eclipse, open one of your project's and click Run

icon from the toolbar. Before starting your application, Eclipse will disp

window to select an option where you want to run your Android application.

http://www.tutorialspoint.com/android/android_sending_email.htm

4/7

5/2/2014

Android Sending Email

Select your mobile device as an option and then check your mobile device which will disp screen:

Now use Compose Email button to list down all the installed email clients. From the choose one of email clients to send your email. I'm going to use Gmail client to send my will have all the provided defaults fields available as shown below. Here From: will be defa you have registered for your Android device.

http://www.tutorialspoint.com/android/android_sending_email.htm

5/7

5/2/2014

Android Sending Email

You can modify either of the given default fields and finally use send email button (mark rectangle) to send your email to the mentioned recipients.

Previous Page
Advertisements

Print Version

http://www.tutorialspoint.com/android/android_sending_email.htm

6/7

5/2/2014

Android Sending Email

ASP.NET | jQuery | AJAX | ANT | JSP | Servlets | log4j | iBATIS | Hibernate | JDBC | Struts |

Copyright 2014 by tutorialspoint. All Rights Reserved.

http://www.tutorialspoint.com/android/android_sending_email.htm

7/7

Vous aimerez peut-être aussi