我对 Java 不是很熟悉。我更新了现有 Java Web 应用程序中的一些功能。我的应用程序读取 .eml 文件并将附件保存在本地服务器上。现在当附件是一个味精时我有两个问题:
- 文件名为空
- 我没有正确阅读内容类型“message/rfc822”
这是我的代码:
import javax.mail.internet.*;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class MailUtility {
public static String[] displayEML(String emlID, String folder) throws Exception{
String[] mymessageArray = null;
String myyear = folder.substring(0, 4);
String mymonth = folder.substring(4, 6);
String myday = folder.substring(6, 8);
String folderpath = myyear + "/" + mymonth + "/" + myday;
//LINUX PATH
String mypath = "/opt/tomcat/webapps/MyWebApp/attachments/";
String mypathHref = "attachments\\";
String emlFile = "/opt/externalemails/" + folderpath + "/" + emlID + ".eml";
System.out.println("**** emlFile: " + emlFile);
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream(emlFile);
MimeMessage message = new MimeMessage(mailSession, source);
Address[] emlFrom = message.getFrom();
String email = emlFrom == null ? null : ((InternetAddress) emlFrom[0]).getAddress();
String priority = "Normal";
String emlBody = "";
String emlAttach = "";
if(message.getHeader("X-Priority", "")!=null) {
if(message.getHeader("X-Priority", "").startsWith("1")) {
priority = "<b><font color=\"red\">Highest</font></b>";
}
}
//view header info (Sender, Objbect, Data)
String myTable = "<table><font face=\"Courier New\" >";
myTable = myTable + "<tr><td><B>From:</B></td><td>" + email +"</td></tr>";
myTable = myTable + "<tr><td><B>Received Data:</B></td><td>" + message.getSentDate()+"</td></tr>";
myTable = myTable + "<tr><td><B>Priority:</B></td><td>" + priority +"</td></tr>";
myTable = myTable + "<tr><td><B>Subject:</B></td><td>" + message.getSubject() + "</td></tr>";
if (message.isMimeType("text/plain")) {
emlBody = ConvertTextPlanToHTML(message.getContent().toString());
} else if(message.isMimeType("text/html")) {
emlBody = message.getContent().toString();
} else if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
emlBody = getTextFromMimeMultipart(mimeMultipart);
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if(part.getFileName()!=null) {
String strAttachment = "";
//getfilename and remove invalid characters in the name
String myFileName = MimeUtility.decodeText(part.getFileName()).replaceAll("[\\\\/:*?\"<>|]", "");
String myPathSave = mypath + myFileName;
String myPathHref = mypathHref + myFileName;
try {
if (partCount==1) {
strAttachment = "<B>Attachment:</B>";
} else {
strAttachment = "";
}
part.saveFile(myPathSave);
emlAttach = emlAttach + "<tr><td>" + strAttachment + "</td><td><a href=\"" + myPathHref + "\" target=\"_blank\">"+ myFileName +"</a></td></tr>";
} catch (IOException exc) {
System.out.println(exc.toString());
}
}
}
}
myTable = myTable + emlAttach + "</font></table><br/>";
mymessageArray = new String[]{myTable,emlBody};
return mymessageArray;
}
//function to convert Text/Plan format in HTML
public static String ConvertTextPlanToHTML(String s) {
StringBuilder builder = new StringBuilder();
boolean previousWasASpace = false;
for( char c : s.toCharArray() ) {
if( c == ' ' ) {
if( previousWasASpace ) {
builder.append(" ");
previousWasASpace = false;
continue;
}
previousWasASpace = true;
} else {
previousWasASpace = false;
}
switch(c) {
case '<': builder.append("<"); break;
case '>': builder.append(">"); break;
case '&': builder.append("&"); break;
case '"': builder.append("""); break;
case '\n': builder.append("<br>"); break;
// case '*': builder.append("<b>"); break;
// We need Tab support here, because we print StackTraces as HTML
case '\t': builder.append(" "); break;
default:
if( c < 128 ) {
builder.append(c);
} else {
builder.append("&#").append((int)c).append(";");
}
}
}
return builder.toString();
}
// function to read email multipart
private static String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException{
String result = "";
String result2 = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
Object o = bodyPart.getContent();
if (o instanceof Multipart) {
Multipart mp = (Multipart)o;
for (int j = 0; j < mp.getCount(); j++) {
BodyPart b = mp.getBodyPart(j);
// Loop if the content type is multipart then get the content that is in that part,
// make it the new container and restart the loop in that part of the message.
if (b.getContentType().contains("multipart")) {
mp = (Multipart)b.getContent();
j = -1;
continue;
}
if(!b.getContentType().contains("text/html")) {
continue;
}
Object o2 = b.getContent();
if (o2 instanceof String) {
result = result + b.getContent();
}
}
}
if (bodyPart.isMimeType("text/plain")) {
result = ConvertTextPlanToHTML(bodyPart.getContent().toString());
} else if (bodyPart.isMimeType("text/html")) {
result = bodyPart.getContent().toString();
} else if (bodyPart.getContent() instanceof MimeMultipart){
result = getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
} else if (bodyPart.isMimeType("message/rfc822")){ //message/rfc822
/*
How can read bodyPart when is "message/rfc822"
*/
}
}
return result + result2;
}
}
你能帮我解决我的问题吗,有什么例子吗?谢谢你的帮助。
马可