首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用javamail实现对邮局365的IMAP访问

使用javamail实现对邮局365的IMAP访问
EN

Stack Overflow用户
提问于 2016-04-08 01:32:37
回答 2查看 16K关注 0票数 7

我正在尝试使用JavaMail 1.4API读取电子邮件帐户Office365文件夹中的电子邮件。

我的代码是:

代码语言:javascript
复制
    public static void main(String argv[]) throws Exception {
    // Get a Properties object
    Properties props = System.getProperties();

    // Get a Session object
    Session session = Session.getInstance(props, null);

    // Get a Store object
    Store store = null;
    Folder rf = null;

    if (CUR_MAIL_PROTOCOL != null)
        store = session.getStore("imap");

    // Connect
    if (CUR_MAIL_HOST != null || CUR_MAIL_LOGIN != null || CUR_MAIL_PWD != null)
        store.connect("outlook.office365.com", 993, "**MYEMAIL**", "**MYPASS**");
    else
        store.connect();

    // List namespace
    rf = store.getDefaultFolder();

    getFolderDetails(rf, true, "");
    if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
        Folder[] f = rf.list("%");
        for (int i = 0; i < f.length; i++)
            getFolderDetails(f[i], recursive, "    ");
    }

    store.close();
}

我的错误是:

代码语言:javascript
复制
    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:298)
    at javax.mail.Service.connect(Service.java:234)

谢谢

PS:使用JavaMail 1.4API的帐户Office365

EN

回答 2

Stack Overflow用户

发布于 2017-08-10 00:25:47

PFB代码此解决方案。

代码语言:javascript
复制
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;

public class OutLookReader_imaps {

    Folder inbox;

    // Constructor of the calss.

    public OutLookReader_imaps() {
        System.out.println("Inside MailReader()...");
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        // Set manual Properties
        props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.port", "993");
        props.setProperty("mail.imaps.socketFactory.port", "993");
        props.put("mail.imaps.host", "outlook.office365.com");


        try {
            /* Create the session and get the store for read the mail. */

            Session session = Session.getDefaultInstance(System.getProperties(), null);
            Store store = session.getStore("imaps");

            store.connect("outlook.office365.com", 993, "My email ID", "my password");

            /* Mention the folder name which you want to read. */


            inbox = store.getFolder("INBOX");

            /* Open the inbox using store. */

            //inbox.open(Folder.READ_ONLY);
            inbox.open(Folder.READ_WRITE);

            Message messages[] = inbox.search(new FlagTerm(new Flags(
                    Flags.Flag.ANSWERED), false));
            //Message[] msgs = inbox.getMessages();

            System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
            System.out.println("No. of Messages : " + inbox.getMessageCount());
            System.out.println("No. of Deleted Messages : " + inbox.getMode());

            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);

            inbox.fetch(messages, fp);

            try {

                printAllMessages(messages);

                inbox.close(true);
                store.close();

            } catch (Exception ex) {
                System.out.println("Exception arise at the time of read mail");
                ex.printStackTrace();
            }

        } catch (MessagingException e) {
            System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
            e.printStackTrace();
            System.exit(2);
        }

    }

    public void printAllMessages(Message[] msgs) throws Exception {
        for (int i = 0; i < msgs.length; i++) {
            System.out.println("MESSAGE #" + (i + 1) + ":");
            printEnvelope(msgs[i]);
        }
    }

    public void printEnvelope(Message message) throws Exception {

        Address[] a;

        if ((a = message.getFrom()) != null) {
            for (int j = 0; j < a.length; j++) {
                System.out.println("Email From : " + a[j].toString());
            }
        }

        String subject = message.getSubject();

        Date receivedDate = message.getReceivedDate();
        Date sentDate = message.getSentDate(); 

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        System.out.println("Email Subject : " + subject);

        if (receivedDate != null) {
            System.out.println("Received Date: " + df.format(receivedDate));
        }

        System.out.println("Sent Date : " + df.format(sentDate));
    }


    public static void main(String args[]) {
        new OutLookReader_imaps();
    }
}
票数 6
EN

Stack Overflow用户

发布于 2021-05-25 21:51:56

在我的例子中,缺少的是:

代码语言:javascript
复制
 props.setProperty("mail.imaps.sasl.mechanisms","XOAUTH2")

所有为我工作的道具(包括gmail和o365):

代码语言:javascript
复制
"mail.imaps.ssl.enable"         true
"mail.imaps.sasl.enable"        true
"mail.imaps.auth.login.disable" true
"mail.imaps.auth.plain.disable" true
"mail.imaps.auth.mechanisms"    "XOAUTH2"
"mail.imaps.usesocketchannels"  true
"mail.event.scope"              "session"
"mail.imaps.starttls.enable"    true
"mail.imaps.sasl.mechanisms"    "XOAUTH2"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36483341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档