简体   繁体   English

无法使用JavaMail读取Outlook电子邮件

[英]unable to read outlook emails with javamail

I'm writing a program to read emails from outlook. 我正在编写一个程序来读取来自Outlook的电子邮件。

I wrote the below program to send email. 我写了下面的程序来发送电子邮件。

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;
        }
    }
}

Here i've given host as myhost , i'm sorry for that, since it is my organizations host address. 在这里,我已将host指定为myhost ,对此感到抱歉,因为它是我的组织的主机地址。

And i'm trying to read the contents of my inbox using the below program. 我正在尝试使用以下程序阅读收件箱中的内容。

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());
        }
    }

}

here when i run the above program i'm getting the below output. 在这里,当我运行上面的程序时,我得到下面的输出。

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

Here even if i change imaps to imap , i'm getting the same error. 即使在这里将imaps更改为imap ,我imaps遇到相同的错误。

And also i want to tell that I used the same host for both reading and writing emails. 我也想告诉我,我使用同一host读取和写入电子邮件。

please let me know how can i fix this and get my emails. 请让我知道如何解决此问题并获取电子邮件。 I'm using JavaMail(I've to use only this). 我正在使用JavaMail(我只能使用它)。

By "outlook" I assume you're connecting to a Microsoft Exchange mail server in your organization. 通过“外表”,我假设您正在连接到组织中的Microsoft Exchange邮件服务器。

Exchange normally uses a Microsoft proprietary protocol with the Outlook mail reader. Exchange通常将Microsoft专有协议与Outlook邮件阅读器一起使用。 Unless IMAP support has been enabled on your Exchange server, you won't be able to connect using JavaMail. 除非您的Exchange服务器上启用了IMAP支持,否则您将无法使用JavaMail进行连接。 Check with your server administrator to see if IMAP has been enabled. 请与您的服务器管理员联系,以查看是否已启用IMAP。 (Enabling IMAP for reading is separate from enabling SMTP for sending.) (启用IMAP进行读取与启用SMTP进行发送是分开的。)

Also, you're using a very old version of JavaMail. 另外,您使用的JavaMail版本非常旧。 You should consider upgrading to the current version . 您应该考虑升级到当前版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM