commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Petr Osipov (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (EMAIL-155) Not Able To Parse Attachments
Date Wed, 24 Jun 2015 14:54:04 GMT

    [ https://issues.apache.org/jira/browse/EMAIL-155?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14599515#comment-14599515
] 

Petr Osipov commented on EMAIL-155:
-----------------------------------

{quote}
The problem must then be related to the way you created or received the MimeMessage, can you
provide a code snippet for this as well?
{quote}
This is the snippet. Is that OK?
{code}
	Store store = getEmailStore(username);
	Folder inbox = store.getFolder("Inbox");

	try {
		inbox.open(Folder.READ_WRITE);

		LOG.info("Retrieving messages for {}.", username);
		Message[] messages = inbox.getMessages();
		LOG.info("Count of messages {} for {}.", messages.length, username);
	}
	for (Message msg : messages) {
		LOG.info("Processing message for {}. Message from {}.", username, Arrays.toString(msg.getFrom()));
		try {
			if (msg instanceof MimeMessage) {
				DataSource attachmentDatasource = getAttachment(username, (MimeMessage) msg);
			}
	}
	
    public DataSource getAttachment(String username, MimeMessage mimeMessage) throws Exception
{
        MimeMessageParser messageParser = new MimeMessageParser(mimeMessage);
		////////////////////////////////////////////////////////////////////////////
		// HERE it blows
		////////////////////////////////////////////////////////////////////////////	
        messageParser.parse();

        List<DataSource> attachmentList = messageParser.getAttachmentList();
        int attachmentCount = attachmentList.size();
        if (attachmentCount != 1) {
            String msg = String.format("Unexpected number of attachments %s for %s. Should
be exactly one.", attachmentCount, username);
            notifyError("Unexpected Number of Attachments", msg);
            return null;
        }
        return attachmentList.get(0);
    }

    public Store getEmailStore(String user) throws MessagingException {
        String pass = mailProp.getProperty("mail.retriever.password");
        String host = mailProp.getProperty("mail.pop3.host");
        LOG.info("Connecting using mail account {} to {}.", user, host);

        Session sess = Session.getInstance(mailProp, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pass);
            }
        });
        Store store = sess.getStore("pop3");
        store.connect(user, pass);

        return store;
    }
{code}

{quote}
As a test, could you write the MimeMessage to a File (using the MimeMessageUtils class) and
then read it back in and parse?
{quote}
Will have a try. (y)

> Not Able To Parse Attachments
> -----------------------------
>
>                 Key: EMAIL-155
>                 URL: https://issues.apache.org/jira/browse/EMAIL-155
>             Project: Commons Email
>          Issue Type: Bug
>         Environment: org.apache.commons:commons-email:1.4
>            Reporter: Petr Osipov
>            Priority: Critical
>         Attachments: erroneous.email
>
>
> h4. Scenario
> # Sent an email from Microsoft Web UI,
> # When code tries to parse
> {code}
>     public DataSource getAttachment(String username, MimeMessage mimeMessage) throws
Exception {
>         MimeMessageParser messageParser = new MimeMessageParser(mimeMessage);
>         messageParser.parse();
>         // further code
> }
> {code}
> An exception is thrown
> {noformat}
> java.lang.ClassCastException: javax.mail.util.SharedByteArrayInputStream cannot be cast
to javax.mail.Multipart
> 	at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:195)
> 	at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:96)
> {noformat}
> Probably int the wild casting to javax.mail.Multipart in MimeMessageParser is a too brave
step
> {code}
>                 if (isMimeType(part, "multipart/*"))
>                 {
>                     this.isMultiPart = true;
>                     final Multipart mp = (Multipart) part.getContent();
>                     final int count = mp.getCount();
>                     // iterate over all MimeBodyPart
>                     for (int i = 0; i < count; i++)
>                     {
>                         parse(mp, (MimeBodyPart) mp.getBodyPart(i));
>                     }
>                 }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message