基于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[0];
password = Information[1];

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="1"
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="2"
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[0];
password = Information[1];

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 = 0; 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[0];
String password = Information[1];

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();
}

}

}

界面显示图如下:





 

转载于:https://www.cnblogs.com/sarah-susan/archive/2011/12/12/2284685.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值