有人可以告诉我为什么应用程序会抛出这个吗?
W/System.err: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
W/System.err: at javax.mail.Transport.send(Transport.java:118)
at com.example.**.Utils.SendMail.doInBackground(SendMail.java:71)
at com.example.**.Utils.SendMail.doInBackground(SendMail.java:19)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
我正在尝试使用javamail api发送邮件。
我想提一下,我检查了邮件帐户,但安全性较差的应用程序。
这是SendMail类
package com.example.****.Utils;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.Properties;
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;
public class SendMail extends AsyncTask<Void, Void, Void> {
private Context context;
private Session session;
private String email;
private String subject;
private String message;
public SendMail(Context context, String email, String subject, String message)
{
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(context, "Sending mail...", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
Toast.makeText(context, "Message Sent", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... params) {
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", "true");
props.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
try {
MimeMessage mm = new MimeMessage(session);
mm.setFrom(new InternetAddress(Config.EMAIL));
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
mm.setSubject(subject);
mm.setText(message);
Transport.send(mm);
}
catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
这里我正在使用它
String email = "******";
String subject = "Activation";
String message = "This User requires activation: " + "<a href='http://localhost:80/approve/" +mAuth.getCurrentUser().getEmail() +"> Click here </a>";
SendMail sm = new SendMail(this, email, subject,message);
sm.execute();
我提供了正确的密码,我检查了自己的Google帐户,该密码应该可以正常工作 你能帮我吗?
提前谢谢!
答案 0 :(得分:0)
您的实现中缺少一个属性。
在您的props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
方法中使用doInBackground()
可以解决此问题。
来自java mail documentation: 下面的代码片段显示了一种在应用程序中合并所需配置的简单方法:
String host = "smtp.gmail.com";
String username = "user";
String password = "passwd";
Properties props = new Properties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.ssl.enable", "true");
// set any other needed mail.smtp.* properties here
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport.send(msg, username, password);