java - Android - javax.mail.Authenticator 不再发送电子邮件?

标签 java android email

我在一个应用程序中使用 GMail 来发送带有附件的电子邮件。我的做法是,我的应用程序会自动撰写消息、附加附件并将其发送给收件人,而无需打开邮件应用程序。

该应用程序目前已经投入生产,就在今天早上,我发现我无法再发送电子邮件了。起初我以为设备没有互联网连接,或者密码已更改,但这两者都不是真的,我检查了 logcat,收到此错误:

com.sun.mail.smtp.SMTPSendFailedException: 553 5.1.2 The address specified is not a valid RFC-5321 address. si10sm4645040pab.15 - gsmtp

在研究该问题时,我发现 Google 在 10 月 31 日至 11 月 1 日之间发布了更新,它以某种方式影响了此问题(以及 Office Outlook、Thunderbird 和其他邮件应用程序)。然而,我不知道他们改变了什么(如果是端口)或其他什么。这是我的代码:

这是我正在使用的 Mail.java 类:

public class Mail extends javax.mail.Authenticator { 
    private String _user; 
    private String _pass; 

    private String[] _to; 
    private String _from; 

    private String _port; 
    private String _sport; 

    private String _host; 

    private String _subject; 
    private String _body; 

    private boolean _auth; 

    private boolean _debuggable; 

    private Multipart _multipart; 


    public Mail() { 
        _host = "smtp.gmail.com"; // default smtp server 
        _port = "465"; // 465 -- default smtp port
        _sport = "465"; // 465 -- default socketfactory port

        _user = ""; // username 
        _pass = ""; // password 
        _from = ""; // email sent from 
        _subject = ""; // email subject 
        _body = ""; // email body 

        _debuggable = false; // debug mode on or off - default off 
        _auth = true; // smtp authentication - default on 

        _multipart = new MimeMultipart(); 

        // There is something wrong with MailCap, javamail can not find a handler for the
        // multipart/mixed part, so this bit needs to be added.
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
        CommandMap.setDefaultCommandMap(mc); 
    } 

    public Mail(String user, String pass) { 
        this(); 

        _user = user; 
        _pass = pass; 
    } 

    public boolean send() throws Exception { 
        Properties props = _setProperties(); 

        if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("")
                && !_subject.equals("") && !_body.equals("")) {
            Session session = Session.getInstance(props, this); 

            MimeMessage msg = new MimeMessage(session); 

            msg.setFrom(new InternetAddress(_from)); 

            InternetAddress[] addressTo = new InternetAddress[_to.length]; 
            for (int i = 0; i < _to.length; i++) { 
                addressTo[i] = new InternetAddress(_to[i]); 
            } 
            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 

            msg.setSubject(_subject); 
            msg.setSentDate(new Date()); 

            // setup message body 
            BodyPart messageBodyPart = new MimeBodyPart(); 
            messageBodyPart.setText(_body); 
            _multipart.addBodyPart(messageBodyPart); 

            // Put parts in message 
            msg.setContent(_multipart); 

            // send email 
            Transport.send(msg); 

            return true; 
        } else { 
            return false; 
        } 
    } 

    public void addAttachment(String filename) throws Exception { 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        DataSource source = new FileDataSource(filename);        
        messageBodyPart.setDataHandler(new DataHandler(source));

        // Truncating the full file path to just filename
        Pattern p = Pattern.compile("[^/]*$");
        Matcher m = p.matcher(filename);

        if (m.find()){          
            messageBodyPart.setFileName(m.group());
        }
        else{ 
            messageBodyPart.setFileName(filename);
        }

        _multipart.addBodyPart(messageBodyPart);
    }


    @Override 
    public PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(_user, _pass); 
    } 

    private Properties _setProperties() { 
        Properties props = new Properties(); 

        props.put("mail.smtp.host", _host); 

        if(_debuggable) { 
            props.put("mail.debug", "true"); 
        } 

        if(_auth) { 
            props.put("mail.smtp.auth", "true"); 
        } 

        props.put("mail.smtp.port", _port); 
        props.put("mail.smtp.socketFactory.port", _sport); 
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
        props.put("mail.smtp.socketFactory.fallback", "false"); 

        return props; 
    } 

    // the getters and setters here
}

我在 Activity 中使用此类:

private boolean sendEmail(String attachmentFilePath, String subject){

    Mail m = new Mail("sender.email@gmail.com", "passwordHere");
    String[] toArr = {"destination@yahoo.com"};

    m.set_to(toArr); 

    //include time here
    m.set_from("sender");
    m.set_subject(subject);
    m.setBody("Please check the attached file.");

    try { 
        m.addAttachment(attachmentFilePath); 
        if(m.send()) { 

            File exportDir = new File(Environment.getExternalStorageDirectory().getPath() + "/csv", "");

            File original = new File(attachmentFilePath);

            String baseFilename = original.getName().toString().replace(".csv", "");
            File to = new File(exportDir, baseFilename +  " (sent).csv");
            original.renameTo(to);

        }
        else { 
            Toast.makeText(getBaseContext(), "Email was not sent.", Toast.LENGTH_LONG).show();
            return false;
        } 
    }
    catch(Exception e) { 
        //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
        Log.e("MailApp", "Could not send email", e);
        return false;
    } 
    return true;
}

但是,由于收到错误消息,我似乎无法发送电子邮件:

11-05 10:58:56.244    7672-7692/com.agict.marswin E/MailApp﹕ Could not send email
com.sun.mail.smtp.SMTPSendFailedException: 553 5.1.2 The address specified is not a valid RFC-5321 address. qc16sm4604799pab.47 - gsmtp

我不太确定这里发生了什么。端口似乎没有更改,并且我用来发送的电子邮件地址和要发送到的地址中没有任何错误或打印错误。

最佳答案

我也面临类似的问题。尝试将@gmail.com 添加到您的发件人电子邮件地址。为我工作。

关于java - Android - javax.mail.Authenticator 不再发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536004/

相关文章:

java - 使用 Spring Boot 请求包装体

java - 根据提供的关键字对文本进行排名

Android 主屏幕小部件文档

android - 为什么我收到无法打开调试器端口的错误 (127.0.0.1 :58061): java.net.SocketException "socket closed"

.NET 预定群发邮件开源解决方案

java - 如何在邮件服务器上测试安全 SMTP 邮件服务

java - 用Java有效解析Excel数据

java - 为什么Hashtable的load factor和CLRS书中描述的不一致?

Android Facebook SDK "Sessionless Request needs token but missing either application ID or client token"

python - 如何使用 python 2.7 发送电子邮件附件