【问题标题】:Email not sending from background in android?电子邮件不是从android中的后台发送的?
【发布时间】:2014-11-23 18:20:55
【问题描述】:

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

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

当我从我的应用程序发送邮件时,它显示以下消息“请从您的浏览器登录您的帐户”,我检查了我的密码并通过电子邮件发送它们是正确的,请帮助我找出实际问题。 谢谢!

【问题讨论】:

    标签: android email


    【解决方案1】:

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

            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");
           }
       }
    

    【讨论】:

    • 感谢您的评论,我正在按照我在帖子中提到的发送邮件的确切程序进行操作。
    • 可以把你的代码放在这里吗?然后我可以尝试找出问题所在
    • 同时检查权限
    • 嗨,我正在添加所有必要的权限 和代码和这个帖子一模一样:stackoverflow.com/questions/2020088/…
    • 您的 logcat 中是否有任何异常?如果您正在获取 NetWorkOnMainThread,那么您应该在 AsyncTask 中进行电子邮件发送过程。如果发生任何异常,我想看看你的 logcat。
    猜你喜欢
    • 2011-09-13
    • 1970-01-01
    • 2015-04-30
    • 2015-05-19
    • 2013-11-25
    • 2013-04-03
    • 2011-08-08
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多