android中發送郵件

來源:互聯網
上載者:User

在移動互連網時代,手機郵件已不是什麼新鮮事了,我們可以使用內建的Gmail引擎來發送郵件,也可以使用SMTP來發送郵件,下面用一個簡單樣本來示範郵件的發送,包括單方發送郵件、多方發送郵件以及抄送郵件,密送郵件,發送附件等。代碼如下:

Activity:

package com.home;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SendEmailActivity extends Activity implements OnClickListener {private Button sendBtn;private Button sendToManyBtn;private Button sendAttachmentBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);sendBtn = (Button) findViewById(R.id.main_btn_send);sendToManyBtn = (Button) findViewById(R.id.main_btn_send_many);sendAttachmentBtn = (Button) findViewById(R.id.main_btn_send_attachment);sendBtn.setOnClickListener(this);sendToManyBtn.setOnClickListener(this);sendAttachmentBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v == sendBtn) {Intent intent = new Intent(Intent.ACTION_SENDTO);intent.setData(Uri.parse("mailto:+297890152@qq.com"));intent.putExtra(Intent.EXTRA_SUBJECT, "這是單方發送的郵件主題");intent.putExtra(Intent.EXTRA_TEXT, "這是單方發送的郵件內容");startActivity(intent);}if (v == sendToManyBtn) {Intent intent = new Intent(Intent.ACTION_SENDTO);intent.setData(Uri.parse("mailto:297890152@qq.com"));intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"313766045@qq.com", "980324510@qq.com" });// 抄送intent.putExtra(Intent.EXTRA_CC,new String[] { "981413230@qq.com" });// 密送intent.putExtra(Intent.EXTRA_BCC,new String[] { "1316106487@qq.com" });intent.putExtra(Intent.EXTRA_SUBJECT, "這是多方發送的郵件主題");intent.putExtra(Intent.EXTRA_TEXT, "這是多方發送的郵件內容");startActivity(intent);}if (v == sendAttachmentBtn) {Intent intent = new Intent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_EMAIL,new String[] { "297890152@qq.com" });intent.putExtra(Intent.EXTRA_SUBJECT, "這是包含附件的郵件主題");intent.putExtra(Intent.EXTRA_TEXT, "這是包含附件的郵件內容");intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(""));intent.setType("text/plain");startActivity(intent);}}}

布局檔案只有三個按鈕,就此省略。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.