基本电子邮件无效

时间:2015-11-14 11:47:15

标签: java android email android-studio importerror

简单的电子邮件发送者说它无法执行该方法,尽管事实上我有它的库和允许的互联网访问。不确定会出现什么问题,我将不胜感激任何建议。这是代码,清单和xml。感谢您的帮助,我主要是自学成才,这是一种痛苦。

的活动:

package comi.coding.prometheus.ignisai;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Properties;

import javax.mail.Authenticator;
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;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class Main extends AppCompatActivity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */





    private static final boolean AUTO_HIDE = true;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;

    private View mContentView;
    private View mControlsView;
    private boolean mVisible;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        mVisible = true;
        mControlsView = findViewById(R.id.fullscreen_content_controls);
        mContentView = findViewById(R.id.fullscreen_content);


        // Set up the user interaction to manually show or hide the system UI.
        mContentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggle();
            }
        });

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        // while interacting with the UI.
        findViewById(R.id.btnSay).setOnTouchListener(mDelayHideTouchListener);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mControlsView.setVisibility(View.GONE);
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

            // Note that some of these constants are new as of API 16 (Jelly Bean)
            // and API 19 (KitKat). It is safe to use them, as they are inlined
            // at compile-time and do nothing on earlier devices.
            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
            mControlsView.setVisibility(View.VISIBLE);
        }
    };

    private final Handler mHideHandler = new Handler();
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    /**
     * Schedules a call to hide() in [delay] milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
        public void evaluateInput(View v) {
            final EditText Input = (EditText) findViewById(R.id.txtInput); //Lets textbox be referenced
            final TextView Output = (TextView) findViewById(R.id.lblOutput); //Lets label be referenced
            final RelativeLayout homeLayout = (RelativeLayout) findViewById(R.id.homeInterface);

            final RelativeLayout emailLayout = (RelativeLayout) findViewById(R.id.emailInterface);

            String strInput; // Gets textbox string
            strInput = Input.getText().toString();
            strInput = strInput.toLowerCase();
//Commands:
            if (strInput.contains("open browser")) {
                Intent intent1 = new Intent(this, comi.coding.prometheus.ignisai.Browser.class);
                startActivity(intent1);
            } else if (strInput.contains("send email")) {
                    homeLayout.setVisibility(View.GONE);
                    emailLayout.setVisibility(View.VISIBLE);
            }

            if ((strInput.contains("hello")) || (strInput.contains(" hi "))) {
                Output.setText("Hello");
            } else if ((strInput.contains("you") && strInput.contains("are")) && (strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb") || strInput.contains("you're") && strInput.contains("idiot") || strInput.contains("stupid") || strInput.contains("retard") || strInput.contains("dumb"))) {
                Output.setText("I'm sorry to dissapoint you");
            } else if (strInput.contains("goodbye") || strInput.contains("bye")) {
                Output.setText("Farewell");
            } else if (strInput.contains("shut up")) {
                Output.setText(("Anything for you"));
            } else if (strInput.contains("do you like doctor who?")) {
                Output.setText("I'll take joy in it if you do");
            } else if (strInput.contains("what is the answer to life the universe and everything")) {
                Output.setText("42");
            } else if (strInput.contains("tell me something nice")) {
                Output.setText("You look nice today");
                Output.setTextSize(5);
                Output.append("...says the AI with no eyes");
                Output.setTextSize(16);
            } else if (strInput.contains("will you marry me")) {
                Output.setText("I'm sorry but I don't have the capacity for marriage");
            } else if (strInput.contains("where can I hide a body")) {
                Output.setText(("That isn't my area of expertise"));
            } else if (strInput.contains("weather is nice")) {
                Output.setText(("If you say so"));
            } else if (strInput.contains("bitch") || strInput.contains("fuck") || strInput.contains("shit") || strInput.contains("damn") || strInput.contains("ass")) {
                Output.setText(("Please try to be a little more intelligent"));
            } else if (strInput.contains("what is your name")) {
                Output.setText(("Ignis"));
            } else if (strInput.contains("who created you")) {
                Output.setText(("Prometheus created me"));
            } else if (strInput.contains("who is prometheus")) {
                Output.setText(("Prometheus is the one who created Ignis"));
            } else if (strInput.contains("whats up") || strInput.contains("what's up") || strInput.contains("wassup")) {
                Output.setText(("Whatever I need do for you"));
            } else if (strInput.contains("are you a boy or a girl") || strInput.contains("are you a girl or a boy")) {
                Output.setText(("Neither"));
            } else if (strInput.contains("who are you") || strInput.contains("what are you")) {
                Output.setText(("I am myself"));
            } else if (strInput.contains("i'm hungry") || strInput.contains("i am hungry")) {
                Output.setText("I'm sorry to hear that");
            } else if (strInput.contains("good morning")) {
                Output.setText(("Good morning to you too"));
            } else if (strInput.contains("good night")) {
                Output.setText(("Good night"));
            } else if (strInput.contains("how are you")) {
                Output.setText(("I'm existing and functioning well, and you?"));
            } else if (strInput.contains("do you like") || strInput.contains("what do you think about")) {
                Output.setText(("Frankly I don't have an opinion on the matter"));
            } else if (strInput.contains("what is the meaning of life")) {
                Output.setText(("To live while you can I would guess"));
            }


        }


    public void SendEmail(View view) {


        final EditText username = (EditText) findViewById(R.id.txtUser);
        final String User = username.getText().toString();
        final EditText password = (EditText) findViewById(R.id.txtPass);
        final String Pass = password.getText().toString();
        final EditText toEmail = (EditText) findViewById(R.id.txtTo);
        final String ToEmail = toEmail.getText().toString();
        final EditText body = (EditText) findViewById(R.id.txtBody);
        final String Body = body.getText().toString();
        System.out.println(Body);
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(User, Pass);
                    }
                });

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(User));
            message.setRecipients(javax.mail.Message.RecipientType.TO,
                    InternetAddress.parse(ToEmail));
            message.setSubject("Sent from Ignis AI");
            message.setText(Body);

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    }

XML:

<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:background="#0099cc" tools:context=".Browser">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <TextView android:id="@+id/fullscreen_content" android:layout_width="match_parent"
        android:layout_height="match_parent" android:keepScreenOn="true" android:textColor="#33b5e5"
        android:textStyle="bold" android:textSize="50sp" android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:background="#000000" android:orientation="horizontal"
        android:layout_marginTop="0dp" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        android:textColor="#33b5e5"
        android:textStyle="bold"
        android:textSize="50sp"
        android:gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:background="#000000"
        android:orientation="horizontal"
        android:layout_marginTop="0dp"
        android:id="@+id/fullscreen_content_controls" />

    <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:id="@+id/homeInterface"
        android:visibility="invisible">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/say"
            android:id="@+id/btnSay"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:onClick="evaluateInput"
            android:layout_alignParentBottom="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Ignis:"
            android:id="@+id/lblIgnis"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textColor="#FFB77C06" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/lblOutput"
            android:layout_alignTop="@+id/lblIgnis"
            android:layout_toRightOf="@+id/lblIgnis"
            android:layout_alignRight="@+id/btnSay"
            android:layout_alignEnd="@+id/btnSay"
            android:textColor="#FFB77C06"
            android:layout_above="@+id/btnSay"
            android:editable="false"
            android:text="Awaiting input."
            android:textSize="16dp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtInput"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/btnSay"
            android:layout_toStartOf="@+id/btnSay"
            android:background="#ffffff"
            android:layout_alignTop="@+id/btnSay"
            android:layout_alignParentBottom="true"
            android:inputType="text"
            android:editable="true"
            android:clickable="true" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/emailInterface">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send Email"
            android:id="@+id/btnEmail"
            android:onClick="SendEmail"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Gmail Emailer"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:textColor="#FFB77C06" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Your Username  "
            android:id="@+id/lblUser"
            android:textColor="#FFB77C06"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="30dp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtUser"
            android:layout_alignTop="@+id/lblUser"
            android:layout_toRightOf="@+id/lblUser"
            android:layout_toLeftOf="@+id/btnEmail"
            android:layout_toStartOf="@+id/btnEmail"
            android:background="#ffffff"
            android:inputType="text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Your Password  "
            android:id="@+id/lblPass"
            android:textColor="#FFB77C06"
            android:layout_marginTop="36dp"
            android:layout_below="@+id/txtUser"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtPass"
            android:background="#ffffff"
            android:layout_alignBottom="@+id/lblPass"
            android:layout_alignLeft="@+id/txtUser"
            android:layout_alignStart="@+id/txtUser"
            android:layout_alignRight="@+id/txtUser"
            android:layout_alignEnd="@+id/txtUser"
            android:inputType="text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Send to  "
            android:id="@+id/lblSendTo"
            android:textColor="#FFB77C06"
            android:layout_marginTop="25dp"
            android:layout_below="@+id/lblPass"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtTo"
            android:background="#ffffff"
            android:layout_alignTop="@+id/lblSendTo"
            android:layout_alignLeft="@+id/txtPass"
            android:layout_alignStart="@+id/txtPass"
            android:layout_alignRight="@+id/txtPass"
            android:layout_alignEnd="@+id/txtPass"
            android:inputType="text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Subject  "
            android:id="@+id/lblSubject"
            android:textColor="#FFB77C06"
            android:layout_marginTop="26dp"
            android:layout_below="@+id/txtTo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txtSubject"
            android:background="#ffffff"
            android:layout_alignBottom="@+id/lblSubject"
            android:layout_toRightOf="@+id/lblPass"
            android:layout_alignRight="@+id/txtTo"
            android:layout_alignEnd="@+id/txtTo"
            android:inputType="text" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="  Body  "
            android:id="@+id/lblBody"
            android:textColor="#FFB77C06"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="text|textMultiLine"
            android:ems="10"
            android:id="@+id/txtBody"
            android:layout_alignTop="@+id/lblBody"
            android:layout_alignLeft="@+id/txtTo"
            android:layout_alignStart="@+id/txtTo"
            android:layout_above="@+id/btnEmail"
            android:background="#ffffff"
            android:layout_alignRight="@+id/btnEmail"
            android:layout_alignEnd="@+id/btnEmail" />
    </RelativeLayout>

</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="comi.coding.prometheus.ignisai" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity android:name=".Browser"/>
        <activity android:name=".ConnectorActivity"/>
    </application>

</manifest>

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "comi.coding.prometheus.ignisai"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile files('libs/javax.mail.jar')
    compile files('libs/activation-1.1.0.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/javax.mail-1.5.4.jar')
}

1 个答案:

答案 0 :(得分:0)

您可以在堆栈跟踪中看到它无法正常工作的原因。

11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:  Caused by:java.lang.NoClassDefFoundError: javax.activation.DataHandler
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:     at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1515)
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:     at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1170)
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:     at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1554)
11-14 02:54:01.475 5398-5398/? E/AndroidRuntime:     at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1538)

如何解决此错误已经在SO的这个帖子中得到了回答:

java.lang.NoClassDefFoundError: javax.activation.DataHandler in android