使用Javamail获取附件
早上好 我已经在这个项目上工作了几天,现在已经停了下来。下载附件似乎要花很长时间,而且似乎是在将文件写入磁盘时。我已经阅读了很多选项(FileChannel、bulk getContent和其他一些选项,但无法使此代码以合理的速度执行)我不确定唯一的瓶颈是否是从O365下载文件,但是我想我会问一个问题,看看是否有人可以查看此代码,并希望告诉我我做错了什么。该应用程序的目标是登录Exchange Online(o365)并下载特定邮箱中的所有附件。请注意,这段代码已经修改了很多次,以查看我是否可以通过使用线程等提高性能: 正如我所说,我已经改变了很多东西,试图使这项工作更好,所以请不要因为一些代码没有太多意义而批评我。我不是想让其他人完成这个项目,我正在寻找一种我没有太多经验的语言的指导使用Javamail获取附件,java,jakarta-mail,imap,Java,Jakarta Mail,Imap,早上好 我已经在这个项目上工作了几天,现在已经停了下来。下载附件似乎要花很长时间,而且似乎是在将文件写入磁盘时。我已经阅读了很多选项(FileChannel、bulk getContent和其他一些选项,但无法使此代码以合理的速度执行)我不确定唯一的瓶颈是否是从O365下载文件,但是我想我会问一个问题,看看是否有人可以查看此代码,并希望告诉我我做错了什么。该应用程序的目标是登录Exchange Online(o365)并下载特定邮箱中的所有附件。请注意,这段代码已经修改了很多次,以查看我是否可以
package o365connect;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
/**
*
* @author Charlie
*/
public class AttDownload extends Thread {
public static int Lower = 0;
public static int Upper = 0;
public static int Counter = 0;
public static Session session;
public static Store store;
public static Properties props = new Properties();
public static boolean fTest = false;
AttDownload(int i, int ii) {
Lower = i;
Upper = ii;
}
AttDownload() throws UnknownHostException {
super();
}
@Override
public void run() {
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String pop3Host = "outlook.office365.com";
String mailStoreType = "imap";
String path = "Inbox/Scans to file";
String userName = "XXX@XXX.com";
String password = "XXXXXXX";
Folder emailFolder;
try {
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");
session = Session.getInstance(props);
int Size = functions.MBSize(pop3Host, userName, password, path);
System.out.println(Size);
store = session.getStore("imaps");
store.connect(pop3Host, userName, password);
emailFolder = store.getFolder(path);
emailFolder.open(Folder.READ_ONLY);
try {
Message[] messages;
messages = emailFolder.getMessages(Lower, Upper);
System.out.println("starting thread for - " + Lower + " - " + Upper);
int ASuc = receiveEmail(messages);
} catch (MessagingException | IOException ex) {
Logger.getLogger(AttDownload.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (NoSuchProviderException ex) {
Logger.getLogger(AttDownload.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
Logger.getLogger(AttDownload.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static int receiveEmail(Message messagesarr[]) throws IOException, MessagingException {
for (Message messagesarr1 : messagesarr) {
try {
Message message = messagesarr1;
Object content = message.getContent();
if (content instanceof String) {
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) message.getContent();
for (int k = 0; k < multipart.getCount(); k++) {
MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(k);
if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
long startTime = System.currentTimeMillis();
int ran = (int) startTime;
String fileName;
String fName = bodyPart.getFileName();
if (fName != null && !fName.isEmpty()) {
fileName = fName.replaceAll("\\s+", "");
} else {
continue;
}
if ("ATT00001.txt".equals(fileName)) {
continue;
} else {
System.out.println("starting copy of - " + fileName);
}
String destFilePath = "D:/Scans/";
bodyPart.saveFile(destFilePath + bodyPart.getFileName());
long stopTime = System.currentTimeMillis();
System.out.println("finished copying of - " + fileName + " - " + (stopTime - startTime) + " miliseconds.");
System.out.println(Counter);
Counter++;
} else {
}
}
}
}catch (MessagingException e) {
System.out.println(e);
}
}
return 1;
}
}
O365Connect.java
package o365connect;
import java.io.IOException;
import javax.mail.MessagingException;
public class O365Connect {
public static void main(String[] args) throws IOException, MessagingException, InterruptedException {
MainScreen ms = new MainScreen();
ms.setVisible(true);
AttDownload dl = new AttDownload(1, 1000);
dl.start();
}
}
编辑:
加快速度,下载7mb文件的时间从128秒缩短到12秒。如果partialfetch为false,则不使用fetchsize;它将在一个请求中下载整个附件。只要你有足够的内存来存储尽可能大的附件,就可以了。否则,将partialfetch设置为true(默认),并将fetchsize设置为足够大,以便在不使用过多内存的情况下提供合理的性能
JavaMail属性在每个协议提供者页面的javadocs中描述。例如,上描述了IMAP提供程序属性。如果partialfetch为false,则不使用fetchsize;它将在一个请求中下载整个附件。只要你有足够的内存来存储尽可能大的附件,就可以了。否则,请将partialfetch设置为true(默认),并将fetchsize设置为足够大的大小,以便在不使用过多内存的情况下提供合理的性能。您能否将其作为一个答案,因为这解释了我似乎找不到的一切,因为我不知道这两个属性存在。完成。您在哪里查找有关JavaMail属性的文档?您是否查看了javadocs,但没有找到属性?你甚至不知道javadocs吗?你看了吗?我看了这么多的文件,但我想我找错地方了(我挂断的地方)。我在追踪文件写入幽灵。我正在努力让这些信息更容易找到,所以如果你有任何建议,请告诉我。
package o365connect;
import java.io.IOException;
import javax.mail.MessagingException;
public class O365Connect {
public static void main(String[] args) throws IOException, MessagingException, InterruptedException {
MainScreen ms = new MainScreen();
ms.setVisible(true);
AttDownload dl = new AttDownload(1, 1000);
dl.start();
}
}
props.put("mail.imaps.fetchsize", "819200");
props.put("mail.imaps.partialfetch", "false");