电子邮件不是从android中的后台发送的?

时间:2014-09-29 11:10:44

标签: android email

我正在使用此帖中提及的代码在我的应用中发送电子邮件:Sending Email in Android using JavaMail API without using the default/built-in app。我在属性中放置了一个调试选项来检查和验证电子邮件发送过程,如下所示:

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

当我从我的应用程序发送邮件时,它会显示以下消息"请从您的浏览器登录到您的帐户"我检查了我的密码和电子邮件,他们是正确的,请帮我弄清楚实际问题。 谢谢!

1 个答案:

答案 0 :(得分:0)

首先您作为发件人进行身份验证,然后使用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");
       }
   }