Android中使用JavaMail发送Email带群发加附件(2)

package com.example.mailtest;


import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import Android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MailActivity extends Activity {

private Button sendBtn;
 private String username;
 private String password;
 private Handler sendHandler;
 private ProgressDialog progressDialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_mail);
  sendBtn=(Button)findViewById(R.id.btnSend);
  sendHandler =  new Handler();
  sendBtn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    SendTask sTask = new SendTask();   
    sTask.execute();
   }
  });

}

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

class SendTask extends AsyncTask<Integer, Integer, String>{   
  //后面尖括号内分别是参数(例子里是线程休息时间),进度(publishProgress用到),返回值 类型   

@Override   
  protected void onPreExecute() {   
   //第一个执行方法   
   Toast.makeText(getApplicationContext(), "Begin Send!", Toast.LENGTH_SHORT).show(); 
   super.onPreExecute();   
  }   

@Override   
  protected String doInBackground(Integer... params) {   
   //第二个执行方法,onPreExecute()执行完后执行   
   // TODO Auto-generated method stub 
   Mail m = new Mail("empty.shen@gmail.com", "*****");

String[] toArr = {"silangquan@gmail.com","k283228391@126.com"};
   m.setTo(toArr);
   m.setFrom("wooo@wooo.com");
   m.setSubject("JavamailTest");
   m.setBody("Email body.");

try {
    //If you want add attachment use function addAttachment.
    //m.addAttachment("/sdcard/filelocation");

if(m.send()) {
     System.out.println("Email was sent successfully.");
    } else {
     System.out.println("Email was not sent.");
    }
   } catch(Exception e) {
    //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
    Log.e("MailApp", "Could not send email", e);
   }

return "";   
  }   

@Override   
  protected void onProgressUpdate(Integer... progress) {   
   //这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数   
   //但是这里取到的是一个数组,所以要用progesss[0]来取值   
   //第n个参数就用progress[n]来取值       
   super.onProgressUpdate(progress);   
  }   

@Override   
  protected void onPostExecute(String r) {   
   //doInBackground返回时触发,换句话说,就是doInBackground执行完后触发   
   //这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"   
   //setTitle(result); 
   super.onPostExecute(r);   
  }   

}   

}

这里用到了AsyncTask,应为直接在UI线程中连接互联网的话,或爆 android.os.NetworkOnMainThreadException异常。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8c1aec5a7aaffcbd70c8f9fb9808597c.html