【问题标题】:Sending Mail: Authentication Failure发送邮件:认证失败
【发布时间】:2014-08-31 04:16:23
【问题描述】:

我正在尝试从应用程序发送电子邮件,我使用以下代码:

private static final String username = "sth@gmail.com";
private static final String password = "pass";


private void sendMail(String email, String subject, String messageBody) {
        Session session = createSessionObject();

        try {
            Message message = createMessage(email, subject, messageBody, session);
            new SendMailTask().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("sth@gmail.com"));
        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(username, password);
            }
        });
    }

    private class SendMailTask extends AsyncTask<Message, Void, Void> {
        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(SendQuestion.this, "Please wait", "Sending mail", true, false);
        }

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

        @Override
        protected Void doInBackground(Message... messages) {
            try {
                Transport.send(messages[0]);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Eclipse 抛出 javax.mail.AuthenticationFailedException。当我第一次尝试发送邮件时,我的 gmail 帐户收到了来自 Google 的关于安全性的邮件,并且他们阻止了该邮件。然后我在 /lesssecureapps 激活我想允许不太安全的应用程序。但这并没有解决它。是我的代码有问题还是 gmail 问题?

PS:我已经添加了所有必要的 .jar 并包含了对清单的 Internet 访问权限

【问题讨论】:

    标签: java android email


    【解决方案1】:

    尝试这样做:

    public static int Email(String fromemail, String toemail, String cc,
                String bcc, String Subject, String Body, String Attachment) {
            final String fromEmail = fromemail;// "user@gmail.com"; //requires
            // valid gmail id
            // final String password = Password; // correct password for gmail id
            final String toEmail = toemail;// "user2@ymail.com"; // can be any
            // email id
    
            MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }
            sf.setTrustAllHosts(true);
    
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com"); // SMTP Host
            props.put("mail.smtp.socketFactory.port", "465"); // SSL Port
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory"); // SSL Factory Class
            props.put("mail.smtp.auth", "true"); // Enabling SMTP Authentication
            props.put("mail.smtp.port", "465"); // SMTP Port
    
            Authenticator auth = new Authenticator() {
                // override the getPasswordAuthentication method
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, Password);
                }
            };
    
            Session session = Session.getDefaultInstance(props, auth);
    
            int i;
            i = sendEmail(session, toEmail, Subject, Body);
            return i;
    
        }
    

    这是发送函数:

    public static void sendEmail(Session session, String toEmail,
                String subject, String body) {
            try {
                MimeMessage msg = new MimeMessage(session);
                // set message headers
                msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
                msg.addHeader("format", "flowed");
                msg.addHeader("Content-Transfer-Encoding", "8bit");
    
                msg.setFrom(new InternetAddress("no_reply@journaldev.com",
                        "Operation Department Email"));
    
                msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com",
                        false));
    
                msg.setSubject(subject, "UTF-8");
    
                msg.setText(body, "UTF-8");
    
                msg.setSentDate(new Date());
    
                msg.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(toEmail, false));
                Transport.send(msg);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 感谢您的回答。两件事:1)i = sendEmail(会话,toEmail,主题,正文); sendEmail 是无效的,所以这是错误 2)对于 MailSSLSocketFactory,我应该创建新类吗?
    【解决方案2】:

    事实证明,这是谷歌服务器的问题。我与支持人员联系,他们修复了它。上面的代码运行良好。

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2015-02-05
      • 2016-04-11
      • 2014-11-18
      • 2014-08-09
      • 2015-08-27
      • 2012-05-24
      • 2016-02-15
      • 1970-01-01
      相关资源
      最近更新 更多