0

I am sending email in background in my app using code mention in this post: Sending Email in Android using JavaMail API without using the default/built-in app. I put a debug option in properties to check and verify email sending process like this:

props.put("mail.debug", "true");

and when i send mail from my app it shows following message like this "Please log-in to your account from you browser" i checked my password and email they are correct, please help me to figure out the actual problem. Thanks!

Community
  • 1
  • 1
Abhishek Kotiyal
  • 283
  • 1
  • 5
  • 17

1 Answers1

0

First you Authenticate as sender's and then send email in your code like this using javax.mail

props.put("mail.smtp.user", "YOUREMAIL@gmail.com");
       props.put("mail.smtp.host", d_host);
       props.put("mail.smtp.port", d_port);
       props.put("mail.smtp.starttls.enable", "true");
       props.put("mail.smtp.auth", "true");
       //props.put("mail.smtp.debug", "true");
       props.put("mail.smtp.socketFactory.port", d_port);
       props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
       props.put("mail.smtp.socketFactory.fallback", "false");

       try {
           Authenticator auth = new SMTPAuthenticator();
           Session session = Session.getInstance(props, auth);     
           MimeMessage msg = new MimeMessage(session);

           msg.setSubject(m_subject);
           msg.setFrom(new InternetAddress("YOUREMAIL@gmail.com"));
           msg.addRecipient(Message.RecipientType.TO, new InternetAddress("DESTINATION_EMAIL@gmail.com"));

           Multipart multipart = new MimeMultipart();  

           String data=" Hello There.";




           //attach Image
           BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(pathUserImg); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName("IMG_NAME.png"); 

        //attach String Data

MimeBodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setText("data: "+data);

        multipart.addBodyPart(messageBodyPart); 
        multipart.addBodyPart(messageBodyPart1); 

           msg.setContent(multipart );  

          Transport.send(msg);


       } catch (Exception mex)
       {
           mex.printStackTrace();
           Log.v("TAG", " Email Not Sent "+mex.getMessage());
       }


   private class SMTPAuthenticator extends javax.mail.Authenticator
   {
       public PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(YOURrEMAIL@gmail.com@gmail.com", "YourPASSWORD");
       }
   }