SMTP邮件代码在Android ICS中运行正常,但在Gingerbread中没有?

时间:2013-07-11 05:59:50

标签: java android smtp javamail smtp-auth

我已经编写了一个通过SMTP发送邮件的代码,邮件是从android ICS设备发送的,但是从Gingerbread设备发送的邮件没有发送。 任何人都可以通过代码帮助我......

首先我调用GMailOauthSender类的sendmail()方法。

class GMailOauthSender {

    private Session session;

    public SMTPTransport connectToSmtp(String host, int port,
            String userEmail, String oauthToken, boolean debug)
            throws Exception {

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "false");
        session = Session.getInstance(props);
        session.setDebug(debug);

        final URLName unusedUrlName = null;
        SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
        // If the password is non-null, SMTP tries to do AUTH LOGIN.
        final String emptyPassword = null;
        transport.connect(host, port, userEmail, emptyPassword);

        byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
                userEmail, oauthToken).getBytes();
        response = BASE64EncoderStream.encode(response);

        transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);

        return transport;
    }

    public synchronized void sendMail(String subject, String body,
            String user, String oauthToken, String recipients, String cc) {
        try {
            spinner = ProgressDialog.show(FeedbackActivity.this, "",
                    getResources().getString(R.string.sending_mail), false, true);
            new SendMailTask().execute(subject, body, user, oauthToken,
                    recipients, cc);

        } catch (Exception e) {
            Log.d("exception inside send mail method", e.toString());
        }

    }

    class SendMailTask extends AsyncTask<Object, Object, Object> {

        @Override
        protected Object doInBackground(Object... params) {
            String subject = (String) params[0];
            String body = (String) params[1];
            String user = (String) params[2];
            String oauthToken = (String) params[3];
            String recipients = (String) params[4];
            String cc = (String) params[5];
            try {
                SMTPTransport smtpTransport = connectToSmtp(
                        "smtp.gmail.com", 587, user, oauthToken, false);
                for(int i = 0; i< attachedFiles.size(); i++){
                    File file = new File(attachedFiles.get(i).getFilePath());
                    addAttachment(multipart, file);
                }
                MimeMessage message = new MimeMessage(session);
                message.setSender(new InternetAddress(user));
                message.setSubject(subject);

                MimeBodyPart mbp1 = new MimeBodyPart();
                mbp1.setText(body + "\nThanks\n"+user+"\n\n"+getResources().getString(R.string.signature_text));
                multipart.addBodyPart(mbp1);


                message.setContent(multipart);
                if (recipients.indexOf(',') > 0){
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(recipients));
                }else{
                    message.setRecipient(Message.RecipientType.TO,
                            new InternetAddress(recipients));
                }
                if(cc != null && cc.length() >0){
                    if (cc.indexOf(',') > 0){
                        message.addRecipients(Message.RecipientType.CC,
                                InternetAddress.parse(cc));
                    }else{
                        message.addRecipient(Message.RecipientType.CC,
                                new InternetAddress(cc));
                    }   

                }
                smtpTransport.sendMessage(message,
                        message.getAllRecipients());
                spinner.dismiss();
                finish();
                return new Object[] { smtpTransport, message };
            } catch (Exception e) {
                spinner.dismiss();
                finish();

            }
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);

            if(result != null){
            Toast.makeText(FeedbackActivity.this, getResources().getString(R.string.mail_sent), Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(FeedbackActivity.this, getResources().getString(R.string.mail_not_sent), Toast.LENGTH_LONG).show();  
            }
        }
    }
}

0 个答案:

没有答案