Java(Android工作室)电子邮件

时间:2015-11-11 10:01:33

标签: java android email

我目前正在Android工作室处理AI,并且遇到电子邮件问题。基本上,我点击"发送"按钮,绝对没有任何反应。我已经仔细检查了获取电子邮件传递/地址/地址的代码,并且所有ID都匹配并拥有正确的方法。该方法还匹配正确的按钮ID。但是,它没有输出错误,它也没有发送电子邮件,据我所知,它什么也没做。谢谢你的帮助。

 public void SendEmail(View view) {


        final EditText username = (EditText) findViewById(R.id.IgnisEmailUser);
   final String User = username.getText().toString();
        final EditText password = (EditText) findViewById(R.id.IgnisEmailPass);
    final String Pass = password.getText().toString();
    final EditText toEmail = (EditText) findViewById(R.id.IgnisTheirEmail);
    final String ToEmail = toEmail.getText().toString();
    final EditText body = (EditText) findViewById(R.id.IgnisEmailBody);
    final String Body = body.getText().toString();

    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 javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(User, Pass);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(User));
            message.setRecipients(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:

<!-- 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" />

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

    <LinearLayout android:id="@+id/fullscreen_content_controls" style="?metaButtonBarStyle"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay" android:orientation="horizontal"
        tools:ignore="UselessParent"
        android:weightSum="1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="1022dp">

    </LinearLayout>

    <EditText
        android:layout_width="249dp"
        android:layout_height="35dp"
        android:id="@+id/editText"
        android:layout_gravity="left|bottom"
        android:background="#ffffff"
        android:editable="false"
        android:height="50dp"
        android:minHeight="50dp"
        android:maxHeight="50dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="952dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ButtonSay"
        android:id="@+id/dummy_button"
        android:textColor="#b77c06"
        android:background="#b6b6b6"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:clickable="true"
        android:onClick="evaluateInput" />/>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/IgnisName"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="16dp"
        android:textColor="#b77c06" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/lblOutput"
        android:textColor="#b77c06"
        android:textSize="16dp"
        android:text="@string/IgnisOutput"
        android:layout_alignTop="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_above="@+id/dummy_button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="101dp"
    android:keepScreenOn="true"
    android:textColor="#b77c06"
    android:textStyle="bold"
    android:textSize="50sp"
    android:gravity="center"
    android:text="@string/Title"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp" />

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

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/IgnisEmailUser"
        android:layout_marginTop="96dp"
        android:visibility="visible"
        android:background="#ffffff"
        android:layout_alignParentTop="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="Your email address:"
        android:id="@+id/lblEmailUser"
        android:visibility="visible"
        android:background="#ffffff"
        android:layout_alignTop="@+id/IgnisEmailUser"
        android:layout_toLeftOf="@+id/IgnisEmailUser"
        android:layout_toStartOf="@+id/IgnisEmailUser"
        android:layout_marginRight="25dp"
        android:layout_marginEnd="25dp"
        android:layout_alignBottom="@+id/IgnisEmailUser" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/IgnisEmailPass"
        android:visibility="visible"
        android:background="#ffffff"
        android:layout_alignBottom="@+id/lblEmailPass"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:autoText="false"
        android:inputType="textPassword" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Your email pass:"
        android:id="@+id/lblEmailPass"
        android:visibility="visible"
        android:background="#ffffff"
        android:autoText="false"
        android:layout_below="@+id/lblEmailUser"
        android:layout_alignLeft="@+id/lblEmailUser"
        android:layout_alignStart="@+id/lblEmailUser"
        android:layout_marginTop="26dp"
        android:layout_alignRight="@+id/lblEmailUser"
        android:layout_alignEnd="@+id/lblEmailUser" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Their email address:"
        android:id="@+id/lblTheirEmail"
        android:visibility="visible"
        android:background="#ffffff"
        android:autoText="false"
        android:layout_marginTop="26dp"
        android:layout_below="@+id/lblEmailPass"
        android:layout_alignLeft="@+id/lblEmailPass"
        android:layout_alignStart="@+id/lblEmailPass"
        android:layout_alignRight="@+id/lblEmailPass"
        android:layout_alignEnd="@+id/lblEmailPass" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/IgnisTheirEmail"
        android:visibility="visible"
        android:background="#ffffff"
        android:layout_alignTop="@+id/lblTheirEmail"
        android:layout_alignLeft="@+id/IgnisEmailPass"
        android:layout_alignStart="@+id/IgnisEmailPass"
        android:layout_alignBottom="@+id/lblTheirEmail" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Body:"
        android:id="@+id/lblEmailBody"
        android:visibility="visible"
        android:background="#ffffff"
        android:autoText="false"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/lblTheirEmail"
        android:layout_alignStart="@+id/lblTheirEmail" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/IgnisEmailBody"
        android:layout_alignTop="@+id/lblEmailBody"
        android:layout_toRightOf="@+id/lblEmailBody"
        android:layout_marginLeft="26dp"
        android:layout_marginStart="26dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/SendEmail"
        android:background="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return Home"
        android:id="@+id/EmailReturnHome"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/SendEmail"
        android:layout_toStartOf="@+id/SendEmail" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/SendEmail"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/IgnisEmailBody"
        android:layout_alignEnd="@+id/IgnisEmailBody" />
</RelativeLayout>

<!-- This FrameLayout insets its children based on system windows using
     android:fitsSystemWindows. -->

1 个答案:

答案 0 :(得分:0)

它不是针对按钮的id而是针对onClick属性进行评估的。该按钮应该是,您可以根据需要选择任何ID。

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/SendEmail"
        android:onClick="SendEmail"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/IgnisEmailBody"
        android:layout_alignEnd="@+id/IgnisEmailBody" />