Android应用程序在使用javamail Android发送邮件时崩溃

Android应用程序在使用javamail Android发送邮件时崩溃,java,android,email,Java,Android,Email,我正在制作一个android应用程序,它使用一个简单的表单从用户那里获取一些信息,并向包含这些信息的特定地址发送电子邮件。 我正在使用javamail android库() 我的应用程序在模拟器中运行良好,但当我单击“提交”按钮时,应用程序会崩溃。这是日志- 11-12 11:57:50.730 1232-1248/com.android.imran.quickcomplain E/SendMailTask﹕ null javax.mail.AuthenticationFailed

我正在制作一个android应用程序,它使用一个简单的表单从用户那里获取一些信息,并向包含这些信息的特定地址发送电子邮件。 我正在使用javamail android库()

我的应用程序在模拟器中运行良好,但当我单击“提交”按钮时,应用程序会崩溃。这是日志-

11-12 11:57:50.730    1232-1248/com.android.imran.quickcomplain E/SendMailTask﹕ null
    javax.mail.AuthenticationFailedException
            at javax.mail.Service.connect(Service.java:319)
            at javax.mail.Service.connect(Service.java:169)
            at com.android.imran.quickcomplain.GMail.sendEmail(GMail.java:74)
            at com.android.imran.quickcomplain.SendMailTask.doInBackground(SendMailTask.java:38)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
11-12 11:57:50.730    1232-1232/com.android.imran.quickcomplain E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.android.imran.quickcomplain.SendMailTask.onProgressUpdate(SendMailTask.java:50)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
11-12 11:57:55.306    1232-1232/? I/Process﹕ Sending signal. PID: 1232 SIG: 9
下面是java文件-

MainActivity.java

package com.android.imran.quickcomplain;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    EditText appellor_name;
    EditText appellor_mobileno;
    CheckBox make_appellor_anonymous;

    EditText victim_name;
    EditText victim_mobileno;
    CheckBox same_as_appellorinfo;

    EditText suspect_name;
    EditText suspect_details;
    CheckBox i_couldnt_recognize;

    Spinner location;
    EditText specify_road;

    Spinner category;
    EditText other_category;

    EditText crime_details;

    ImageButton addFile;
    ImageButton takePhoto;

    CheckBox terms_conditions;

    Button submit;

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

        //Assigning findViewById
        appellor_name = (EditText) findViewById(R.id.appellor_name);
        appellor_mobileno = (EditText) findViewById(R.id.appellor_mobileno);
        make_appellor_anonymous = (CheckBox) findViewById(R.id.make_appellor_anonymous);

        victim_name = (EditText) findViewById(R.id.victim_name);
        victim_mobileno = (EditText) findViewById(R.id.victim_mobileno);
        same_as_appellorinfo = (CheckBox) findViewById(R.id.same_as_appellorinfo);

        suspect_name  = (EditText) findViewById(R.id.suspect_name);
        suspect_details  = (EditText) findViewById(R.id.suspect_details);
        i_couldnt_recognize = (CheckBox) findViewById(R.id.i_couldnt_recognize);

        location=(Spinner)findViewById(R.id.location);
        specify_road = (EditText) findViewById(R.id.specify_road);

        category=(Spinner)findViewById(R.id.category);
        other_category = (EditText) findViewById(R.id.other_category);

        crime_details = (EditText) findViewById(R.id.crime_details);

        addFile = (ImageButton) findViewById(R.id.addFile);
        takePhoto = (ImageButton) findViewById(R.id.takePhoto);

        terms_conditions = (CheckBox) findViewById(R.id.terms_conditions);
        submit = (Button) findViewById(R.id.submit);
        //close


        //Setting the spinner data
        SpinnerItemData idata=new SpinnerItemData();

        ArrayList<SetItemData> splocation=new ArrayList<>();
        for (int i=0; i<idata.locationdata.length; i++)
            splocation.add(new SetItemData(idata.locationdata[i], R.drawable.location));

        ArrayList<SetItemData> spcategory=new ArrayList<>();
        for (int j=0; j<idata.categorydata.length; j++)
            spcategory.add(new SetItemData(idata.categorydata[j], R.drawable.category));

        SpinnerAdapter locadapter=new SpinnerAdapter(this,
                R.layout.spinner_layout,R.id.txt,splocation);
        location.setAdapter(locadapter);

        SpinnerAdapter catadapter=new SpinnerAdapter(this,
                R.layout.spinner_layout,R.id.txt,spcategory);
        category.setAdapter(catadapter);
        //close



        //Submit email
        submit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Log.i("SendMailActivity", "Send Button Clicked.");
                String fromEmail = "******@gmail.com";
                String fromPassword ="********";
                String toEmails = "someone@gmail.com";
                List<String> toEmailList = Arrays.asList(toEmails.split("\\s*,\\s*"));
                //Log.i("MainActivity", "To List: " + toEmailList);

                String emailSubject = category.getSelectedItem().toString() + " in " + location.getSelectedItem().toString();
                String emailBody = crime_details.getText().toString()+"\nVictim's Name: "+victim_name.getText().toString()+
                        "\nVictim's Contact: "+victim_mobileno.getText().toString()+
                        "\nSuspect Details: "+suspect_details.getText().toString()+"\n";

                new SendMailTask(MainActivity.this).execute(fromEmail,
                        fromPassword, toEmailList, emailSubject, emailBody);
            }
        });
    }
}
package com.android.imran.quickcomplain;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

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

import android.util.Log;

public class GMail {

    final String emailPort = "587";// gmail's smtp port
    final String smtpAuth = "true";
    final String starttls = "true";
    final String emailHost = "smtp.gmail.com";

    String fromEmail;
    String fromPassword;
    List<String> toEmailList;
    String emailSubject;
    String emailBody;

    Properties emailProperties;
    Session mailSession;
    MimeMessage emailMessage;

    public GMail() {}

    public GMail(String fromEmail, String fromPassword,
                 List<String> toEmailList, String emailSubject, String emailBody) {
        this.fromEmail = fromEmail;
        this.fromPassword = fromPassword;
        this.toEmailList = toEmailList;
        this.emailSubject = emailSubject;
        this.emailBody = emailBody;

        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", emailPort);
        emailProperties.put("mail.smtp.auth", smtpAuth);
        emailProperties.put("mail.smtp.starttls.enable", starttls);
        Log.i("GMail", "Mail server properties set.");
    }

    public MimeMessage createEmailMessage() throws AddressException,
            MessagingException, UnsupportedEncodingException {

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
        for (String toEmail : toEmailList) {
            Log.i("GMail","toEmail: "+toEmail);
            emailMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toEmail));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html");// for a html email
        // emailMessage.setText(emailBody);// for a text email
        Log.i("GMail", "Email Message created.");
        return emailMessage;
    }

    public void sendEmail() throws AddressException, MessagingException {

        Transport transport = mailSession.getTransport("smtp");
        transport.connect(emailHost, fromEmail, fromPassword);
        Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients());
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        Log.i("GMail", "Email sent successfully.");
    }

}
package com.android.imran.quickcomplain;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class SendMailTask extends AsyncTask {

    private ProgressDialog statusDialog;
    private Activity sendMailActivity;

    public SendMailTask(Activity activity) {
        sendMailActivity = activity;
    }

    protected void onPreExecute() {
        statusDialog = new ProgressDialog(sendMailActivity);
        statusDialog.setMessage("Getting ready...");
        statusDialog.setIndeterminate(false);
        statusDialog.setCancelable(false);
        statusDialog.show();
    }

    @Override
    protected Object doInBackground(Object... args) {
        try {
            Log.i("SendMailTask", "About to instantiate GMail...");
            publishProgress("Processing input....");
            GMail androidEmail = new GMail(args[0].toString(),
                    args[1].toString(), (List) args[2], args[3].toString(),
                    args[4].toString());
            publishProgress("Preparing mail message....");
            androidEmail.createEmailMessage();
            publishProgress("Sending email....");
            androidEmail.sendEmail();
            publishProgress("Email Sent.");
            Log.i("SendMailTask", "Mail Sent.");
        } catch (Exception e) {
            publishProgress(e.getMessage());
            Log.e("SendMailTask", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public void onProgressUpdate(Object... values) {
        statusDialog.setMessage(values[0].toString());

    }

    @Override
    public void onPostExecute(Object result) {
        statusDialog.dismiss();
    }

}
活动\u main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/main_layout_style">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:background="#3b004f"
            android:text="Quick Complain"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Appellor's informations"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginLeft="20dp"/>

            <EditText
                android:id="@+id/appellor_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"

                android:layout_marginBottom="10dp"
                android:hint="Enter your name (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <EditText
                android:id="@+id/appellor_mobileno"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:hint="Enter your mobile number (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <CheckBox
                android:id="@+id/make_appellor_anonymous"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Make me anonymous"
                android:layout_marginLeft="20dp"
                android:textColor="#545454" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Victim's informations"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <EditText
                android:id="@+id/victim_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:hint="Enter victim's name (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <EditText
                android:id="@+id/victim_mobileno"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:hint="Enter victim's mobile number (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <CheckBox
                android:id="@+id/same_as_appellorinfo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Same as appellor's informations"
                android:layout_marginLeft="20dp"
                android:layout_marginBottom="10dp"
                android:textColor="#545454" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Suspect's informations"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <EditText
                android:id="@+id/suspect_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:hint="Enter suspect's name (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <EditText
                android:id="@+id/suspect_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:hint="Enter suspect's details (Optional)"
                android:inputType="textMultiLine"
                android:gravity="top|left"
                android:lines="3"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>

            <CheckBox
                android:id="@+id/i_couldnt_recognize"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="I couldn't recognize"
                android:layout_marginLeft="20dp"
                android:layout_marginBottom="10dp"
                android:textColor="#545454" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Location informations"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <Spinner
                android:id="@+id/location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:padding="10dp">
            </Spinner>

            <EditText
                android:id="@+id/specify_road"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:hint="Specify road (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Crime category"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <Spinner
                android:id="@+id/category"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:padding="10dp">
            </Spinner>

            <EditText
                android:id="@+id/other_category"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:hint="Others (Optional)"
                android:textSize="15sp"
                android:textColorHint="#9C9C9C"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Crime details (Compulsory)"
                android:textColor="#3b004f"
                android:textSize="15sp"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"/>

            <EditText
                android:id="@+id/crime_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/default_view_style"
                android:padding="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:hint="Write crime details (Compulsory)"
                android:textSize="15sp"
                android:inputType="textMultiLine"
                android:gravity="top|left"
                android:lines="8"
                android:minLines="8"
                android:scrollbars="vertical"
                android:textColorHint="#9C9C9C" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Add attachment: "
                android:padding="10dp"
                android:layout_margin="5dp"
                android:textColor="#3b004f"/>

            <ImageButton
                android:id="@+id/addFile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/file"
                android:background="@null"
                android:gravity="center"
                android:layout_margin="10dp"
                />

            <ImageButton
                android:id="@+id/takePhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/camera"
                android:background="@null"
                android:gravity="center"
                android:layout_margin="10dp"/>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <CheckBox
                android:id="@+id/terms_conditions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="By clicking submit, I agree to the terms &amp; conditions."
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:textColor="#545454" />

            <Button
                android:id="@+id/submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_style"
                android:text="Submit"
                android:textColor="#FFF"
                android:textAllCaps="false"
                android:textSize="15sp"
                android:layout_marginLeft="150dp"
                android:layout_marginRight="150dp"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="20dp"/>
        </LinearLayout>

    </LinearLayout>

</ScrollView>

我有向清单文件添加权限:

<uses-permission android:name="android.permission.INTERNET"/>


应用程序崩溃的原因是什么?我看不出这些日志。请帮我解决这个问题。

你看到了吗?NPE是由
publishProgress(e.getMessage())引起的
e.getMessage()
为空。(因为在
onProgressUpdate
中,
values[0]
为空,对于
AuthenticationFailedException
,这似乎是不言自明的。那么,我该如何解决这个问题?@njzk2fix您的身份验证?