本文实例讲述了Android发送邮件的方法。分享给大家供大家参考,具体如下:
在android手机中实现发送邮件的功能也是不可缺少的。如何实现它呢?下面以简单的例子进行说明。
程序如下:
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如下
<&#63;xml version="1.0" encoding="utf-8"&#63;>
上面是android中实现发送邮件功能的方法之一,还有另外两种方法如下所示:
方法一:
Uri uri=Uri.parse("mailTo:1650***185@qq.com"); Intent i=new Intent(Intent.ACTION_SENDTO,uri); startActivity(i);
方法二:
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,"你的邮件"));
如果想在发送的邮件中添加附件,则可以这样写:
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相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》及《Android开发入门与进阶教程》
希望本文所述对大家Android程序设计有所帮助。