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 ...
随机推荐
- 修改PHP 加载loaded configuration file 目录
在/usr/local/php/etc/目录下新建php2.ini 复制php.ini 内容到php2.ini 或cp php.ini 也行. 配置php-fpm 参数: Usage: php [-n ...
- 无废话ExtJs 入门教程十九[API的使用]
无废话ExtJs 入门教程十九[API的使用] extjs技术交流,欢迎加群(201926085) 首先解释什么是 API 来自百度百科的官方解释:API(Application Programmin ...
- IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...
- Angular JS 学习之路由
1.AngularJS路由允许我们通过不同的URL访问不同的内容:通过AngularJS可以实现多视图的单页WEB访问(SPA) 2.通常我们的URL形式为http://runoob.com/firs ...
- SQL注入攻防入门详解(2)
SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱 ...
- NIO的一些相关链接
Architecture of a Highly Scalable NIO-Based Server Scalable IO in Java Tricks and Tips with NIO part ...
- 2015 ACM Syrian Collegiate Programming Contest
A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...
- ZeroMQ接口函数之 :zmq_ctx_term - 终结一个ZMQ环境上下文
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_term zmq_ctx_term(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_ ...
- listview改变单个单元格的背景色
ListViewItem lvi = listView1.Items[1]; lvi.UseItemStyleForSubItems = false; System.Windows.Forms.Lis ...
- java任务调度quartz框架的小例子
quartz是一个开源的作业调度框架,当然,java可以使用Timer来实现简单任务调度的功能,但Timer是单线程的设计方案,使得一个任务延迟会影响到其他的任务.java也可以使用Scheduled ...