K.Muthu
K.Muthu

Reputation: 1252

send email without user interaction in android geting auth error

Hi every one following code for i worte send email in android but i am geting auth error kaindly help me to solve this

    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("[email protected]", "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("[email protected]", "*********");
            }
        });
    }
}
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;
    }

}

Getting Error like this:

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)

same timei get mail like bellow :

sub : Sign-in attempt prevented

mail :

Hi name some one just try to sign in your google account "mailid " form app that doesn't meet modern security standards.

Upvotes: 1

Views: 613

Answers (1)

King of Masses
King of Masses

Reputation: 18765

These are the most common things for AuthenticationFailedException in JavaMail

  1. First, fix these common mistakes.
  2. Turn on Session debugging to see if the protocol trace gives any more clues about what's wrong.
  3. You may need to enable access for less secure apps. This error may be from google security... This can be Resolved by Enabling Less Secure "TURN ON"

After these all steps, still your facing problem ? I am here to help you !!

Upvotes: 1

Related Questions