无法使用 javamail 阅读 outlook 电子邮件

unable to read outlook emails with javamail

我正在编写一个程序来读取来自 outlook 的电子邮件。

我写了下面的程序来发送电子邮件。

package com.getEmails;

import java.util.Properties;
import javax.mail.*;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailExample {
    public static void main(String args[]) {

        String to = "xyz@abc.com";
        String from = "ubv@abc.com";
        String host = "myhost";

        try {
            // Get system properties
            Properties props = System.getProperties();

            Authenticator authenticator = new Authenticator();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            // props.put("mail.smtp.auth.mechanisms","NTLM");
            props.put("mail.smtp.submitter", authenticator
                    .getPasswordAuthentication().getUserName());
            // Get session
            Session session = Session.getInstance(props, authenticator);

            session.setDebug(true);

            // Define message
            MimeMessage message = new MimeMessage(session);

            // Set the from address
            message.setFrom(new InternetAddress(from));

            // Set the to address
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    to));

            // Set the subject
            message.setSubject("JavaMail Test");

            // Set the content
            message.setText("Test email through Java Mail");
            // Send message
            Transport.send(message);
            System.out.println("OK Man");
        }

        catch (MessagingException e) {
            e.toString();
        } catch (Exception mex) {
            System.out.print(mex);
        }
    }

    static class Authenticator extends javax.mail.Authenticator {
        private PasswordAuthentication authentication;

        public Authenticator() {
            String username = "";
            String password = "";
            authentication = new PasswordAuthentication(username, password);
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }
    }
}

这里我给 host 作为 myhost,我很抱歉,因为这是我的组织主机地址。

我正在尝试使用以下程序阅读收件箱中的内容。

package com.getEmails;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class readEmails {

    public static void main(String[] args) throws Exception {
        final String userName = "xyz@abc.com";
        final String password = "password";
        final String host = "myhost";
        // properties
        Properties props = System.getProperties();

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, password);
                    }
                });
        session.setDebug(true);

        // Store object
        Store store = session.getStore("imaps");
        store.connect(host, userName, password);

        // Get folder
        Folder folder = store.getFolder("Drafts");
        folder.open(Folder.READ_ONLY);
        Message messages[] = folder.getMessages();
        for (Message message : messages) {
            System.out.println(message.getSubject());
        }
    }

}

这里当我 运行 上述程序时,我得到以下输出。

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myhost", port 993, isSSL true
Exception in thread "main" javax.mail.MessagingException: Connection timed out: connect;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at com.getEmails.readEmails.main(readEmails.java:30)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
    ... 3 more

在这里,即使我将 imaps 更改为 imap,我也会遇到同样的错误。

我还想告诉大家,我使用相同的 host 来阅读和撰写电子邮件。

请告诉我如何解决此问题并获取我的电子邮件。我正在使用 JavaMail(我只能使用这个)。

来自 "outlook" 我假设您正在连接到您组织中的 Microsoft Exchange 邮件服务器。

Exchange 通常对 Outlook 邮件使用 Microsoft 专有协议 reader。除非在您的 Exchange 服务器上启用了 IMAP 支持,否则您将无法使用 JavaMail 进行连接。请与您的服务器管理员联系以查看是否已启用 IMAP。 (启用 IMAP 进行读取与启用 SMTP 进行发送是分开的。)

此外,您使用的是非常旧的 JavaMail 版本。你应该考虑 upgrading to the current version.