Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我';我得到javax.mail.AuthenticationFailedException?_Java_Android_Email - Fatal编程技术网

为什么我';我得到javax.mail.AuthenticationFailedException?

为什么我';我得到javax.mail.AuthenticationFailedException?,java,android,email,Java,Android,Email,有人能告诉我为什么应用程序会抛出这个吗 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)

有人能告诉我为什么应用程序会抛出这个吗

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;
  }
}

package com.example.***.Utils;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.widget.Toast;
导入java.util.Properties;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
公共类SendMail扩展了异步任务{
私人语境;
非公开会议;
私人字符串电子邮件;
私有字符串主题;
私有字符串消息;
public SendMail(上下文上下文、字符串电子邮件、字符串主题、字符串消息)
{
this.context=上下文;
this.email=电子邮件;
this.subject=主语;
this.message=消息;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Toast.makeText(上下文,“发送邮件…”),Toast.LENGTH_SHORT.show();
}
@凌驾
受保护的void onPostExecute(void避免)
{
super.onPostExecute(避免);
Toast.makeText(上下文,“消息已发送”,Toast.LENGTH_SHORT).show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
Properties props=新属性();
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.socketFactory.port”,“465”);
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(){
受保护的密码身份验证getPasswordAuthentication(){
返回新的PasswordAuthentication(Config.EMAIL,Config.PASSWORD);
}
});
试一试{
MimeMessage mm=新MimeMessage(会话);
mm.setFrom(新的InternetAddress(Config.EMAIL));
mm.addRecipient(Message.RecipientType.TO,新的Internet地址(电子邮件));
mm.设置主题(主题);
mm.setText(消息);
传输发送(mm);
}
捕获(消息异常e){
e、 printStackTrace();
}
返回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();
String email=“*******”;
字符串subject=“Activation”;
String message=“此用户需要激活:”+”;
SendMail sm=新的SendMail(此、电子邮件、主题、消息);
sm.execute();
我提供了正确的密码,我检查了我的谷歌帐户,它应该工作 你能帮帮我吗


提前谢谢你

您的实现中缺少一个属性

使用props.put(“mail.smtp.ssl.trust”、“smtp.gmail.com”)doInBackground()方法中的code>来解决此问题

说明:

发件人: 以下代码片段显示了在应用程序中合并所需配置的简单方法:

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);

您的实现中缺少一个属性

使用props.put(“mail.smtp.ssl.trust”、“smtp.gmail.com”)doInBackground()方法中的code>来解决此问题

说明:

发件人: 以下代码片段显示了在应用程序中合并所需配置的简单方法:

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);

嘿,你需要添加这个
props.put(“mail.smtp.ssl.trust”,“smtp.gmail.com”)我希望这对你有帮助。嘿,你需要添加这个
props.put(“mail.smtp.ssl.trust”,“smtp.gmail.com”)