When Google published the Android mobile phone platform, it emphasized the powerful network support capabilities. Therefore, no matter through GPRS, 3G telecom network, or Wi-Fi wireless WLAN Network, it can send emails.
The Intent action used in the sent email is android. content. Intent. ACTION_SEND. In fact, the mail sending service used on Android calls the Gmail program instead of directly using the SMTP Protocol.
Now we will introduce the list of functions to be used in this article:
• Verify whether the user input is in the correct email format;
• You can enter the email address manually or press the email address text box to find the contact and get the email address of the contact;
• Send an email.
• Method 1
The email sending program is not complex. It is mainly built in the EditText and Button controls. By constructing a custom Intent (android. content. intent. ACTION_SEND) is used as the Activity for sending emails. In this Intent, setType () must also be used to determine the Email format, and putExtra () must be used to place the mail in (EXTRA_EMAIL), subject (EXTRA_SUBJECT), Email content (EXTRA_TEXT), and other Email fields (EXTRA_BCC, EXTRA_CC ).
The Code is as follows:
MyButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent mailIntent = new Intent (android. content. Intent. ACTION_SEND );
MailIntent. setType ("plain/test ");
StrEmailReciver = new String [] {myEditText. getText (). toString ()};
StrEmailCC = new String [] {myEditText2.getText (). toString ()};
StrEmailSubject = myEditText3.getText (). toString ();
StrEmailBody = myEditText4.getText (). toString ();
MailIntent. putExtra (android. content. Intent. EXTRA_EMAIL, strEmailReciver );
MailIntent. putExtra (android. content. Intent. EXTRA_CC, strEmailCC );
MailIntent. putExtra (android. content. Intent. EXTRA_SUBJECT, strEmailSubject );
MailIntent. putExtra (android. content. Intent. EXTRA_TEXT, strEmailBody );
StartActivity (Intent. createChooser (mailIntent, getResources (). getString (R. string. send )));
}
});
There are many ways to write emails in Android. This example is just one of them. We will share other methods with you as follows:
• Method 2
Uri uri = Uri. parse ("mailto: terryyhl@gmail.com ");
Intent MymailIntent = new Intent (Intent. ACTION_SEND, uri );
StartActivity (MymailIntent );
• Method 3
Intent testintent = new Intent (Intent. ACTION_SEND );
String [] tos = {"terryyhl@gmail.com "};
String [] ccs = {"kalaicheng@hotmail.com "};
Testintent. putExtra (Intent. EXTRA_EMAIL, tos );
Testintent. putExtra (Intent. EXTRA_CC, ccs );
Testintent. putExtra (Intent. EXTRA_TEXT, "this is content ");
Testintent. putExtra (Intent. EXTRA_SUBJECT, "this is the title ");
Testintent. setType ("message/rfc822 ");
StartActivity (Intent. createChooser (testintent, "send "));
• Method 4: Upload attachments. Here we use the SD card music file as an example.
Intent testN = new Intent (Intent. ACTION_SEND );
TestN. putExtra (Intent. EXTRA_SUBJECT, "title ");
TestN. putExtra (Intent. EXTRA_STREAM, "file: // sdcard/musicloud ");
StartActivity (Intent. createChooser (testN, "send "));
The email sending program is not complex. It is mainly built in the EditText and Button controls. By constructing a custom Intent (android. content. intent. ACTION_SEND) is used as the Activity for sending emails. In this Intent, setType () must also be used to determine the Email format, and putExtra () must be used to place the mail in (EXTRA_EMAIL), subject (EXTRA_SUBJECT), Email content (EXTRA_TEXT), and other Email fields (EXTRA_BCC, EXTRA_CC ). The Code is as follows:
MyButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent mailIntent = new Intent (android. content. Intent. ACTION_SEND );
MailIntent. setType ("plain/test ");
StrEmailReciver = new String [] {myEditText. getText (). toString ()};
StrEmailCC = new String [] {myEditText2.getText (). toString ()};
StrEmailSubject = myEditText3.getText (). toString ();
StrEmailBody = myEditText4.getText (). toString ();
MailIntent. putExtra (android. content. Intent. EXTRA_EMAIL, strEmailReciver );
MailIntent. putExtra (android. content. Intent. EXTRA_CC, strEmailCC );
MailIntent. putExtra (android. content. Intent. EXTRA_SUBJECT, strEmailSubject );
MailIntent. putExtra (android. content. Intent. EXTRA_TEXT, strEmailBody );
StartActivity (Intent. createChooser (mailIntent, getResources (). getString (R. string. send )));
}
});
There are many ways to write emails in Android. This example is just one of them. We will share other methods with you as follows:
• Method 2
Uri uri = Uri. parse ("mailto: terryyhl@gmail.com ");
Intent MymailIntent = new Intent (Intent. ACTION_SEND, uri );
StartActivity (MymailIntent );
• Method 3
Intent testintent = new Intent (Intent. ACTION_SEND );
String [] tos = {"terryyhl@gmail.com "};
String [] ccs = {"kalaicheng@hotmail.com "};
Testintent. putExtra (Intent. EXTRA_EMAIL, tos );
Testintent. putExtra (Intent. EXTRA_CC, ccs );
Testintent. putExtra (Intent. EXTRA_TEXT, "this is content ");
Testintent. putExtra (Intent. EXTRA_SUBJECT, "this is the title ");
Testintent. setType ("message/rfc822 ");
StartActivity (Intent. createChooser (testintent, "send "));
• Method 4: Upload attachments. Here we use the SD card music file as an example.
Intent testN = new Intent (Intent. ACTION_SEND );
TestN. putExtra (Intent. EXTRA_SUBJECT, "title ");
TestN. putExtra (Intent. EXTRA_STREAM, "file: // sdcard/musicloud ");
StartActivity (Intent. createChooser (testN, "send "));
• Use javamail. Here I will not introduce the implementation method of javamail.
• Because the current simulator does not have a built-in Gmail Client program, after the e-mail program sends data, the simulator will send "No Application can perform this action", so it cannot be tested, if you have an Android phone, you can submit the test results to me. Thank you.