【问题标题】:Java to *get* mail from SMTP sevrerJava 从 SMTP 服务器 *get* 邮件
【发布时间】:2020-02-19 07:56:01
【问题描述】:

我正在尝试从 Exchange SMTP 服务器(端口 25)获取一封电子邮件。 我看到的所有示例都是使用 SMTP 发送电子邮件,而我想获取(阅读)电子邮件。 我使用 JAVAMail 编写了一个代码,该代码使用 impas 接收电子邮件,效果很好,但在最后一刻,需求更改为使用 SMTP 接收邮件。

IMAP 传入邮件的 Java 代码

 public void getAttachment() throws MessagingException, IOException {
        properties.setProperty("exchange server host",host);
        properties.put("smtp.gmail.auth", "true");
        Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
                }
                }
        );

        Store store = session.getStore("imaps");
        try {
            logger.info(String.format("Going to get connection to exchange server %s for user %s " ,host, user));
            store.connect(host, user, password);
        }
        catch (MessagingException ex){
            logger.error(String.format("Unable to connect exchange server {}", host) + ex.getMessage());
            logger.error(ex.getStackTrace());
        }
        Folder inboxFolder = store.getFolder("inbox");
        inboxFolder.open(Folder.READ_WRITE);
        // search for all "unseen" messages
        Flags seen = new Flags(Flags.Flag.SEEN);
        FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
        Message[] message = inboxFolder.search(unseenFlagTerm);

有人可以建议使用 SMTP 协议而不是 IMAP 获取邮件吗? 可以吗?

谢谢。

【问题讨论】:

    标签: java smtp jakarta-mail exchange-server


    【解决方案1】:

    你不能。

    SMTP 供客户端发送电子邮件到服务器。

    为了让客户端接收来自服务器的电子邮件,您需要使用诸如POP3(邮局协议)或IMAP(互联网消息访问协议)之类的协议。

    实际上,SMTP 的 Wikipedia 页面也说了这么多:

    用户级email clients 通常仅使用 SMTP 将消息发送到邮件服务器进行中继,通常按照RFC 8314 在端口 587 或 465 上将外发电子邮件提交到邮件服务器。对于检索消息,IMAPPOP3 是标准的,但专有服务器也经常实现专有协议,例如 Exchange ActiveSync

    【讨论】:

    • 好的,谢谢。但实际上,exchnage 服务器配置为对传入和传出的电子邮件都使用 SMTP。怎么可能?
    • @Asfbar 因为除了一些专有协议之外,Microsoft Exchange Server 还实现了所有这些协议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2014-11-15
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多