おはようございます、Javamailを使用して添付ファイルを取得する
私は数日間このプロジェクトに取り組んでおり、中止になっています。添付ファイルをダウンロードすることは永遠に続くようで、ディスク行への書き込みファイルのように見えます。私は多くのオプション(FileChannel、一括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 = "[email protected]";
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;
}
}
Functions.java
package o365connect;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import static o365connect.AttDownload.store;
/**
*
* @author Oliver
*/
public class functions {
public static int TestUser(String pop3host, String Username, String Password) throws NoSuchProviderException, MessagingException {
try {
Session session = Session.getInstance(AttDownload.props);
store = session.getStore("imaps");
store.connect(pop3host, Username, Password);
return 0;
} catch (MessagingException e) {
System.out.println(e + "User Name Invalid");
return 1;
}
}
public static Folder TestFolder(Store store, String Path) throws MessagingException {
Folder emailFolder;
emailFolder = store.getFolder(Path);
emailFolder.open(Folder.READ_ONLY);
AttDownload.fTest = true;
emailFolder.close(false);
return emailFolder;
}
public static int MBSize(String pop3Host, String userName, String password, String Path) {
int Size = 0;
Session session = Session.getInstance(AttDownload.props);
try {
Store store = session.getStore("imaps");
store.connect(pop3Host, userName, password);
Folder emailFolder = store.getFolder(Path);
emailFolder.open(Folder.READ_ONLY);
Size = emailFolder.getMessageCount();
emailFolder.close(false);
store.close();
return Size;
} catch (MessagingException e) {
System.out.println(e + "MBSize");
}
return Size;
}
}
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();
}
}
編集:
props.put("mail.imaps.fetchsize", "819200");
props.put("mail.imaps.partialfetch", "false");
は7メガバイトをダウンロードするには12秒まで、128秒物事をスピードアップファイル。
partialfetchがfalseの場合、fetchsizeは使用されません。添付ファイル全体を1回のリクエストでダウンロードします。あなたが可能な限り大きな添付ファイルのための十分なメモリを持っている限り、それはいいです。それ以外の場合は、partialfetchをtrue(デフォルト)に設定し、余分なメモリを使用せずに妥当なパフォーマンスを得るのに十分な大きさのフェッチサイズを設定します。 –
あなたはこれを答えにすることができますか?私はこれらの2つの特性が存在するかどうかわからないので、私が見えなかったすべてを説明します。 – Charlie
完了。 JavaMailのプロパティに関するドキュメントはどこで探していましたか?あなたはjavadocsを見ましたが、プロパティを見つけられませんでしたか?あなたはjavadocsについて知りませんでしたか? [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html)を見ましたか? –