使用imap使用Java接收电子邮件会导致连接超时

时间:2016-05-24 10:03:40

标签: java imap

我正在尝试使用Java创建一个程序,该程序进入我的 { "name": "angular2-quickstart", "version": "1.0.0", "scripts": { "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ", "tsc": "tsc", "tsc:w": "tsc -w", "lite": "lite-server", "typings": "typings", "postinstall": "typings install" }, "license": "ISC", "dependencies": { "angular2": "2.0.0-beta.7", "bootstrap": "^3.3.6", "es6-promise": "^3.0.2", "es6-shim": "^0.33.3", "jquery": "^2.2.1", "moment": "^2.11.2", "primeng": "^0.5.0", "reflect-metadata": "0.1.2", "rxjs": "5.0.0-beta.2", "systemjs": "0.19.22", "zone.js": "0.5.15" }, "devDependencies": { "concurrently": "^2.0.0", "font-awesome": "^4.5.0", "gulp": "^3.9.1", "gulp-bless": "^3.0.1", "gulp-compass": "^2.1.0", "gulp-if": "^2.0.0", "gulp-imagemin": "^2.4.0", "gulp-livereload": "^3.8.1", "gulp-ng-annotate": "^2.0.0", "gulp-sass": "^2.2.0", "gulp-sourcemaps": "^1.6.0", "gulp-uglify": "^1.5.3", "gulp-uglifycss": "^1.0.6", "gulp-watch": "^4.3.5", "lite-server": "^2.1.0", "typescript": "^1.7.5", "typings": "^0.6.8" } } 帐户并打印发件人,主题和未读电子邮件的正文。

以下是代码:

gmail

以下是出现的错误,我不明白这些错误的原因。

任何人都知道如何解决它?

import java.util.*;
import javax.mail.*;

public class CheckingMails {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect("imap.gmail.com", "email@gmail.com", "password");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            Message msg = inbox.getMessage(inbox.getMessageCount());
            Address[] in = msg.getFrom();
            for (Address address : in) {
                System.out.println("FROM:" + address.toString());
            }
            Multipart mp = (Multipart) msg.getContent();
            BodyPart bp = mp.getBodyPart(0);
            System.out.println("SENT DATE:" + msg.getSentDate());
            System.out.println("SUBJECT:" + msg.getSubject());
            System.out.println("CONTENT:" + bp.getContent());
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

代码没有问题。从意大利语到英语的翻译错误是:connection timed out

尝试将以下属性添加到prop对象以覆盖默认属性:

props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");

如果错误仍然存​​在,您可以使用cmd行中的telnet imap.gmail.com {port number}命令检查端口连接。该错误可能是由防火墙或网络问题引起的。