Java Mail缓慢下载附件Office 365

Chr*_*dor 3 java jakarta-mail

我正在阅读Office 365邮件帐户的收件箱,并下载它的附件(txt文件)。对于小于2 MB的小文件,它可以正常工作,但是当我尝试使用20 MB的文件时,它确实很慢。我在两个不同的机器(Linux和Windows)上测试了代码,速度是一样的,真​​的很慢。

String user = "mail@mail.com";
String password = "mypassword";
try{

    boolean foundSites = false;

    // Get a Properties object
    Properties properties = new Properties();

    properties.put("mail.imaps.host", host);
    properties.put("mail.imaps.port", port);
    properties.put("mail.imaps.starttls.enable", SSL);
    //properties.put("mail.imap.fetchsize", "1000000");
    Session emailSession = Session.getDefaultInstance(properties);

    //create the POP3 store object and connect with the pop server
    Store store = emailSession.getStore("imaps");
    store.connect(host, user, password);

    //create the folder object and open it
    Folder emailFolder = store.getFolder("INBOX");
    emailFolder.open(Folder.READ_ONLY);

    // retrieve the messages from the folder in an array and print it
    Message[] messages = emailFolder.getMessages();
    System.out.println("messages.length---" + messages.length);

    LocalDateTime now = LocalDateTime.now();
    int year = now.getYear();
    int month = now.getMonthValue();
    int day = now.getDayOfMonth();

    for (int i = messages.length - 1; i >= 0; i--) {
    Message message = messages[i];


    Calendar cal = Calendar.getInstance();
    cal.setTime(message.getReceivedDate());
    int yearMessage = cal.get(Calendar.YEAR);
    int monthMessage = cal.get(Calendar.MONTH) + 1;
    int dayMessage = cal.get(Calendar.DAY_OF_MONTH);



    if(year == yearMessage && month == monthMessage && day == dayMessage)
    {
        //The real code is doing this with 20 Subjects (20 emails)
        if(message.getSubject().equalsIgnoreCase("Integration - Site Info") && foundSites != true)
        {
            System.out.println("found Integration - Site Info");

            Multipart multipart = (Multipart) message.getContent();
            List<File> attachments = new ArrayList<>();
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
                {
                    InputStream is = bodyPart.getInputStream();
                    File f = new File("./attachments/"
                            + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv");
                    FileOutputStream fos = new FileOutputStream(f);
                    byte[] buf = new byte[4096];
                    int bytesRead;
                    while((bytesRead = is.read(buf))!=-1) {
                        fos.write(buf, 0, bytesRead);
                    }
                    fos.close();
                    attachments.add(f);
                    foundSites = true;
                    break;
                }


            }
        }
    }

    if(foundSites)
    {
        break;
    }
} catch (Exception e) {
    System.out.println(e);  
}
Run Code Online (Sandbox Code Playgroud)

我可以创建线程,但是还有其他选择吗?

附带说明:我尝试使用python代码,速度显着提高。

=====================================更新1:

我对saveFile方法进行了更改,代码简化了很多,但仍然保持每秒10 KB的下载速度。

MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(j);
if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))
{
    bodyPart.saveFile("./attachments/"
        + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv");
Run Code Online (Sandbox Code Playgroud)

我还使用了探查器,结果是: 在此处输入图片说明

在此处输入图片说明

Bil*_*non 5

解决这些常见错误

通过使用MimeBodyPart.saveFile保存附件来简化代码。

由于您使用的是“ imaps”协议而不是“ imap”协议,因此请将mail.imaps.fetchsize属性设置为足够大的值,以在不使用过多内存的情况下获得所需的性能。或设置mail.imaps.partialfetch为false,如果您不在乎使用多少内存并且确定将始终有足够的内存。