Android 基于Android的手机邮件收发(JavaMail)之三(邮件接收)
初次做这个程序的时候,是仿照着网上别人的程序做的。因为本人比较菜,是一个新手,以前的基础知识没有学好,所以尽管有了别人的代码但是还是不知道怎么在界面上显示出它的效果来,废话不多少,现在就贴出我的参考程序的来源。http://www.cnblogs.com/love2009/archive/2009/02/24/1397201.html 大家可以在看以下内容前,通读一下。想要了解javamail的机制,我们还需要JAVAMAIL的API,这里也贴出帮助文档内容《JavaMail API详解》http://pringles.iteye.com/blog/125196。因为我本人也是在零的基础上做出这个程序的,参考这两篇文章才懂得什么意思。
下面进行后续内容的介绍:
我们需要读取内容,就是需要把手机上的账号和互联网上的账号绑定起来。所以我们需要读取到,welcome界面时候存入的用户名,以及密码。才能执行自己所需要的操作
1.读取数据内容(用户名,以及密码):
sharedPreference
// sharedpreference读取数据,用split()方法,分开字符串。
SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,
MODE_WORLD_READABLE);
String content = pre.getString("save", "");
String[] Information = content.split(";");
username = Information[];
password = Information[];
2.界面准备工作
每次点击收信按钮的时候,大家看到的都是一个列表形式的收信箱,然后点击一条数据,才会显示信件的具体内容。所以我们就需要进行一下操作:
效果布局文件有两个,一个是listMenu.xml和Item.xml
listMenu <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:text="收件箱:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="311dp"
android:layout_weight=""
android:orientation="horizontal" > <View
android:layout_width="12dp"
android:layout_height="309dp" >
</View> <ListView
android:id="@+id/my_list"
android:layout_width="wrap_content"
android:layout_height="313dp" >
</ListView>
</LinearLayout> <ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=""
android:src="@drawable/sinalogo" /> </LinearLayout>
Item <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="22px" >
</TextView> <TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="13px" >
</TextView> </LinearLayout>
界面效果图如下:

3.现在进行代码显示部分(ReceiveList.java)
ReceiveList package mi.email.activity; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties; import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage; import mi.email.core.ResolveMail;
import mi.learn.com.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class ReceiveList extends Activity { private static final String SAVE_INFORMATION = "save_information"; private ListView listview;
private int number; String Title;
String Date;
String From;
String Content;
String username;
String password; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.listmenu);
listview = (ListView) findViewById(R.id.my_list); try {
MenuList();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void MenuList() throws MessagingException, IOException { // sharedpreference读取数据,用split()方法,分开字符串。
SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,
MODE_WORLD_READABLE);
String content = pre.getString("save", "");
String[] Information = content.split(";");
username = Information[];
password = Information[]; Properties props = new Properties();
Session session = Session.getDefaultInstance(props); // 取得pop3协议的邮件服务器
Store store = session.getStore("pop3"); // 连接pop.sina.com邮件服务器 //
store.connect("pop.sina.com", username, password); // 返回文件夹对象 Folder folder = store.getFolder("INBOX"); // 设置仅读
folder.open(Folder.READ_ONLY); // 获取信息
Message message[] = folder.getMessages(); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();//定义一个List并且将其实例化
for (int i = ; i < message.length; i++) {//通过for语句将读取到的邮件内容一个一个的在list中显示出来
ResolveMail receivemail = new ResolveMail((MimeMessage) message[i]); Title = receivemail.getSubject();//得到邮件的标题
Date = receivemail.getSentDate();//得到邮件的发送时间 HashMap<String, String> map = new HashMap<String, String>();//定义一个Map.将获取的内容以键值的方式将内容展现
map.put("title", Title);//显示邮件的标题
map.put("info", Date);//显示邮件的信息
list.add(map); SimpleAdapter listAdapter = new SimpleAdapter(this, list,R.layout.item, new String[] { "title", "info" }, new int[] {
R.id.title, R.id.info });
listview.setAdapter(listAdapter);
} folder.close(true);//用好之后记得将floder和store进行关闭
store.close(); // Item长按事件。得到Item的值,然后传递给MailDetail的值
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("ID", position);
intent.setClass(ReceiveList.this, MailDetails.class);
startActivity(intent);
return true;
} });
}
}
4.我们长按一个Item的时候需要触发点击事件,然后获取到ID得值,将ID的值传送到下一个MailDetail.java的界面
setOnItemLongClickListener // Item长按事件。得到Item的值,然后传递给MailDetail的值
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("ID", position);
intent.setClass(ReceiveList.this, MailDetails.class);
startActivity(intent);
return true;
} });
5.MailDetails.java
MailDetails package mi.email.activity; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties; import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage; import mi.email.core.ResolveMail;
import mi.learn.com.R; import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView; public class MailDetails extends Activity {
private static final String SAVE_INFORMATION = "save_information";
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private ReceiveList ml; public void receive() throws Exception { // sharedpreference读取数据,用split()方法,分开字符串。
SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,MODE_WORLD_READABLE);
String content = pre.getString("save", "");
String[] Information = content.split(";");
String username = Information[];
String password = Information[]; Intent intent = getIntent();//得到上一个文件传入的ID号
Bundle i = intent.getExtras(); int num = i.getInt("ID");//将得到的ID号传递给变量num Properties props = new Properties();
Session session = Session.getDefaultInstance(props);
// 取得pop3协议的邮件服务器
Store store = session.getStore("pop3");
// 连接pop.qq.com邮件服务器
store.connect("pop.sina.com", username, password);
// 返回文件夹对象
Folder folder = store.getFolder("INBOX");
// 设置仅读
folder.open(Folder.READ_ONLY); // 获取信息
Message message[] = folder.getMessages();
ResolveMail receivemail = new ResolveMail((MimeMessage) message[num]);
text1.setText(receivemail.getSubject());//得到邮件解析后的标题内容并且在控件中显示出来
text2.setText(receivemail.getFrom());//得到邮件解析后的发送者
text3.setText(receivemail.getSentDate());//得到邮件解析后的发送时间
text4.setText((CharSequence) message[num].getContent().toString());//得到邮件解析有的内容 folder.close(true);
store.close(); } @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2); text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text3 = (TextView) findViewById(R.id.text3);
text4 = (TextView) findViewById(R.id.text4); try {
receive();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
界面显示图如下:

Android 基于Android的手机邮件收发(JavaMail)之三(邮件接收)的更多相关文章
- Android 基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )
周末休息,这次我们继上次内容继续.上一篇内容我们讲述的是一些准备工作.下载两个javamail.jar和activation.jar文件,然后再BuildPath~ 言归正传,为了展示效果,在这里我申 ...
- Android 基于Android的手机邮件收发(JavaMail)之四(邮件的发送)
上一个邮件的接受,因为不当操作,保存未完成,一切东西都得从头开始那就先从邮件发送来吧. 先下我们做一个较为简单的邮件发送 以下是源代码:相信看过上篇文章所给连接的人,对于javamail应该都有了一定 ...
- Android 基于Android的手机邮件收发(JavaMail)之一(准备工作)
界面一共是五个界面,分别是welcomeActivity,ReceiveAndSendActivity,ReceiveListActivity,SendMailActivity,MailDetails ...
- 【转】 Android 基于google Zxing实现对手机中的二维码进行扫描--不错
原文网址:http://blog.csdn.net/xiaanming/article/details/14450809 转载请注明出处:http://blog.csdn.net/xiaanming/ ...
- 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell [ ...
- Android 基于google Zxing实现对手机中的二维码进行扫描
转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809 有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的 ...
- Android基于XMPP的即时通讯2-文件传输
本文是在上一篇博文Android基于XMPP的即时通讯1-基本对话的基础上,添加新的功能,文件传输 1.初始化文件传输管理类 public static FileTransferManager get ...
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...
- 基于Android 平台简易即时通讯的研究与设计[转]
摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...
随机推荐
- weiphp布署在sina sae图片显示不了问题
sae 上傳因為新浪云是有權限的限制的,所以要有權限才能上傳,以圖片為例首先在入口文件目錄(applicaiton)里的common的conf 里的config配置把上传驱动设为sea 代码 如下:' ...
- intellij idea 插件 ideaVim 用法
intellij idea 插件 ideaVim - Genji_ - 博客园http://www.cnblogs.com/nova-/p/3535636.html IdeaVim插件使用技巧 - - ...
- java打包文件夹为zip文件
//待压缩的文件目录 String sourceFile=sourceFilePath+"\\"+userName; //存放压缩文件的目录 String zipFilePath ...
- MySQL - 常用命令及常用查询SQL
常用查询SQL #查看临时目录 SHOW VARIABLES LIKE '%tmp%'; #查看当前版本 SELECT VERSION(); 常用命令 #查看当前版本,终端下未进入mysql mysq ...
- 基于Proteus仿真的Arduino学习(1)——Arduino Uno最小系统及LED的简单使用
一.前言: A.Arduino简介 Arduino是由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Banzi.David Cuartielles.Tom Igoe.Gianluc ...
- jQuery插件 -- 动态事件绑定插件jquery.livequery.js
http://blog.csdn.net/zzq58157383/article/details/7721974 动态事件绑定插件livequery, 可以利用它给相应的DOM元素注册事件或者触发回调 ...
- Using MySQL Connector .NET 6.6.4 with Entity Framework 5
I had been waiting for the latest MySQL connector for .NET to come out so I can move on to the new a ...
- MySQL字段数据类型表
* MySQL支持所有标准SQL数值数据类型. 数值类型BIT数据类型保存位字段值,并且支持MyISAM.MEMORY.InnoDB和BDB表.作为SQL标准的扩展,MySQL也支持整数类型TINYI ...
- 【Eclipse】 Eclipse 中JPEGEncodeParam 错误波浪线问题
[异常信息] Description Resource Path Location Type Access restriction: The method encode(BufferedImage, ...
- python函数
一.函数: 创建函数:使用def语句 举例:定义一个返回斐波那楔数列列表的函数 def fibs(num): result = [0,1] for i in range(num-2): result. ...