0

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

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

我正进入(状态

subjectStr 为“在 Jenkins 中构建失败:appanalytix » appanal”而不是“在 Jenkins 中构建失败: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) {
        }
}

电子邮件示例是(下面的主题行)在 Jenkins 中构建失败:appanalyti​x » appanalyti​x #51

(下面的正文部分)更改:


<===[JENKINS REMOTING CAPACITY]===> 频道已启动 log4j:WARN 找不到记录器 (org.apache.commons.beanutils.converters.BooleanConverter) 的附加程序。log4j:WARN 请正确初始化 log4j 系统。

4

2 回答 2

0

JavaMail 为您解码主题。当然,如果主题编码不正确,它就不会正确解码。

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

JavaMail 调试输出显示什么?

于 2013-05-30T18:53:16.500 回答
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);
        }
    }
}
于 2013-05-30T10:34:06.267 回答