Java
Chilkat2 Java
Save Email Attachments to Filesystem
Saves email attachments to a directory.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
public class ChilkatExample {
public static void main(String[] args) {
Email email = new Email();
// Load an email object containing attachments.
// This .eml can be downloaded from:
// http://www.example-code.com/testData/HtmlEmail.eml
try {
email.loadEml("HtmlEmail.eml");
// If OverwriteExisting is turned on, files with the same
// name are overwritten. If turned off, new/unique filenames
// are automatically generated. The filenames actually saved
// are accessible via the GetAttachmentFilename method.
email.setOverwriteExisting(true);
// Save all attachments to the "myAttachments" subdirectory
// found under the calling process's current working directory.
// This directory is automatically created if it does not already
// exist.
email.saveAllAttachments("myAttachments");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// List the attachment filenames:
for (int i = 0; i < email.getNumAttachments(); i++) {
System.out.println(email.getAttachmentFilename(i));
}
}
}