本文实例讲述了Android发送邮件的方法。分享给大家供大家参考,具体如下:
在android手机中实现发送邮件的功能也是不可缺少的。如何实现它呢?下面以简单的例子进行说明。
程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
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( "发送邮件" );
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( "请输入邮箱地址" ); //设置默认字段
body.setText( "请输入邮件内容" );
subject.setText( "请输入主题" );
cc.setText( "请输入邮件的字段" );
//点击编辑框,进入可编辑状态
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如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<? 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 >
|
上面是android中实现发送邮件功能的方法之一,还有另外两种方法如下所示:
方法一:
1
2
3
|
Uri uri=Uri.parse( "mailTo:1650***185@qq.com" );
Intent i= new Intent(Intent.ACTION_SENDTO,uri);
startActivity(i);
|
方法二:
1
2
3
4
5
6
7
8
9
|
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, "邮件内容" );
i.putExtra(Intent.EXTRA_SUBJECT, "邮件主题" );
i.setType( "message/rfc822" );
startActivity(Intent.createChooser(i, "你的邮件" ));
|
如果想在发送的邮件中添加附件,则可以这样写:
1
2
3
4
|
Intent i= new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "邮件主题" );
i.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/xyz.mp3" );
startActivity(Intent.createChooser(i, "你的邮件" ));
|
希望本文所述对大家Android程序设计有所帮助。