1. 程式人生 > >利用Android實現電子郵箱客戶端

利用Android實現電子郵箱客戶端

本文主要講述了安卓平臺上利用QQ郵箱SMTP協議,POP3協議傳送與接收訊息的實現

傳送郵件核心程式碼

 import java.security.Security;    
  import java.util.Date;    
  import java.util.Properties;    

   import javax.mail.Authenticator;    
    import javax.mail.Message;    
    import javax.mail.MessagingException;    
   import javax.mail.PasswordAuthentication
;
import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * 使用Gmail傳送郵件 * @author Winter Lau */ public class GmailSender { public static void main(String[] args) throws AddressException, MessagingException { Security.addProvider
(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = System.getProperties(); props.setProperty("mail.smtp.host", "smtp.qq.com"); props.setProperty("mail.smtp.socketFactory.class"
, SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); final String username = "yourusername"; final String password = "yourpassword"; Session session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress(username + "@qq.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]",false)); msg.setSubject("Hello"); msg.setText("How are you"); msg.setSentDate(new Date()); Transport.send(msg); System.out.println("Message sent."); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

接收郵件核心程式碼

 import java.io.UnsupportedEncodingException;    
    import java.security.*;    
    import java.util.Properties;    
    import javax.mail.*;    
    import javax.mail.internet.InternetAddress;    
    import javax.mail.internet.MimeUtility;    

   /**    
    * 用於收取Gmail郵件    
     * @author Winter Lau    
    */    
   public class GmailFetch {    

    public static void main(String argv[]) throws Exception {    

      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());    
     final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";    

      // Get a Properties object    
      Properties props = System.getProperties();    
      props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);    
     props.setProperty("mail.pop3.socketFactory.fallback", "false");    
      props.setProperty("mail.pop3.port", "995");    
      props.setProperty("mail.pop3.socketFactory.port", "995");    

      //以下步驟跟一般的JavaMail操作相同    
      Session session = Session.getDefaultInstance(props,null);    

     //請將紅色部分對應替換成你的郵箱帳號和密碼    
     URLName urln = new URLName("pop3","pop.qq.com",995,null,    
       "[email protected]", "yourpassword");    
    Store store = session.getStore(urln);    
      Folder inbox = null;    
      try {    
       store.connect();    
       inbox = store.getFolder("INBOX");    
       inbox.open(Folder.READ_ONLY);    
       FetchProfile profile = new FetchProfile();    
       profile.add(FetchProfile.Item.ENVELOPE);    
       Message[] messages = inbox.getMessages();    
      inbox.fetch(messages, profile);    
       System.out.println("收件箱的郵件數:" + messages.length);    
       for (int i = 0; i < messages.length; i++) {    
       //郵件傳送者    
       String from = decodeText(messages[i].getFrom()[0].toString());    
        InternetAddress ia = new InternetAddress(from);    
       System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');    
        //郵件標題    
        System.out.println("TITLE:" + messages[i].getSubject());    
        //郵件大小    
        System.out.println("SIZE:" + messages[i].getSize());    
        //郵件傳送時間    
        System.out.println("DATE:" + messages[i].getSentDate());    
       }    
      } finally {    
       try {    
        inbox.close(false);    
      } catch (Exception e) {}    
      try {    
        store.close();    
       } catch (Exception e) {}    
      }    
     }    

    protected static String decodeText(String text)    
       throws UnsupportedEncodingException {    
      if (text == null)    
       return null;    
     if (text.startsWith("=?GB") || text.startsWith("=?gb"))    
      text = MimeUtility.decodeText(text);    
     else    
      text = new String(text.getBytes("ISO8859_1"));    
     return text;    
    }    

   }    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

注意,接收郵件使用java客戶端時可以正常得到資料,在使用安卓的時候,就會卡在store.connect(),原因不詳,無法解決

問題已解決,安卓似乎強行並不允許連線網路的操作在主執行緒中,將store.connnect()放到子執行緒中即可,不然會卡住

注意,發郵件時,要匯入activation.jar,不然的話會報java.lang.noClassDefFoundError:javax.activation.DataHandler

最後完成版本

主介面

package com.zj.myemail;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

public class MainActivity extends Activity {

    private ExpandableListView expendView;
    private int []group_click=new int[5];
    private long mExitTime=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
         final MyExpendAdapter adapter=new MyExpendAdapter();

        expendView=(ExpandableListView) findViewById(R.id.list);
        expendView.setGroupIndicator(null);  //設定預設圖示不顯示
        expendView.setAdapter(adapter);

        //一級點選事件
        expendView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {

                group_click[groupPosition]+=1;
                adapter.notifyDataSetChanged();
                return false;
            }
        });

        //二級點選事件
        expendView.setOnChildClickListener(new OnChildClickListener() { 
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                //可在這裡做點選事件
                if(groupPosition==0&&childPosition==1){
                    /*AlertDialog.Builder builder=new Builder(MainActivity.this);
                    builder.setTitle("新增聯絡人");

                    View view=getLayoutInflater().inflate(R.layout.email_add_address, null);
                    final EditText name=(EditText) view.findViewById(R.id.name);
                    final EditText addr=(EditText) view.findViewById(R.id.address);

                    builder.setView(view);
                    builder.setPositiveButton("確定", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            insertAddress(name.getText().toString().trim(),addr.getText().toString().trim());
                        }
                    });
                    builder.setNegativeButton("取消", null);
                    builder.show();*/
                }else if(groupPosition==0&&childPosition==0){
                    //Intent intent=new Intent(MainActivity.this, MailConstactsActivity.class);
                    //startActivity(intent);
                }else if(groupPosition==1&&childPosition==0){
                    Intent intent=new Intent(MainActivity.this, MailEditActivity.class);
                    startActivity(intent);
                }else if(groupPosition==1&&childPosition==1){
                    //Intent intent=new Intent(MainActivity.this, MailCaogaoxiangActivity.class);
                    //startActivity(intent);
                }else if(groupPosition==2&&childPosition==0){
                    Intent intent=new Intent(MainActivity.this, MailBoxActivity.class);
                    intent.putExtra("TYPE", "INBOX");
                    intent.putExtra("status", 0);//全部
                    startActivity(intent);
                }else if(groupPosition==2&&childPosition==1){
                    //Intent intent=new Intent(MainActivity.this, MailBoxActivity.class);
                    //intent.putExtra("TYPE", "INBOX");
                    //intent.putExtra("status", 1);//未讀
                    //startActivity(intent);
                }else if(groupPosition==2&&childPosition==2){
                    //Intent intent=new Intent(MainActivity.this, MailBoxActivity.class);
                    //intent.putExtra("TYPE", "INBOX");
                    //intent.putExtra("status", 2);//已讀
                    //startActivity(intent);
                }
                adapter.notifyDataSetChanged();
                return false;
            }
        });

    }

    /**
     * 新增聯絡人
     */
    /*private void insertAddress(String user,String address){
        if(user==null){
            Toast.makeText(HomeActivity.this, "使用者名稱不能為空", Toast.LENGTH_SHORT).show();
        }else{
            if(!EmailFormatUtil.emailFormat(address)){
                Toast.makeText(HomeActivity.this, "郵箱格式不正確", Toast.LENGTH_SHORT).show();
            }else{
                Uri uri=Uri.parse("content://com.emailconstantprovider");
                ContentValues values=new ContentValues();
                values.put("mailfrom", MyApplication.info.getUserName());
                values.put("name", user);
                values.put("address", address);
                getContentResolver().insert(uri, values);

                Toast.makeText(HomeActivity.this, "新增資料成功", Toast.LENGTH_SHORT).show();
            }
        }


    }*/

    /**
     * 介面卡
     * @author Administrator
     *
     */
    private class MyExpendAdapter extends BaseExpandableListAdapter{

        /**
         * pic state
         */
        //int []group_state=new int[]{R.drawable.group_right,R.drawable.group_down};

        /**
         * group title
         */
        String []group_title=new String[]{"聯絡人","寫郵件","收件箱"};

        /**
         * child text
         */
        String [][] child_text=new String [][]{
                {"聯絡人列表","新增聯絡人"},
                {"新郵件","草稿箱"},
                {"全部郵件","未讀郵件","已讀郵件"},};
        int [][] child_icons=new int[][]{
                {R.drawable.listlianxiren,R.drawable.tianjia},
                {R.drawable.xieyoujian,R.drawable.caogaoxiang},
                {R.drawable.all,R.drawable.notread,R.drawable.hasread},
        };

        /**
         * 獲取一級標籤中二級標籤的內容
         */
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return child_text[groupPosition][childPosition];
        }

        /**
         * 獲取二級標籤ID
         */
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        /**
         * 對一級標籤下的二級標籤進行設定
         */
        @SuppressLint("SimpleDateFormat")
        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(R.layout.email_child, null);
            TextView tv=(TextView) convertView.findViewById(R.id.tv);
            tv.setText(child_text[groupPosition][childPosition]);

            ImageView iv=(ImageView) convertView.findViewById(R.id.child_icon);
            iv.setImageResource(child_icons[groupPosition][childPosition]);
            return convertView;
        }

        /**
         * 一級標籤下二級標籤的數量
         */
        @Override
        public int getChildrenCount(int groupPosition) {
            return child_text[groupPosition].length;
        }

        /**
         * 獲取一級標籤內容
         */
        @Override
        public Object getGroup(int groupPosition) {
            return group_title[groupPosition];
        }

        /**
         * 一級標籤總數
         */
        @Override
        public int getGroupCount() {
            return group_title.length;
        }

        /**
         * 一級標籤ID
         */
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        /**
         * 對一級標籤進行設定
         */
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(R.layout.email_group, null);

            ImageView icon=(ImageView) convertView.findViewById(R.id.icon);
            ImageView iv=(ImageView) convertView.findViewById(R.id.iv);
            TextView tv=(TextView) convertView.findViewById(R.id.iv_title);

            iv.setImageResource(R.drawable.group_right);
            tv.setText(group_title[groupPosition]);

            if(groupPosition==0){
                icon.setImageResource(R.drawable.constants);
            }else if(groupPosition==1){
                icon.setImageResource(R.drawable.mailto);
            }else if(groupPosition==2){
                icon.setImageResource(R.drawable.mailbox);
            }

            if(group_click[groupPosition]%2==0){
                iv.setImageResource(R.drawable.group_right);
            }else{
                iv.setImageResource(R.drawable.group_down);
            }


            return convertView;
        }
        /**
         * 指定位置相應的組檢視
         */
        @Override
        public boolean hasStableIds() {
            return true;
        }

        /**
         *  當選擇子節點的時候,呼叫該方法
         */
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    /**
     * 返回退出系統
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if((System.currentTimeMillis()-mExitTime)<2000){
                android.os.Process.killProcess(android.os.Process.myPid());
            }else{
                Toast.makeText(MainActivity.this, "再按一次退出程式", Toast.LENGTH_SHORT).show();
                mExitTime=System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295

收件箱


package com.zj.myemail;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.DialerFilter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;



public class MailBoxActivity extends Activity {

    private ArrayList<Email> mailslist = new ArrayList<Email>();
    private ArrayList<ArrayList<InputStream>> attachmentsInputStreamsList = new ArrayList<ArrayList<InputStream>>();
    private String type;
    private int status;
    private MyAdapter myAdapter;
    private ListView lv_box;
    //private List<MailReceiver> mailReceivers;
    private ProgressDialog dialog;

    private List<String> messageids;
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            dialog.dismiss();

             myAdapter = new MyAdapter();
             lv_box.setAdapter(myAdapter);
        };

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);


        setContentView(R.layout.email_mailbox);
        initView();

    }

    private void initView() {
        lv_box = (ListView) findViewById(R.id.lv_box);


        dialog=new ProgressDialog(this);
        dialog.setMessage("正載入");
        dialog.show();


        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());    
         final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";    

          // Get a Properties object    
          Properties props = System.getProperties();    
          props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);    
         props.setProperty("mail.pop3.socketFactory.fallback", "false");    
          props.setProperty("mail.pop3.port", "995");    
          props.setProperty("mail.pop3.socketFactory.port", "995");    

          //以下步驟跟一般的JavaMail操作相同    
          final Session session = Session.getDefaultInstance(props,null);    

         //請將紅色部分對應替換成你的郵箱帳號和密碼    
         final URLName urln = new URLName("pop3","pop.qq.com",995,null,    
           "[email protected]", "yourpassword");   



         new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                 Store store=null;
                  Folder inbox = null; 
                try { 

                     store = session.getStore(urln);    

                     Log.i("test","here1");
                       store.connect();    
                       Log.i("test","here2");
                       inbox = store.getFolder("INBOX");    
                       inbox.open(Folder.READ_ONLY);    
                       FetchProfile profile = new FetchProfile();    
                       profile.add(FetchProfile.Item.ENVELOPE);    
                       Message[] messages = inbox.getMessages();    
                      inbox.fetch(messages, profile);  
                      Log.i("test",messages.length+"");
                       System.out.println("收件箱的郵件數:" + messages.length);    
                       for (int i = 0; i < messages.length; i++) {    
                       //郵件傳送者    
                       String from = decodeText(messages[i].getFrom()[0].toString());    
                        InternetAddress ia = new InternetAddress(from);    
                       System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');    
                        //郵件標題    
                        System.out.println("TITLE:" + messages[i].getSubject());    
                        //郵件大小    
                        System.out.println("SIZE:" + messages[i].getSize());    
                        //郵件傳送時間    
                        System.out.println("DATE:" + messages[i].getSentDate());   
                        Email email=new Email();
                        email.setFrom(ia.getPersonal()+'('+ia.getAddress()+')');
                        email.setSubject( messages[i].getSubject());
                        email.setSentdata(messages[i].getSentDate()+"");

                       mailslist.add(email);

                       }    

                       handler.sendEmptyMessage(0);
                      } catch(Exception e)
                      {

                      }
                      finally {    
                       try {    
                        inbox.close(false);    
                      } catch (Exception e) {}    
                      try {    
                        store.close();    
                       } catch (Exception e) {}    
                      }  
            }
        }).start();

    }

    protected static String decodeText(String text)    
               throws UnsupportedEncodingException {    
              if (text == null)    
               return null;    
             if (text.startsWith("=?GB") || text.startsWith("=?gb"))    
              text = MimeUtility.decodeText(text);    
             else    
              text = new String(text.getBytes("ISO8859_1"));    
             return text;    
            }    



    /**
     * 介面卡
     * @author Administrator
     *
     */
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mailslist.size();
        }

        @Override
        public Object getItem(int position) {
            return mailslist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(MailBoxActivity.this).inflate(R.layout.email_mailbox_item, null);

            TextView tv_from = (TextView) convertView.findViewById(R.id.tv_from);
            tv_from.setText(mailslist.get(position).getFrom());

            TextView tv_sentdate = (TextView) convertView.findViewById(R.id.tv_sentdate);
            tv_sentdate.setText(mailslist.get(position).getSentdata());

            TextView tv_subject = (TextView) convertView.findViewById(R.id.tv_subject);
            tv_subject.setText(mailslist.get(position).getSubject());

            return convertView;
        }

    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220

傳送郵件

package com.zj.myemail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;





public class MailEditActivi