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 ...
随机推荐
- Power BI for Office 365(一)移动端应用
此篇来自于微软商业智能网站的官方博客团队发布的Power BI在线资料其中的一部分,完整版地址: http://office.microsoft.com/en-us/office365-sharepo ...
- Spring MVC基础入门
Spring MVC简介 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱 ...
- MEF Parts Sample
namespace Microshaoft.MEF.Contracts { using System; public delegate void ExceptionEventHandler<TS ...
- 用Less CSS定义常用的CSS3效果函数
定义圆角及调用 /* 定义圆角 @radius 圆角大小 */ .round(@radius:5px){ border-radius:@radius; -webkit-border-radius: @ ...
- chrome控制台调试学习笔记 暂未整理
15:03 2015/12/7chrome控制台调试学习:推荐博客:http://www.cnblogs.com/Wayou/p/chrome-console-tips-and-tricks.html ...
- Java_动态编译总结
不多说直接上代码: 动态编译的主类: package com.lkb.autoCode.util; import com.lkb.autoCode.constant.AutoCodeConstant; ...
- windows下调试virtualbox的虚拟机串口
1.我不知道其他人是怎么实现的,我是这么实现的. 2.下载一个叫做VSPD的软件,其作用是在windosw上虚拟几个串口出来. 下载完了安装,安装完了注册,如果不是花钱买来的,那就自己想办法注册吧.我 ...
- 01@MySQL_Course_LabVIEW+MySQL程序开发
LabVIEW+MySQL程序开发 大数据时代,LabVIEW程序开发怎么能少了数据库这一强大的工具,然而大多数的LabVIEW工程师对于数据库的概念了解比较少,所以本课程重点介绍如何利用MySQL, ...
- Java 对象拷贝方式
(1)BeanUtils.cloneBean()使用: http://www.cnblogs.com/fervour/archive/2009/12/18/1627868.html package c ...
- java并发编程(九)死锁
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17200937 大部分代码并不容易产生死锁,死锁可能在代码中隐藏相当长的时间,等待不常见的条 ...