The example in this article describes how Android sends mail. Share to everyone for your reference, specific as follows:
The ability to send mail in Android phones is also essential. How do you implement it? The following is illustrated in a simple example.
The procedure is as follows:
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnKeyListener;
Import Android.widget.Button;
Import Android.widget.EditText;
public class A04activity extends activity {private EditText reciver,cc,subject,body;
Private Button B;
Private string[] Strreciver;
Private string[] STRCC;
Private String strbody;
Private String strsubject; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
b= (Button) Findviewbyid (R.id.button);
B.setenabled (FALSE);
B.settext ("Send Mail");
Reciver= (EditText) Findviewbyid (r.id.reciver);
subject= (EditText) Findviewbyid (r.id.subject);
Cc= (EditText) Findviewbyid (r.id.cc); Body= (EditText) Findviewbyid (r.id.body); Reciver.settext ("Please enter the email address");
Set the default field Body.settext ("Please enter the message content");
Subject.settext ("Please enter subject");
Cc.settext ("Please enter the Mail field"); Click the edit box to enter the editable state reciver.setonclicklistener (new Onclicklistener () {@Override public void OnClick (View v) {//TOD
O auto-generated Method Stub Reciver.settext ("");
}
}); Cc.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method s
Tub Cc.settext ("");
}
}); Subject.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated met
Hod stub Subject.settext ("");
}
}); Body.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated method
Stub Body.settext ("");
}
});
Reciver.setonkeylistener (New Onkeylistener () {@Override public boolean onkey (View v, int keycode, keyevent event) { TODO Auto-generated Method StUB if (Isemail (Reciver.gettext (). toString ()) {b.setenabled (true);
} else{b.setenabled (false);
return false;
}
}); B.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Method St
UB Strreciver=new String[]{reciver.gettext (). toString ()};
Strcc=new String[]{cc.gettext (). toString ()};
Strsubject=subject.gettext (). toString ();
Strbody=body.gettext (). toString ();
Intent i=new Intent (Android.content.Intent.ACTION_SEND);
I.putextra (Android.content.Intent.EXTRA_EMAIL, strreciver);
I.putextra (Android.content.Intent.EXTRA_CC, STRCC);
I.putextra (Android.content.Intent.EXTRA_SUBJECT, strsubject);
I.putextra (Android.content.Intent.EXTRA_TEXT, strbody);
StartActivity (Intent.createchooser (i, Getresources (). getString (R.string.str_message)));
}
}); public static Boolean Isemail (String s) {string expression= "^[a-za-z][\\w\\.-]*[a-za-z0-9]@[a-za-z0-9][\\w\\.-]*[a- Za-z0-9]\\. [A-za-z] [a-za-Z\\.]
*[a-za-z]$ ";
Pattern p=pattern.compile (expression);
Matcher M=p.matcher (s);
return m.matches ();
}
}
Res/layout/main.xml is as follows
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <textview android:layout_width= "fill_parent" android:layout_height= wrap_content "android:text=" @str Ing/hello "/> <button android:id= @+id/button" android:layout_width= "Fill_parent" Android:layout_heig ht= "Wrap_content"/> <edittext android:id= "@+id/reciver" android:layout_width= "Fill_parent" Andro
id:layout_height= "Wrap_content"/> <edittext android:id= "@+id/cc" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/> <edittext android:id= "@+id/subject" android:layout_width= "Fill_parent" android:layout_height= "wrap_content"/> <edittext android:id= "@+id/body" Android:la Yout_width= "Fill_parent" android:layout_height= "Wrap_content"/> </LinearLayout>
This is one of the ways you can implement the Send mail feature in Android, and there are two other ways to do this:
Method One:
Uri uri=uri.parse ("mailto:1650***185@qq.com");
Intent i=new Intent (Intent.action_sendto,uri);
StartActivity (i);
Method Two:
Intent i=new Intent (intent.action_send);
String[] tos={"1650***185@qq.com"};
String[] ccs={"7885***158@qq.com"};
I.putextra (Intent.extra_emall,tos);
I.putextra (INTENT.EXTRA_CC,CCS);
I.putextra (Intent.extra_text, "Mail Content");
I.putextra (Intent.extra_subject, "mail Subject");
I.settype ("message/rfc822");
StartActivity (Intent.createchooser (i, "your Mail"));
If you want to add an attachment to a message that you send, you can write this:
Intent i=new Intent (intent.action_send);
I.putextra (Intent.extra_subject, "mail Subject");
I.putextra (Intent.extra_stream, "File:///sdcard/xyz.mp3");
StartActivity (Intent.createchooser (i, "your Mail"));
More interested readers of Android-related content can view this site: "The summary of Android controls usage" and "Android Development introduction and Advanced Course"
I hope this article will help you with the Android program.