为什么我用javamail写的程序,总是收不到邮件的正文内容?
我看了很长时间,看不出哪里错。但是getmailcontent 函数就是返回不了邮件正文的内容,请高手看看。先谢谢了。
import javax.mail.*;
import java.util.Properties;
import java.io.*;
import javax.mail.internet.*;
public class email{
static Store s;
static Folder f;
public static Message[] getmessage()throws Exception{
.....................
}
public static String getmailfrom(Message m) throws Exception{
...........................
}
public static String getmailcontent(Message m) throws Exception{
Part messagepart=m;
Object content = messagepart.getContent();
String contentstring="";
if(content instanceof Multipart){
messagepart = ((Multipart)content).getBodyPart(0);
String contentType = messagepart.getContentType();
if(contentType.startsWith("text/plain") || contentType.startsWith("text/html")) {
InputStream is = messagepart.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String thisLine = reader.readLine();
while(thisLine != null)
{
contentstring=contentstring + reader.readLine();
}
else if(content instanceof String){
contentstring=content.toString();
}
}
return "Content: "+contentstring;
}
public static void main(String[] args){
try{
Message[] message=email.getmessage();
System.out.println(email.getmailfrom(message[1]));
System.out.println(email.getmaildate(message[1]));
System.out.println(email.getmailsubject(message[1]));
System.out.println(email.getmailcontent(message[1]));
}
catch (Exception e) {
e.printStackTrace();
}
}
}