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 ...
随机推荐
- iOS,监听tableVIew的偏移量
1. 添加监听 [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObs ...
- ldd 命令用于判断某个可执行的binary档案含有什么动态链接库(so)
[root@NB ok]# ldd /bin/ls linux-vdso.so. => (0x00007ffd7dbf6000) libselinux.so. => /lib64/libs ...
- Git版本控制管理学习笔记2--起步
首先确保系统中已经安装了git,这里使用的linux系统. 一.命令行初步使用: 1.git命令: 列出它的选项和最常用的子命令.标准命令格式中,COMMAND代表的就是下面列出的子命令. [root ...
- 为什么在soui中加载JPG文件失败
在SOUI中解决解码器是一个独立的模块.不同的解码器决定了程序中能够加载什么样的图片类型.使用SComMgr来加载SOUI的模块时,debug模式下默认的图片解码器是imgdecoder-png.这个 ...
- SourceTree安装教程和GitLab配置详解
一.安装Git 链接: http://pan.baidu.com/s/1mh7rICK 密码: 48dj 二.安装SourceTree 链接: http://pan.baidu.com/s/1skWk ...
- [spring源码学习]一、IOC简介
一.程序实例 假设一个简单地实例,我们有一个人,人可能有姓名,年龄等属性,每天上下班的时候需要坐车,他可能做小轿车,suv等,这样一个场景.我们很容易想到如下代码: 1.人的对象类,包括两个属性,姓名 ...
- enumerate()
今天我们学一个单词 enumerate 后面加个括号 他就不是单词了,那是什么呢 来看一下 enumerate() a = ('htc', 'oppo', 'vivo', 'huawei', 'mi' ...
- 踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件
这个大坑... .... 如题,在Windows的eclipse中编写SparkSQL代码时,编写如下代码时,一运行就抛出一堆空指针异常: // 首先还是创建SparkConf SparkConf c ...
- SRM 618 DIV1 500
非常棒的组合问题,看了好一会,无想法.... 有很多做法,我发现不考虑顺序的最好理解,也最好写. 结果一定是两种形式 A....A dp[n-1] A...A...A sgma(dp[j]*dp[ ...
- style
设计的默认单位为px; 为了简化设计,都是以750px为基本单位设计的,如果屏幕大小不同,它会根据屏幕大小自动缩放. 它的样式选择和CSS非常类似,但是和CSS选择器不同的是,它只支持单级选择,不支持 ...