在android geting auth错误中发送没有用户交互的电子邮件

时间:2015-08-14 07:19:29

标签: java android gmail javamail

您好以下每个代码为我worte发送电子邮件在Android但我正在geh auth错误kaindly帮我解决这个

    private void sendMail(String email, String subject, String messageBody,File file) {
        Session session = createSessionObject();
//        new UpdateTask().execute();
        try {
            Message message = createMessage(email, subject, messageBody, session,file);

            new UpdateTask().execute(message);

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private Message createMessage(String email, String subject, String messageBody, Session session,File file) throws MessagingException, UnsupportedEncodingException {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("XXXXXXXX@gmail.com", "Sound Check"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
        message.setSubject(subject);
        message.setText(messageBody);
        /**
         * Attach a file in mail 
         */
        Multipart multipart = new MimeMultipart();
        BodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(file.getName());

        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
        return message;
    }

    private Session createSessionObject() {
           Properties props = new Properties();   
           props.setProperty("mail.transport.protocol", "smtp");   
           props.setProperty("mail.host", "smtp.gmail.com");   
           props.put("mail.smtp.auth", "true");   
           props.put("mail.smtp.port", "465");   
           props.put("mail.smtp.socketFactory.port", "465");   
           props.put("mail.smtp.socketFactory.class",   
                   "javax.net.ssl.SSLSocketFactory");   
           props.put("mail.smtp.socketFactory.fallback", "false");   
           props.setProperty("mail.smtp.quitwait", "false");   

        return Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxxxxxxxxx@gmail.com", "*********");
            }
        });
    }
}
class UpdateTask extends AsyncTask<Message,String,String> {



    @Override
    protected String doInBackground(Message... params) {
        // TODO Auto-generated method stub
        Message message = params[0];
         try {

                Transport.send(message);

            } catch (AddressException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }

        return null;
    }

}

像这样得到错误:

08-14 12:09:23.365: W/System.err(30695): javax.mail.AuthenticationFailedException
08-14 12:09:23.365: W/System.err(30695):    at javax.mail.Service.connect(Service.java:319)
08-14 12:09:23.365: W/System.err(30695):    at javax.mail.Service.connect(Service.java:169)
08-14 12:09:23.365: W/System.err(30695):    at javax.mail.Service.connect(Service.java:118)
08-14 12:09:23.365: W/System.err(30695):    at javax.mail.Transport.send0(Transport.java:188)
08-14 12:09:23.365: W/System.err(30695):    at javax.mail.Transport.send(Transport.java:118)
08-14 12:09:23.365: W/System.err(30695):    at com.example.callrecoder.UpdateTask.doInBackground(RecordService.java:384)

同一时间收到邮件如下:

sub:登录尝试被阻止

邮件:

你好名字  有人试图登录你的谷歌帐户&#34; mailid&#34;表单应用程序不符合现代安全标准。

1 个答案:

答案 0 :(得分:1)

这些是JavaMail中AuthenticationFailedException最常见的事情

  1. 首先,修复这些common mistakes.
  2. 打开Session debugging以查看协议跟踪是否给出 关于什么是错的更多线索。
  3. 您可能需要enable access for less secure apps.此错误可能来自谷歌安全...这可以通过启用不太安全的“开启”来解决
  4. 完成这些步骤后,仍然面临着问题吗?我在这里帮助你!!