Android發(fā)送郵件的方法實(shí)例詳解
本文實(shí)例講述了Android發(fā)送郵件的方法。分享給大家供大家參考,具體如下:
在android手機(jī)中實(shí)現(xiàn)發(fā)送郵件的功能也是不可缺少的。如何實(shí)現(xiàn)它呢?下面以簡(jiǎ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("請(qǐng)輸入郵箱地址"); //設(shè)置默認(rèn)字段 body.setText("請(qǐng)輸入郵件內(nèi)容"); subject.setText("請(qǐng)輸入主題"); cc.setText("請(qǐng)輸入郵件的字段"); //點(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)階教程》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 后臺(tái)發(fā)送郵件到指定郵箱
- Android快速實(shí)現(xiàn)發(fā)送郵件實(shí)例
- Android中使用Service實(shí)現(xiàn)后臺(tái)發(fā)送郵件功能實(shí)例
- Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號(hào)碼的方法(基于PhoneStateListene實(shí)現(xiàn))
- Android 后臺(tái)發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
- Android開發(fā)中怎樣調(diào)用系統(tǒng)Email發(fā)送郵件(多種調(diào)用方式)
- android實(shí)現(xiàn)自動(dòng)發(fā)送郵件
相關(guān)文章
Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
本文介紹了Android數(shù)據(jù)雙向綁定的原理和實(shí)現(xiàn)方式,包括基于觀察者模式和數(shù)據(jù)綁定框架的實(shí)現(xiàn)方法,以及應(yīng)用場(chǎng)景和優(yōu)缺點(diǎn)的分析,幫助開發(fā)者了解和應(yīng)用數(shù)據(jù)雙向綁定技術(shù),提升應(yīng)用的交互性和響應(yīng)速度2023-04-04Android中Activity的四種啟動(dòng)模式和onNewIntent()
android 中activity的啟動(dòng)模式分為四種,(standard、singleTop、singTask、singleInstance),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08Android調(diào)用相機(jī)并將照片存儲(chǔ)到sd卡上實(shí)現(xiàn)方法
Android中實(shí)現(xiàn)拍照有兩種方法,一種是調(diào)用系統(tǒng)自帶的相機(jī),還有一種是自己用Camera類和其他相關(guān)類實(shí)現(xiàn)相機(jī)功能,這種方法定制度比較高,需要的朋友可以了解下2012-12-12android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例
本篇文章主要介紹了android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android 按后退鍵退出Android程序的實(shí)現(xiàn)方法
本篇文章介紹了,在Android中按后退鍵退出Android程序的實(shí)現(xiàn)方法。需要的朋友參考下2013-04-04Android 實(shí)現(xiàn)切圓圖作為頭像使用實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)切圓圖作為頭像使用實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12Dagger2 Android依賴注入學(xué)習(xí)筆記
這篇文章主要介紹了Dagger2 Android依賴注入學(xué)習(xí)筆記,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06android加密參數(shù)定位實(shí)現(xiàn)方法
這篇文章主要介紹了android加密參數(shù)定位方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法,涉及Android日期時(shí)間相關(guān)轉(zhuǎn)換與運(yùn)算操作技巧,需要的朋友可以參考下2018-01-01剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理
在很多的App中,都會(huì)發(fā)現(xiàn)利用手指滑動(dòng)事件,進(jìn)行高效且人性化的交互非常有必要,那么它是怎么實(shí)現(xiàn)的呢,本文給大家解析實(shí)現(xiàn)原理,對(duì)Activity側(cè)滑返回實(shí)現(xiàn)代碼感興趣的朋友一起看看吧2021-06-06