无法在Android中使用意图选择器发送邮件

时间:2015-05-25 07:15:37

标签: android sendmail

05-31 12:43:53.615: E/dalvikvm(15521): Could not find class 'com.example.demosendmail.MainActivity$sendMail$1', referenced from method com.example.demosendmail.MainActivity$sendMail.doInBackground
05-31 12:43:53.615: E/dalvikvm(15521): Could not find class 'com.example.demosendmail.MainActivity$sendMail$1', referenced from method com.example.demosendmail.MainActivity$sendMail.doInBackground
05-31 12:43:53.655: E/doInBackground(15521): ccccccccccccccccccccccccccccccccccccccc
05-31 12:43:53.655: E/AndroidRuntime(15521): FATAL EXCEPTION: AsyncTask #1
05-31 12:43:53.655: E/AndroidRuntime(15521): Process: com.example.demosendmail, PID: 15521
05-31 12:43:53.655: E/AndroidRuntime(15521): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.lang.Thread.run(Thread.java:841)
05-31 12:43:53.655: E/AndroidRuntime(15521): Caused by: java.lang.NoClassDefFoundError: com.example.demosendmail.MainActivity$sendMail$1
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at com.example.demosendmail.MainActivity$sendMail.doInBackground(MainActivity.java:121)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at com.example.demosendmail.MainActivity$sendMail.doInBackground(MainActivity.java:1)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-31 12:43:53.655: E/AndroidRuntime(15521): 	... 4 more
05-31 12:43:54.260: E/WindowManager(15521): android.view.WindowLeaked: Activity com.example.demosendmail.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42ae5b08 V.E..... R......D 0,0-1026,288} that was originally added here
05-31 12:43:54.260: E/WindowManager(15521): 	at android.view.ViewRootImpl.<init>(ViewRootImpl.java:460)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.app.Dialog.show(Dialog.java:288)
05-31 12:43:54.260: E/WindowManager(15521): 	at com.example.demosendmail.MainActivity$sendMail.onPreExecute(MainActivity.java:105)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.os.AsyncTask.execute(AsyncTask.java:535)
05-31 12:43:54.260: E/WindowManager(15521): 	at com.example.demosendmail.MainActivity.onClick(MainActivity.java:69)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.view.View.performClick(View.java:4633)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.view.View$PerformClick.run(View.java:19330)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.os.Handler.handleCallback(Handler.java:733)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.os.Handler.dispatchMessage(Handler.java:95)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.os.Looper.loop(Looper.java:157)
05-31 12:43:54.260: E/WindowManager(15521): 	at android.app.ActivityThread.main(ActivityThread.java:5356)
05-31 12:43:54.260: E/WindowManager(15521): 	at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:43:54.260: E/WindowManager(15521): 	at java.lang.reflect.Method.invoke(Method.java:515)
05-31 12:43:54.260: E/WindowManager(15521): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-31 12:43:54.260: E/WindowManager(15521): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-31 12:43:54.260: E/WindowManager(15521): 	at dalvik.system.NativeStart.main(Native Method)

我正在尝试创建一个页面,其中有三个字段和一个按钮,并在按钮上我试图发送邮件但我无法做到这一点我的代码是当我点击按钮它显示错误和我申请停止

package com.example.demosendmail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;





public class MainActivity extends Activity implements OnClickListener{

	private EditText editName;
	private EditText editContact;
	private EditText editIssue;
	private Button btnSend;
    String username="aaa.singh33@gmail.com";
    String password="";// my password
	String reciepint="anurag290488@gmail.com";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		editName = (EditText) findViewById(R.id.edit_name);
		editContact = (EditText) findViewById(R.id.edit_contact);
		editIssue = (EditText) findViewById(R.id.edit_issue);
		btnSend = (Button) findViewById(R.id.btn_send);
		btnSend.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		
		int connection=hasConnection();			
		if(connection==1){
			new sendMail().execute();
		}
		else{
			Toast.makeText(MainActivity.this, "No network available", Toast.LENGTH_LONG).show();
		}	
	}
	
	public  int hasConnection() {
		int flag=0;
		ConnectivityManager cm =(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			flag=1;
		}

		if(flag==0)
		{
			return 0;
		}
		else 
		{
			return 1;
		}
}
	
	private class sendMail extends AsyncTask<Void, Void, Void> {

		private ProgressDialog pDialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setMessage("Publishing recipe.");
			pDialog.setCancelable(false);
			pDialog.show();

		}

		@Override
		protected Void doInBackground(Void... arg0) {
			Log.e("doInBackground","ccccccccccccccccccccccccccccccccccccccc");

			Properties props = new Properties();
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.socketFactory.port", "465");
			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.port", "465");


			Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
				protected PasswordAuthentication getPasswordAuthentication () {
					return new PasswordAuthentication (username,password);
				}
			});
			
		
			try {
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress(username));
				message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(reciepint));
				message.setSubject("Composed Recipe");

				String msg="Name = "+editName.getText().toString()+"				"+
				"Contact No = "+editContact.getText().toString()+"				"+
				"Issue= "+editIssue.getText().toString();
						

//				if(!ConstantsValues.COMPOSE_RECIPE_PICTURE_PATH.equals("empty")){					
//					Log.e("send mail","with attachment");		
//
//					BodyPart messageBodyPart1 = new MimeBodyPart();  
//					messageBodyPart1.setText(msg); 		
//					MimeBodyPart messageBodyPart = new MimeBodyPart();
//					Multipart multipart = new MimeMultipart();
//					messageBodyPart = new MimeBodyPart();
//					String file = ConstantsValues.COMPOSE_RECIPE_PICTURE_PATH;
//					String fileName = "image";
//					DataSource source = new FileDataSource(file);
//					messageBodyPart.setDataHandler(new DataHandler(source));
//					messageBodyPart.setFileName(fileName);
//					multipart.addBodyPart(messageBodyPart1);
//					multipart.addBodyPart(messageBodyPart);
//					message.setContent(multipart);
//				}
//				else{			
//					Log.e("send mail","without attachment");
					message.setContent(msg, "text/html; charset=utf-8");
//				}

				Transport.send(message);

			} catch (MessagingException e) {
				Log.e("catch","error= "+e.getMessage());
				throw new RuntimeException(e);
			}		

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			// Dismiss the progress dialog
			if (pDialog.isShowing())
				pDialog.dismiss();
			Toast.makeText(MainActivity.this, "Mail Sent", Toast.LENGTH_LONG).show();
		}
	}
}

1 个答案:

答案 0 :(得分:0)

您需要添加三个jar文件。 1)additionnal.jar 2)mail.jar 3)activation.jar

您可以从此链接下载。 https://code.google.com/p/javamail-android/downloads/list