尝试使用JavaMail API从我的Gmail帐户发送电子邮件时,我在LogCat中收到以下错误消息。
11-09 11:04:14.385: W/System.err(18443): javax.mail.AuthenticationFailedException
11-09 11:04:14.385: W/System.err(18443): at javax.mail.Service.connect(Service.java:319)
11-09 11:04:14.385: W/System.err(18443): at javax.mail.Service.connect(Service.java:169)
11-09 11:04:14.385: W/System.err(18443): at javax.mail.Service.connect(Service.java:118)
11-09 11:04:14.385: W/System.err(18443): at com.srindroid.sendemail.MainActivity$RetrievedFeedTask.doInBackground(MainActivity.java:88)
11-09 11:04:14.385: W/System.err(18443): at com.srindroid.sendemail.MainActivity$RetrievedFeedTask.doInBackground(MainActivity.java:1)
11-09 11:04:14.385: W/System.err(18443): at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-09 11:04:14.385: W/System.err(18443): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-09 11:04:14.385: W/System.err(18443): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-09 11:04:14.385: W/System.err(18443): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-09 11:04:14.385: W/System.err(18443): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-09 11:04:14.385: W/System.err(18443): at java.lang.Thread.run(Thread.java:841)
源代码
package com.srindroid.sendemail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
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;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
EditText rec, subject, message;
String strRec, strSubject, strMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button submit = (Button) findViewById (R.id.btnSendEmail);
rec = (EditText) findViewById (R.id.txtToEmail);
subject = (EditText) findViewById (R.id.txtSubject);
message = (EditText) findViewById (R.id.txtMessage);
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
strRec = rec.getText().toString();
strSubject = subject.getText().toString();
strMessage = subject.getText().toString();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("indianclassics27@gmail.com",**<MyPassword>**);
}
});
pdialog = ProgressDialog.show(context, "Send Email", "Sending Email...");
RetrievedFeedTask task = new RetrievedFeedTask();
task.execute();
}
class RetrievedFeedTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("indianclassics27@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strRec));
message.setSubject(strSubject);
message.setContent(strMessage, "text/html; charset=utf-8");
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
pdialog.dismiss();
rec.setText("");
subject.setText("");
message.setText("");
Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
您能帮我理解创建此AuthenticationFailedException的原因吗?请注意,当我在www.gmail.com使用相同的凭据时,我能够成功登录我的电子邮件帐户。
谢谢, 作者Srini
答案 0 :(得分:7)
首先,修复这些common mistakes。
开启Session debugging以查看协议跟踪是否提供了有关错误的更多线索。
答案 1 :(得分:4)
转到您的邮箱,然后点击我的帐户,然后选择登录&amp;安全选项
然后在底部显示允许不太安全的应用程序:关闭已关闭所以打开此选项,现在您可以发送后台邮件。
谢谢