如何正确读取电子邮件信封中的自定义标头值

时间:2012-02-27 22:05:31

标签: java

我正在编写一个java pop3客户端应用程序来发送/阅读向客户发送发票的电子邮件。 我在电子邮件发送过程中包含一个自定义标题,其中包含发票编号作为值。下面给出了相同的代码片段:

                        Message msg = new MimeMessage(session);

            msg.setFrom( new InternetAddress(from));
            InternetAddress [] address = {new InternetAddress(to)};
            InternetAddress [] replyto = {new InternetAddress(from)};
            msg.setRecipients(Message.RecipientType.TO, address);

            msg.setSubject(subject);
            //msg.setSendDate(new Date());
            msg.setReplyTo(replyto );
            msg.setText(messageText);

                   // Header is a string

//msg.setHeader("Custom",header);
            msg.addHeader("Custom",header);


            msg.saveChanges();
            Transport.send(msg);        

电子邮件扫描代码如下:

        // Create empty properties
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        // Get the store
        Store store = session.getStore("pop3");
        store.connect(host, username, password);
        // Get folder
        POP3Folder folder = (POP3Folder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);      

        Message[] msgs = folder.getMessages();

            // Use a suitable FetchProfile
            FetchProfile fp = new FetchProfile();
            fp.add(UIDFolder.FetchProfileItem.UID);
            fp.add(FetchProfile.Item.ENVELOPE);
            fp.add("Custom");

            folder.fetch(msgs, fp);
            Message msg;

            for (int i = 0; i < msgs.length;  i++)
            {
            String uid = folder.getUID(msgs[i]);
            log.info("UID :" +  uid);

            // if the UID is not available
            if (! uids.IsAvailable(uid)) {
                // Then fetch the message and push it to the array list
                Address[]   addrs;
                Date        dt;
                // Get subject
                String sub  = msgs[i].getSubject();

                // Get from address
                addrs   = msgs[i].getFrom();
                String from = addrs[0].toString();

                // Get send date
                dt = msgs[i].getSentDate();

                // Get the Header
                String [] header = msgs[i].getHeader("Custom");

                log.info("Reached Here 1");

                if (header == null)
                    log.info("Reached Here 4");

                if (header.length> 0 ) {
                    log.info("Reached Here 2");
                    log.info("Header value is " +  header[0]);
                    log.info("Reached Here 3");
                }

                log.info("The values are: " + from + "&" + sub);
                //msgs[i].getHeader(arg0)

                //Get the content of the message
                Part messagePart = msgs[i];
                Object content = messagePart.getContent();

                // -- or its first body part if it is a multipart message --
                if (content instanceof Multipart)
                {
                    messagePart = ((Multipart)content).getBodyPart(0);
                    log.info("This is a Multipart Message");
                }

                // -- Get the content type --
                String contentType = messagePart.getContentType();


                log.info("Retrieving the message content");
                String msgContent = "";
                // -- If the content is plain text, we can print it --
                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)
                    {
                        msgContent += thisLine;
                        thisLine = reader.readLine();
                    }
                }

但是在下面的语句中检索到的Header为null。

// Get the Header
String [] header = msgs[i].getHeader("Custom");

我错过了什么吗? 谢谢你的时间。

0 个答案:

没有答案