如何阅读电子邮件内容?

时间:2015-04-29 08:13:12

标签: java email gmail inbox

我开发了简单的Java应用程序来读取我的Gmail收件箱邮件。我可以阅读Email From和Subject。但是,我无法阅读电子邮件内容。

当我尝试阅读时,我得到以下异常:

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块内并触发异常。

2 个答案:

答案 0 :(得分:1)

不确定你在问什么。

在最后的其他情况下,你忽略了这样一个事实,即你已经确定它不是Multipart(它是Multipart的失败实例),但是请继续尝试将其转换为Multipart。当然它会失败。

既然你知道ImapInputStream是一种可能性,那么为该类添加一个else-if(或者更好的是,对于InputStream)并像处理任何其他流一样处理它。更好的是,Java EE docs如果它不知道如何处理数据类型,则会返回一个流,所以也许这是你的最后一个。

如果您专门检查一个流,那么您的最终其他应该会产生某种错误。

答案 1 :(得分:1)

我在代码中做了一些更改,希望有所帮助

 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

下载