如何使用java连接网易企业邮箱

    //获取邮箱会话
    @NotNull
    private static Session getMailSession() {
        Properties prop = System.getProperties();
        prop.put("mail.imap.host", "imaphz.qiye.163.com");

        prop.put("mail.imap.auth.plain.disable", "true");
        prop.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  //使用JSSE的SSL socketfactory来取代默认的socketfactory
        prop.put("mail.imap.socketFactory.fallback", "false");  // 只处理SSL的连接,对于非SSL的连接不做处理
        prop.put("mail.imap.port", 143);
        prop.put("mail.imap.socketFactory.port", 993);

        Session mailSession = Session.getInstance(prop, null);
        return mailSession;
    }
     Session mailSession = getMailSession(); 
     IMAPStore store = null;
     IMAPFolder folder = null;
       // 使用IMAP方式进行连接
      store = (IMAPStore) mailSession.getStore("imap");
      //user参数为邮箱信息***@.com,password为自己的激活码(需要再自己去网易企业邮箱进行认证和获取)
      store.connect(imapServer, user, password);
      // 拿到收件箱
      folder = (IMAPFolder) store.getFolder("INBOX");

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要抓取网易邮箱的邮件并设置时间范围,你可以使用JavaMail API来实现。下面是一个简单的示例代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class MailFetcher { public static void main(String[] args) throws Exception { String host = "pop3.163.com"; String username = "your_email@163.com"; String password = "your_password"; int port = 995; boolean useSSL = true; String fromDate = "2022-01-01"; // 要抓取的邮件时间范围起始日期 String toDate = "2022-01-31"; // 要抓取的邮件时间范围结束日期 // 设置连接属性 Properties props = new Properties(); props.put("mail.pop3.host", host); props.put("mail.pop3.port", port); props.put("mail.pop3.ssl.enable", useSSL); // 获取会话 Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); // 连接邮箱并打开Folder Store store = session.getStore("pop3"); store.connect(); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); // 设置时间范围 SearchTerm searchTerm = new SentDateTerm(ComparisonTerm.GE, new java.util.Date(fromDate)); searchTerm = new AndTerm(searchTerm, new SentDateTerm(ComparisonTerm.LE, new java.util.Date(toDate))); // 过滤邮件并打印 Message[] messages = folder.search(searchTerm); for (Message message : messages) { System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); System.out.println("Sent Date: " + message.getSentDate()); } // 关闭Folder和Store folder.close(false); store.close(); } } ``` 在代码中,我们使用JavaMail API连接网易邮箱的POP3服务器,然后打开收件箱Folder并设置时间范围。我们使用了`SentDateTerm`来设置时间范围,`ComparisonTerm.GE`表示大于等于,`ComparisonTerm.LE`表示小于等于,具体日期可以使用`java.util.Date`来表示。最后,我们过滤出符合时间范围的邮件并打印出来。 需要注意的是,如果你的邮箱开启了两步验证,你需要生成一个授权码来代替密码。另外,如果你的邮箱开启了IMAP服务而不是POP3服务,你需要将代码中的`pop3`改为`imap`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值