Android发送邮件的方法实例详解

所属分类: 软件编程 / Android 阅读数: 1872
收藏 0 赞 0 分享

本文实例讲述了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如下

<?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中实现发送邮件功能的方法之一,还有另外两种方法如下所示:

方法一:

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程序设计有所帮助。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多