Android接受邮件


package com.example.atest21;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.search.AndTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;

import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Button bn1 =(Button)findViewById(R.id.button1);
		final Button bn2 =(Button)findViewById(R.id.button2);
		
		bn1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RecevieMail rm = new RecevieMail("username", "password");
			}
		});
		
		bn2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.exit(0);
				
			}
		});
	}

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

class RecevieMail{

	String strMsg,UserName,passwd;
	String imapServer = "imap.163.com";
	
	private RecevieMail() {
		super();
		// TODO Auto-generated constructor stub	
	}

	public RecevieMail(String userName, String passwd) {
		super();
//		this.strMsg = strMsg;
		UserName = userName;
		this.passwd = passwd;
		
		Properties prop = System.getProperties();
		prop.put("mail.imap.host", imapServer);
		prop.put("mail.imap.auth.plain.disable", "true");
		
		Session mailsession = Session.getInstance(prop, null);
		mailsession.setDebug(false);
		IMAPFolder folder = null;
		IMAPStore store = null;
		int total = 0;
		try{
			store = (IMAPStore)mailsession.getStore("imap");
			store.connect(imapServer, userName, passwd);
			folder = (IMAPFolder)store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			total = folder.getMessageCount();
			System.out.println(total);
			SearchTerm st = new AndTerm(new FromStringTerm("abc@abc.com"),
														  new SubjectTerm("aaaaa"));
			
			Message[] msgs  = folder.search(st);
			int mailcount = msgs.length;
			System.out.println(mailcount);
			for(int i1 = 0;i1 <msgs.length;i1++){
//				try {
//					msgs[i1].writeTo(System.out);
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				String strTmp1 = msgs[i1].getContentType();
			
				try {
					String strTmp1 = (String)msgs[i1].getContent();
					System.out.println(strTmp1);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
//			Message[] msgs = folder.getMessages();
//			System.out.println("\tZongYouJianShu: "+msgs.length);
//			System.out.println("\tWeiDuYouJianShu: "+folder.getUnreadMessageCount());
//			System.out.println("\tXinYouJianShu: "+folder.getNewMessageCount());
			
			
			
//			for(int i1 = 0;i1<msgs.length;i1++){
//				String subject = msgs[i1].getSubject();
//				String from = (msgs[i1].getFrom()[0]).toString();
//				if(subject != null)
//				{
//					
//					System.out.println(subject);
//					if(subject .equals("aaaaa")){
//						System.out.println("OK");
//						try {
//							msgs[i1].writeTo(System.out);
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
						try {
							String strTmp1 =msgs[i1].getContent().toString();
							System.out.println(strTmp1);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
//						}
					
//				}
//				if(from!=null)
//					System.out.println(from);
//				
//				
//			}
//			
//			
		}catch(MessagingException ex){
			System.out.println(ex.getMessage());
		}
		
		
	}

	public String getStrMsg() {
		return strMsg;
	}

	public void setStrMsg(String strMsg) {
		this.strMsg = strMsg;
	}
	
	
}



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="28dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="26dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="22dp"
        android:text="Hello 2" />

</RelativeLayout>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值