Android method to send mail details
- 2021-01-25 07:55:03
- OfStack
This article illustrates Android's method of sending mail. To share with you for your reference, as follows:
In the android mobile phone to realize the function of sending mail is also indispensable. How do you do that? Here's 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 when the activity is first created. */
@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 E-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 email address "); // Set Default Fields
body.setText(" Please enter the message ");
subject.setText(" Please enter the subject ");
cc.setText(" Please enter a field for the message ");
// Click the edit box to enter the editable state
reciver.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
reciver.setText("");
}
});
cc.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cc.setText("");
}
});
subject.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method 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 stub
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 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="@string/hello" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/reciver"
android:layout_width="fill_parent"
android: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:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
The above is one of the ways android implements sending mail, and the other two methods are as follows:
Method 1:
Uri uri=Uri.parse("mailTo:1650***185@qq.com");
Intent i=new Intent(Intent.ACTION_SENDTO,uri);
startActivity(i);
Method 2:
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," Email content ");
i.putExtra(Intent.EXTRA_SUBJECT," Email subject ");
i.setType("message/rfc822");
startActivity(Intent.createChooser(i," Your email "));
If you want to attach an attachment to an outgoing message, you can say:
Intent i=new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT," Email subject ");
i.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/xyz.mp3");
startActivity(Intent.createChooser(i," Your email "));
More about Android related content interested readers can see the site features: "Android control usage summary" and "Android development introductory and advanced tutorial"
Hope this article described to everyone Android program design is helpful.