Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当电子邮件带有附件时,是否会阅读电子邮件正文?_Java_Email_Jakarta Mail - Fatal编程技术网

Java 当电子邮件带有附件时,是否会阅读电子邮件正文?

Java 当电子邮件带有附件时,是否会阅读电子邮件正文?,java,email,jakarta-mail,Java,Email,Jakarta Mail,我发现了一个很好的代码,可以从我的收件箱电子邮件服务器读取电子邮件,该代码工作正常,但有几个问题: 我需要捕获每封电子邮件的邮件正文,但是当电子邮件有附件时,我会收到一个字符串,如下所示javax.mail.internet。MimeMultipart@100363或com.sun.mail.util。BASE64DecoderStream@14e8cee,而不是正文消息。如果电子邮件只是纯文本,没有附件,那么它就可以正常工作。似乎我不是唯一一个有这个问题的人,我一直在网上阅读,但找不到解决办法

我发现了一个很好的代码,可以从我的收件箱电子邮件服务器读取电子邮件,该代码工作正常,但有几个问题:

  • 我需要捕获每封电子邮件的邮件正文,但是当电子邮件有附件时,我会收到一个字符串,如下所示
    javax.mail.internet。MimeMultipart@100363
    com.sun.mail.util。BASE64DecoderStream@14e8cee
    ,而不是正文消息。如果电子邮件只是纯文本,没有附件,那么它就可以正常工作。似乎我不是唯一一个有这个问题的人,我一直在网上阅读,但找不到解决办法

  • 当附件大小超过4MB时,我想忽略附件

  • 下面是课堂,如果有任何评论,我将不胜感激。重要的是,我是java的新手程序员。提前谢谢

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Flags;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.NoSuchProviderException;
    import javax.mail.Part;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Store;
    import javax.mail.Flags.Flag;
    import javax.mail.search.FlagTerm;
    
    public class ReadMailSample {
        Properties properties = null;
        private Session session = null;
        private Store store = null;
        private Folder inbox = null;
        private String userName; 
        private String password;
        String downloadDirectory = "D:/Attachment/";
    
        public ReadMailSample() {
            userName = "myemail@domain.com";// provide user name
            password = "mypassword";// provide password
        }
    
        public void readMails() {
            properties = new Properties();
            properties.setProperty("mail.host", "imap.secureserver.net");
            properties.setProperty("mail.port", "110");
            properties.setProperty("mail.transport.protocol", "imap");
            session = Session.getInstance(properties,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(userName, password);
                        }
                    });
            try {
                store = session.getStore("imap");
                store.connect();
                inbox = store.getFolder("INBOX");
                inbox.open(Folder.READ_WRITE);
                Message messages[] = inbox.search(new FlagTerm(
                        new Flags(Flag.SEEN), false));
                ;
                System.out.println("Number of mails = " + messages.length);
                for (int i = 0; i < messages.length; i++) {
                    Message message = messages[i];
                    Address[] from = message.getFrom();
                    System.out.println("--> ** E-mail No. "+ i + " **");
                    System.out.println("-------------------------------");
                    System.out.println("Date : " + message.getSentDate());
                    System.out.println("From : " + from[0]);
                    System.out.println("Subject: " + message.getSubject());
                    System.out.println("Content :");
                    processMessageBody(message);
                    System.out.println("--------------------------------");
    
                }
                inbox.close(true);
                store.close();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    
        public void processMessageBody(Message message) {
            try {
                Object content = message.getContent();
                // check for string
                // then check for multipart
                if (content instanceof String) {
                    System.out.println(content);
                } else if (content instanceof Multipart) {
                    System.out.println(content);
                    Multipart multiPart = (Multipart) content;
                    procesMultiPart(multiPart);
                } else if (content instanceof InputStream) {
                    InputStream inStream = (InputStream) content;
                    int ch;
                    while ((ch = inStream.read()) != -1) {
                        System.out.write(ch);
                }
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    
        public void procesMultiPart(Multipart content) {
    
            try {
    
                for (int i = 0; i < content.getCount(); i++) {
                    BodyPart bodyPart = content.getBodyPart(i);
                    Object o;
    
                    o = bodyPart.getContent();
                    if (o instanceof String) {
                        System.out.println("Text = " + o);
                    } else if (null != bodyPart.getDisposition()
                            && bodyPart.getDisposition().equalsIgnoreCase(
                                    Part.ATTACHMENT)) {
                        String fileName = bodyPart.getFileName();
                        System.out.println("Text = " + o);
                        System.out.println("fileName = " + fileName);
                        InputStream inStream = bodyPart.getInputStream();
                        FileOutputStream outStream = new FileOutputStream(new File(
                                downloadDirectory + fileName));
                        byte[] tempBuffer = new byte[4096];// 4 KB
                        int numRead;
                        while ((numRead = inStream.read(tempBuffer)) != -1) {
                            outStream.write(tempBuffer);
                        }
                        inStream.close();
                        outStream.close();
                    }
                    // else?
    
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
    
        }
    
        public static void main(String[] args) {
            ReadMailSample sample = new ReadMailSample();
            sample.readMails();
        }
    }
    
    导入java.io.File;
    导入java.io.FileOutputStream;
    导入java.io.IOException;
    导入java.io.InputStream;
    导入java.util.Properties;
    导入javax.mail.Address;
    导入javax.mail.BodyPart;
    导入javax.mail.Flags;
    导入javax.mail.Folder;
    导入javax.mail.Message;
    导入javax.mail.MessaginException;
    导入javax.mail.Multipart;
    导入javax.mail.NoSuchProviderException;
    导入javax.mail.Part;
    导入javax.mail.PasswordAuthentication;
    导入javax.mail.Session;
    导入javax.mail.Store;
    导入javax.mail.Flags.Flag;
    导入javax.mail.search.FlagTerm;
    公共类ReadMailSample{
    Properties=null;
    私有会话=null;
    私有存储=空;
    私人文件夹收件箱=空;
    私有字符串用户名;
    私有字符串密码;
    字符串downloadDirectory=“D:/Attachment/”;
    公共ReadMailSample(){
    用户名=”myemail@domain.com“;//提供用户名
    password=“mypassword”;//提供密码
    }
    公开作废读邮件(){
    属性=新属性();
    properties.setProperty(“mail.host”、“imap.secureserver.net”);
    properties.setProperty(“mail.port”、“110”);
    properties.setProperty(“mail.transport.protocol”、“imap”);
    session=session.getInstance(属性,
    新的javax.mail.Authenticator(){
    受保护的密码身份验证getPasswordAuthentication(){
    返回新密码身份验证(用户名、密码);
    }
    });
    试一试{
    store=session.getStore(“imap”);
    store.connect();
    收件箱=store.getFolder(“收件箱”);
    收件箱。打开(文件夹。读写);
    messages[]=收件箱。搜索(新标记词(
    新标志(Flag.SEEN,false));
    ;
    System.out.println(“邮件数=”+消息长度);
    for(int i=0;i**电子邮件号“+i+”**”);
    System.out.println(“------------------------------------”;
    System.out.println(“日期:+message.getSentDate());
    System.out.println(“From:+From[0]);
    System.out.println(“主题:+message.getSubject());
    System.out.println(“内容:”);
    processMessageBody(消息);
    System.out.println(“-------------------------------------”);
    }
    收件箱。关闭(true);
    store.close();
    }捕获(无此提供异常e){
    e、 printStackTrace();
    }捕获(消息异常e){
    e、 printStackTrace();
    }
    }
    public void processMessageBody(消息消息){
    试一试{
    对象内容=message.getContent();
    //检查字符串
    //然后检查多部分
    if(字符串的内容实例){
    系统输出打印项次(内容);
    }else if(多部分的内容实例){
    系统输出打印项次(内容);
    多部分多部分=(多部分)内容;
    过程多部分(多部分);
    }else if(InputStream的内容实例){
    InputStream inStream=(InputStream)内容;
    int-ch;
    而((ch=inStream.read())!=-1){
    系统输出写入(ch);
    }
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    }捕获(消息异常e){
    e、 printStackTrace();
    }
    }
    public void procesMultiPart(多部分内容){
    试一试{
    for(int i=0;i