英文:
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();
}
}
});
这是我的对话框活动:
英文:
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:
答案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();
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论