在Android中发送电子邮件

时间:2014-01-30 19:30:27

标签: android email gmail javamail

我正在从我的应用发送电子邮件,而不使用电子邮件意图。但是gmail将它们检测为垃圾邮件并阻止发送。这是错误传递报告,

  

交付给以下收件人永久失败:

 customer@abc.in
     

永久性失败的技术细节:消息被拒绝。看到   http://support.google.com/mail/bin/answer.py?answer=69585了解更多信息   信息。

     

-----原始信息-----

     

X-Received:带有SMTP ID的10.68.196.164   in4mr16083110pbc.128.1391108875462;           星期四,2014年1月30日11:07:55 -0800(太平洋标准时间)返回路径:收到:来自localhost([122.166.89.61])           by mx.google.com with ESMTPSA id bz4sm19949383pbb.12.2014.01.30.11.07.53           对于           (version = TLSv1 cipher = ECDHE-RSA-RC4-SHA bits = 128/128);           星期四,2014年1月30日11:07:54 -0800(太平洋标准时间)日期:星期四,2014年1月30日11:07:54 -0800(太平洋标准时间)来自:反馈至:   “customer@abc.in”消息ID:   < 1121064448.5.1391108870969.JavaMail.javamailuser@localhost>学科:   CASTLE STREET反馈MIME版本:1.0内容类型:text / plain;   charset = us-ascii Content-Transfer-Encoding:7bit

我正在使用以下代码发送电子邮件,

 private void sendMail() {
        Session session = createSessionObject();

        try {

            Message[] message=new Message[EMAIL_TITLE.length];






            for(int i=0;i<EMAIL_TITLE.length;i++)

             {

            message[i] = createMessage(EMAIL_TITLE[i], Activity_login.BranchName+" Feedback", "Customer Feedback Received for dining\n\n\n" +
                    "Customer Feedback Received for home delivery\n\n\nOrder No : "+newbill+"\nCustomer Name : "+newname+"\nCustomer Mobile : "+mobile+"\nCall center exec. "+call_cntr+"\nDelivery Boy: "+del_boy+"\nBranch : "+Activity_login.BranchName+"\nFood : "+food+" Star\nAmbiance : "+ambiance+" Star\nService :"+service+" Star\nComments : "+comments.getText().toString(), session);

             }



            new SendMailTask(HDActivityAdmin.this).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) throws MessagingException, UnsupportedEncodingException {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(Activity_login.Email, "Feedback"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
        message.setSubject(subject);
        message.setText(messageBody);
        return message;
    }

    private Session createSessionObject() {
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");

        return Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("feedback@abc.in", "password");
            }
        });
    }


 private class SendMailTask extends AsyncTask<Message, Void, Void> {
        private ProgressDialog progressDialog;
        private ProgressDialog dialog;
        /** application context. */
        private Activity activity;
        private Context context;


        public SendMailTask(Activity activity) {
            this.activity = activity;

            context = activity;
            dialog = new ProgressDialog(context);
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

          dialog = new ProgressDialog(context);


           this.dialog.setMessage("Sending E-mails, Please wait...");
          this.dialog.show();



        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);


           if (dialog.isShowing()) {
                dialog.dismiss();
            }


           AlertDialog alertDialog = new AlertDialog.Builder(
                    HDActivityAdmin.this).create();

            alertDialog
                    .setMessage(Output);
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Add your code for the button here.

                            finish();
                        }
                    });
            alertDialog.show();




        }

        @Override
        protected Void doInBackground(Message... messages) {
            try {


                for(int i=0;i<messages.length;i++)
                {

                    Transport.send(messages[i]);

                }




            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

垃圾邮件检测并不关心您用来发送邮件的代码,它只关心邮件的内容。确保您的邮件看起来不像垃圾邮件。它是否有有效的From和To地址?看起来您的消息只有一个From地址&#34; Feedback&#34;。这很可能是问题所在。