2014-09-29 42 views
0

我在這篇文章中使用代碼提及在我的應用程序的後臺發送電子郵件:Sending Email in Android using JavaMail API without using the default/built-in app。我把性能調試選項來檢查和驗證這樣的郵件發送過程:電子郵件不從Android的背景發送?

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

,當我從我的應用程序發送的郵件就說明下面這樣的消息「請登錄您的帳戶,從您的瀏覽器」我檢查了我的密碼和電子郵件,他們是正確的,請幫我找出實際問題。 謝謝!

回答

0

首先你身份進行驗證發件人的,然後在你的代碼發送電子郵件這樣的使用javax.mail

props.put("mail.smtp.user", "[email protected]"); 
     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("[email protected]")); 
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); 

      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 =新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([email protected]@gmail.com", "YourPASSWORD"); 
     } 
    } 
+0

感謝您的評論,我正在按照發送郵件的確切程序,因爲我在我的帖子中提到它不工作。 – 2014-09-29 11:58:10

+0

可以把你的代碼放在這裏?那麼我可以嘗試找出什麼是錯的 – 2014-09-29 11:59:35

+0

還檢查權限 – 2014-09-29 12:00:14

相關問題