使用java邮件错误地解析java中的电子邮件主题

时间:2013-05-30 09:50:47

标签: java javamail

我使用Java邮件1.4.6来阅读和解析gmail收件箱的电子邮件。但问题是当我想要解析电子邮件主题(示例主题字符串是“在Jenkins构建失败:appanalyti x appanalyti x#51”)时带语句

String subjectStr=(String) mimeMessage.getSubject();

我正在

  

subjectStr改为“在Jenkins构建失败:appanalytix»appanal”   “在詹金斯建造失败:appanalyti x»appanalyti x#51”。

我可以知道我哪里错了吗?是否需要解码主题字符串

代码是:

public boolean parseEmailSubject(String host,String userName,String password,String configurationStringToCheck,String saveDirectory){
....
Session session = Session.getDefaultInstance(properties);
        try {
            IMAPSSLStore store = (IMAPSSLStore)session.getStore(IMAP);
            store.connect( host, userName, password ) ;
            Folder folderInbox =store.getFolder(IMAP_FOLDER);
            folderInbox.open(Folder.READ_ONLY);
            Message[] arrayMessages = folderInbox.getMessages();
            for (int i = 0; i < arrayMessages.length; i++) {
                MimeMessage message = (MimeMessage) arrayMessages[i];
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                message.writeTo(bos);
                bos.close();
                SharedByteArrayInputStream bis =new SharedByteArrayInputStream(bos.toByteArray());
                MimeMessage cmsg = new MimeMessage(session, bis);
                bis.close();

                String megSubject = cmsg.getSubject();
                if(megSubject.contains(configurationStringToCheck)){
                    String contentType = cmsg.getContentType();

                    if (contentType.contains(IMAP_CONTENT_TYPE)) {
                        Multipart multiPart = (Multipart) cmsg.getContent();
                        int numberOfParts = multiPart.getCount();
                        for (int partCount = 0; partCount < numberOfParts; partCount++) {
                            MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                            if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                                String fileName = part.getFileName();
                                part.saveFile(saveDirectory + File.separator + fileName);
                            }
                        }
                    }
                    return true;

                }

            }
            folderInbox.close(false);
            store.close();

        }catch (Exception e) {
        }
}

电子邮件示例是 (下面的主题行) 在詹金斯建立失败:appanalyti x»appanalyti x#51

(下方的身体部位) 变化:


&lt; === [JENKINS REMOTING CAPACITY] ===&gt;频道开始了 log4j:WARN没有为logger找到appender(org.apache.commons.beanutils.converters.BooleanConverter)。 log4j:WARN请正确初始化log4j系统。

2 个答案:

答案 0 :(得分:0)

如果您使用的是gmail服务器,那么这是最好的代码,只需输入您的用户名和密码并运行它

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    public static void main(String[] args) {

        final String username = "username";
        final String password = "fghdf";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("username"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to email is"));
            message.setSubject("Testing Subject");
            message.setText("Dear user ,"
                + "\n\n your username is xxx and pasword is yyy");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

答案 1 :(得分:0)

JavaMail为您解码主题。当然,如果主题编码不正确,则无法正确解码。

主题标题中的原始文本是什么?

JavaMail调试输出显示的是什么?