我有一个EML文件,其中包含另一个EML文件作为附件。
当我尝试使用JAVAMAIL API获取附件文件时,getDisposition值表示附件,但getfileName()
显示NULL
。
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream("C:\\Mail1496085.eml");
MimeMessage message = new MimeMessage(mailSession, source);
System.out.println("Subject : " + message.getSubject());
System.out.println("From : " + message.getFrom()[0]);
System.out.println("--------------");
System.out.println("Body : " + message.getContent());
String contentType = message.getContentType();
if (contentType.contains("multipart")) {
System.out.println("Multipart EMail File");
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
System.out.println("Parts:::"+numberOfParts);
String wi="IM-67890-PROCESS";
String saveDir = System.getProperty("user.dir")+"\\Docs";
saveDir=saveDir + File.separator+wi;
boolean file =new File(saveDir).mkdir();
if (file) {
System.out.println("Directory: " + wi + " created");
// logger.debug("Directory: " + workItem + " created");
}
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
String disposition=part.getDisposition();
if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
// this part is attachment
String fileName = part.getFileName();
String extension="";
System.out.println("Attached File Name::"+fileName);
saveDir=saveDir + File.separator + fileName;
int i=fileName.lastIndexOf(".");
if(i>0)
{
extension=fileName.substring(i+1);
}
if(extension.equalsIgnoreCase("eml"))
{
part.saveFile(saveDir);
extractEML(saveDir, wi);
System.out.println("This is a eml file");
}
else if(extension.equalsIgnoreCase("msg"))
{
part.saveFile(saveDir);
extractMSG(saveDir,wi);
System.out.println("This is a msg file");
}
else
{
System.out.println("This is other file");
}
} else {
System.out.println("Not an eml file");
System.out.println("File Name::"+part.getFileName());
}
}
}
}
public static void extractEML(String emlPath,String wi) throws MessagingException, IOException
{
//String fileName="";
String path=System.getProperty("user.dir") + File.separator + "Docs" + File.separator + wi + File.separator + "EML_PDF";
boolean file =new File(path).mkdir();
if(file)
{
System.out.println("Folder EML_PDF Created Successfully");
}
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream(emlPath);
MimeMessage message = new MimeMessage(mailSession, source);
System.out.println("Subject : " + message.getSubject());
System.out.println("From : " + message.getFrom()[0]);
System.out.println("--------------");
System.out.println("Body : " + message.getContent());
String contentType = message.getContentType();
if (contentType.contains("multipart")) {
System.out.println("Multipart EMail File");
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
System.out.println("Parts:::"+numberOfParts);
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
String fileName = part.getFileName();
String extension="";
path=path + File.separator + fileName;
int i=fileName.lastIndexOf(".");
if(i>0)
{
extension=fileName.substring(i+1);
}
if(extension.equalsIgnoreCase("pdf"))
{
part.saveFile(path);
}
}
}
}
}
答案 0 :(得分:1)
附件的文件名是可选的。您需要修复程序才能在没有文件名的情况下工作。
答案 1 :(得分:0)
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
InputStream inputStream = new FileInputStream(FilePath);
MimeMessage message = new MimeMessage(mailSession, inputStream);
MimeMessageParser mimeParser = new MimeMessageParser(message);
mimeParser.parse();
List<DataSource> dsl = mimeParser.getAttachmentList();
for(DataSource ds : dsl) {
InputStream file = ds.getInputStream();
}