问题描述
我想在Android应用中发送电子邮件,而不使用电子邮件客户端(无需用户交互)。我有代码,我在我的应用程序中尝试过。但是当我试图运行时,它会显示一条通知Message sent。但是我没有从代码中提到的特定ID中收到任何电子邮件。我也没有收到任何错误或警告。请任何人帮我解决这个问题。谢谢
I want to send an email in android app without using email client(without user interaction). I have code and I tried it in my app. But when I tried to run, it showed a notification "Message sent". But I did not receive any email from the particular id which I mentioned in the code. I did not get any error or warnings too. Please anyone help me to get out of this problem. Thanks
这是我的代码
//Import files
public class SendReport extends ActionBarActivity implements View.OnClickListener {
Button button;
EditText editText, editText2;
String recipient;
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
String textmessage = null;
String username = "[email protected]";
String password = "senderpassword";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_report);
context = this;
button = (Button) findViewById(R.id.sendbutton);
editText = (EditText) findViewById(R.id.textView2);
editText2 = (EditText) findViewById(R.id.textView4);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
recipient = editText.getText().toString();
textmessage = editText2.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.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
pdialog = ProgressDialog.show(context, "", "Sending Mail...", true);
RetrieveFeedTask task = new RetrieveFeedTask();
task.execute();
}
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setContent(textmessage,"text/html; charset=utf-8");
Transport.send(message);
}
catch(MessagingException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String feed) {
pdialog.dismiss();
editText2.setText("");
editText.setText("");
Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
}
}
推荐答案
几个月前发生这个问题。
I had this problem some months ago.
您必须允许安全性较低的应用才能访问您的Google帐户。
You have to allowing less secure apps to access your google account.
更改它
现在您的电子邮件必须正确发送;)
Now your email must be correctly sent ;)
更多信息:
这篇关于如何使用javamail API在android中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!