Android發(fā)送郵件的方法實(shí)例詳解
本文實(shí)例講述了Android發(fā)送郵件的方法。分享給大家供大家參考,具體如下:
在android手機(jī)中實(shí)現(xiàn)發(fā)送郵件的功能也是不可缺少的。如何實(shí)現(xiàn)它呢?下面以簡單的例子進(jìn)行說明。
程序如下:
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("發(fā)送郵件");
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("請輸入郵箱地址"); //設(shè)置默認(rèn)字段
body.setText("請輸入郵件內(nèi)容");
subject.setText("請輸入主題");
cc.setText("請輸入郵件的字段");
//點(diǎn)擊編輯框,進(jìn)入可編輯狀態(tài)
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中實(shí)現(xiàn)發(fā)送郵件功能的方法之一,還有另外兩種方法如下所示:
方法一:
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,"郵件內(nèi)容");
i.putExtra(Intent.EXTRA_SUBJECT,"郵件主題");
i.setType("message/rfc822");
startActivity(Intent.createChooser(i,"你的郵件"));
如果想在發(fā)送的郵件中添加附件,則可以這樣寫:
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,"你的郵件"));
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》及《Android開發(fā)入門與進(jìn)階教程》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android 后臺發(fā)送郵件到指定郵箱
- Android快速實(shí)現(xiàn)發(fā)送郵件實(shí)例
- Android中使用Service實(shí)現(xiàn)后臺發(fā)送郵件功能實(shí)例
- Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實(shí)現(xiàn))
- Android 后臺發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
- Android開發(fā)中怎樣調(diào)用系統(tǒng)Email發(fā)送郵件(多種調(diào)用方式)
- android實(shí)現(xiàn)自動發(fā)送郵件
相關(guān)文章
Android SharedPreferences數(shù)據(jù)存儲詳解
SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出2022-11-11
Android項(xiàng)目中引入aar包的正確方法介紹
生成aar之后下一步就是如何引用本地的aar文件,下面這篇文章主要給大家介紹了關(guān)于Android項(xiàng)目中引入aar包的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時間選擇控件
這篇文章主要為大家詳細(xì)介紹了可擴(kuò)展自定義的Android滾輪時間選擇控件,結(jié)合WheelView實(shí)現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下2016-07-07
Android中實(shí)現(xiàn)多線程的幾種方式小結(jié)
在 Android 中,實(shí)現(xiàn)多線程編程主要有7種方式,每種方式都有其適用場景和優(yōu)缺點(diǎn),本文將詳細(xì)介紹一下具體實(shí)現(xiàn)方式,大家可以根據(jù)需要自行選擇2025-03-03
Android中Service與Activity之間通信的幾種方式
本篇文章主要介紹了Android中Service與Activity之間通信的幾種方式,Activity主要負(fù)責(zé)前臺頁面的展示,Service主要負(fù)責(zé)需要長期運(yùn)行的任務(wù),具有一定的參考價值,有興趣的可以了解一下。2017-02-02
Android自定義控件(實(shí)現(xiàn)視圖樹繪制指示器)
本文主要介紹了Android視圖樹繪制指示器的實(shí)現(xiàn)原理和具體步驟。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
教你快速實(shí)現(xiàn)Android動態(tài)模糊效果
相信大家都發(fā)現(xiàn)了越來越多的App里面使用了模糊效果,比如雅虎天氣的界面,雖然我并不知道雅虎天氣是怎么做出這種效果的,但是簡單的模仿一下的話,還是能做到的。下面一起來學(xué)習(xí)學(xué)習(xí)。2016-08-08

