我开发了一个简单的Java应用程序,它可以读取我的Gmail收件箱邮件。我能够阅读电子邮件发件人和主题。但是,我无法阅读电子邮件内容。
当我尝试阅读它时,我得到以下异常:
Exception Msg: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.Multipart
法典:
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
public class Read_Mail {
static String from;
public static void main(String args[])
{
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props,null);
IMAPStore imapstore = null;
try
{
imapstore = (IMAPStore) session.getStore("imaps");
imapstore.connect("imap.gmail.com", "usernamexxxxx@gmail.com", "passwordxxx");
final IMAPFolder folder = (IMAPFolder) imapstore.getFolder("Inbox");
folder.open(IMAPFolder.READ_ONLY);
Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("==============================");
System.out.println("Email #" + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
// System.out.println("Text: " + message.getContent());
Object mp = (Object) message.getContent();
if (mp instanceof String)
{
String body = (String)mp;
System.out.println("MSG Body : " + body);
}
else if (mp instanceof Multipart)
{
Multipart mpp = (Multipart)mp;
final BodyPart bp = mpp.getBodyPart(i);
System.out.println("Text: " +bp.getContent().toString());
} else {
System.out.println("Inside else");
Multipart mpp = (Multipart)mp;
final BodyPart bp = mpp.getBodyPart(i);
System.out.println("Text: " +bp.getContent().toString());
}
}
}
catch(Exception e)
{
System.out.println("Exception Msg: " + e.getMessage());
}
}
}
它总是进入else
块并触发异常。
我对你的代码进行了一些更改,希望有帮助
try {
try {
imapstore = (IMAPStore) session.getStore("imaps");
} catch (NoSuchProviderException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
imapstore.connect("imap.gmail.com", "usernamexxxxx@gmail.com", "passwordxxx");
IMAPFolder folder;
folder = (IMAPFolder) imapstore.getFolder("Inbox");
folder.open(IMAPFolder.READ_ONLY);
Message[] messages;
messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("==============================");
System.out.println("Email #" + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
// System.out.println("Text: " + message.getContent());
Object mp;
try{
mp = (Object) message.getContent();
if (mp instanceof String) {
String body = (String)mp;
System.out.println("MSG Body : " + body);
} else if (mp instanceof MimeMultipart) {
MimeMultipart mpp = (MimeMultipart)mp;
for(int count =0;count<mpp.getCount();count++){
MimeBodyPart bp = (MimeBodyPart)mpp.getBodyPart(count);
InputStream fileNme = bp.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(fileNme, writer, "UTF-8");
String theString = writer.toString();
System.out.println("Text: " +theString);
}
} else if (mp instanceof Multipart) {
Multipart mpp = (Multipart)mp;
for(int count =0;count<mpp.getCount();count++){
MimeBodyPart bp = (MimeBodyPart)mpp.getBodyPart(count);
InputStream fileNme = bp.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(fileNme, writer, "UTF-8");
String theString = writer.toString();
System.out.println("Text: " +theString);
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您还需要一个包含IOUtils的jar,您可以从 http://www.java2s.com/Code/Jar/o/Downloadorgapachecommonsiojar.htm 下载
不知道你在问什么。
在最后的其他人中,您忽略了这样一个事实,即您已经确定它不是多部分(它失败了多部分实例),但无论如何都要继续尝试将其转换为多部分。 当然会失败。
既然您知道 ImapInputStream 是一种可能性,请专门为该类添加一个 else-if(或者更好的是,用于 InputStream),并像处理任何其他流一样处理它。 更好的是,Java EE 文档说,如果流不知道如何处理数据类型,就会返回流,所以也许这是你的最后一个。
如果您专门检查流,那么您的最终 else 应该会生成某种错误。