0
メールから添付ファイルをダウンロードする次のコードを書いています。問題はそれが新しいメッセージのためだけに働いていて、メッセージを読んだ後に条件を置くことができないということです。フラグを使用しようとしましたが、役に立つことはありません。ダウンロード特定の件名のJavaメールを使用してGmailからすべての添付ファイルをダウンロード
if(message.getSubject().equals("Test")
Nullポインタ例外がスローされます。 同じもののコード。
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
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;
import javax.mail.search.FlagTerm;
public class EmailAttachmentReceiver {
/** log4j Logger */
private static String saveDirectory = "E://Movies//";
/**
* Sets the directory where attached files will be stored.
* @param dir absolute path of the directory
*/
public void setSaveDirectory(String dir) {
EmailAttachmentReceiver.saveDirectory = dir;
}
public static void downloadEmailAttachments(String host, String port, String userName, String password) {
Properties properties = new Properties();
// server setting
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
// SSL setting
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port));
Session session = Session.getDefaultInstance(properties);
try {
// connects to the message store
Store store = session.getStore("pop3");
store.connect(userName, password);
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
// fetches new messages from server
//Message[] arrayMessages = folderInbox.getMessages();
Message[] arrayMessages = folderInbox.search(new FlagTerm(new Flags(Flag.SEEN), true));
// FetchProfile fp = new FetchProfile();
// fp.add(FetchProfile.Item.ENVELOPE);
// fp.add(FetchProfile.Item.CONTENT_INFO);
// fp.add("X-mailer");
// folderInbox.fetch(arrayMessages, fp);
System.out.println("Here" + arrayMessages.length);
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
System.out.println(message.getSubject());
//if(message.getSubject().equalsIgnoreCase("Test")){
String contentType = message.getContentType();
String messageContent = "";
// store attachment file name, separated by comma
String attachFiles = "";
if (contentType.contains("multipart")) {
System.out.println("Inside m");
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
System.out.println("Inside Attachment");
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
}
//}
// disconnect
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for pop3.");
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
String host = "pop.gmail.com";
String port = "995";
String userName = "[email protected]"; //username for the mail you want to read
String password = "XXXXX"; //password
String saveDirectory = "E://Movies//";
EmailAttachmentReceiver receiver = new EmailAttachmentReceiver();
receiver.setSaveDirectory(saveDirectory);
EmailAttachmentReceiver.downloadEmailAttachments(host, port, userName, password);
}
}