android中发送邮件
在移动互联网时代,手机邮件已不是什么新鲜事了,我们可以使用内置的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; @Override protected 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); } @Override public 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); } } }
布局文件只有三个按钮,就此省略。