Android accepts emails

Source: Internet
Author: User

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 {@Overrideprotected 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubRecevieMail rm = new RecevieMail("username", "password");}});bn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSystem.exit(0);}});}@Overridepublic 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 blocke.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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.