Android JavaMail 使用 Exchange 服务器发送邮件

Android JavaMail send mail with Exchange server

我正在尝试使用我公司的服务器发送电子邮件,但有些东西不起作用。

private Properties _setProperties() {


    _port = 443;
    _sport = 443;
    Properties props = (Properties) System.getProperties().clone();

    props.put("mail.smtps.host", _host);
    props.put("mails.debug", "true");
    props.put("mail.smtps.auth", "true");

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

    return props;
}

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

    _user = "email";
    _pass = "pass";

    Session session = Session.getInstance(props, this);
    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(_user));
    msg.setRecipients(MimeMessage.RecipientType.TO, "mailTO");
    msg.setSubject("SUBJECT");
    msg.setSentDate(new Date());
    msg.setText("TEXT");

    // send email
    Transport transport = session.getTransport("smtps");
    transport.connect();
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

    return true;

}

调试显示如下:

DEBUG: setDebug: JavaMail version 1.5.5

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]

DEBUG SMTP: need username and password for authentication

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "companyServer", port 443, isSSL true

一直在尝试连接,但没有给出任何答案。当我设置 mail.smtps.writetimeout 它 returns 我 java.net.SocketException: 套接字关闭

有什么想法吗? :S

请注意,所有 Outlook 客户端都使用 MAPI 或 MAPI-over-http(MAC 使用 EWS)。因此,大多数 Exchange 管理员可能不允许用户使用 SMTP,因此它可能无法正常工作。

如果您可以通过那种方式使用 SMTP,您是否咨询过您的 MS Exchange 管理员?