在从 Android 应用发送电子邮件时出现问题。

huangapple go评论117阅读模式
英文:

Problem when sending email from Android app

问题

以下是翻译好的内容:

我正试图从我的Android应用程序直接发送电子邮件给收件人。但是当我点击发送时,什么都不会发生。成功的提示信息也不会出现。

我使用了以下这些类:JSSEProvider、ByteArrayDatasource、MailSender。

这是我的主要代码:

Send_mail.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if ("".equals(recipient_mail.getText().toString().trim())) {
            Toast.makeText(FirstActivity.this, "请输入收件人邮箱", Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        MailSender sender = new MailSender(sender_mail.getText().toString(),
                                mail_password.getText().toString());
                        sender.sendMail(Subject.getText().toString(), Text.getText().toString(),
                                sender_mail.getText().toString(), recipient_mail.getText().toString());

                        Toast.makeText(context, "发送成功!", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Log.e("SendMail", e.getMessage(), e);
                    }
                }
            }).start();
        }
    }
});

这是我的对话框活动:

在从 Android 应用发送电子邮件时出现问题。

英文:

I'm trying to send an email from my Android app directly to the recipient. But when I click send nothing happens. The success toast doesn't appear.

I used these classes: JSSEProvider, ByteArrayDatasource,MailSender

This is my main code:

Send_mail.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if ("".equals(recipient_mail.getText().toString().trim())) {
            Toast.makeText(FirstActivity.this, "Enter Recipent Email ", Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        MailSender sender = new MailSender(sender_mail.getText().toString(),
                                mail_password.getText().toString());
                        sender.sendMail(Subject.getText().toString(), Text.getText().toString(),
                                sender_mail.getText().toString(), recipient_mail.getText().toString());

                        Toast.makeText(context, "Success!", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        Log.e("SendMail", e.getMessage(), e);
                    }
                }
            }).start();
        }
    }
});

This is my dialog activity:

在从 Android 应用发送电子邮件时出现问题。

答案1

得分: 1

在线程内部无法显示 Toast 消息,您需要切换到主线程来实现此操作。因此,请将您的 Toast 消息替换为以下代码:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(FirstActivity.this, "成功!", Toast.LENGTH_SHORT).show();
    }
});
英文:

You can not show a Toast message within a thread, you need to reach the main thread to achieve this. So, replace your toast message with this

runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      Toast.makeText(FirstActivity.this, "Success!", Toast.LENGTH_SHORT).show();
                    }
                });

huangapple
  • 本文由 发表于 2020年4月5日 02:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定