Package net.rim.blackberry.api.mail

Examples of net.rim.blackberry.api.mail.Message


     *
     * This method processes the MIME data and returns the message object
     *
     */      
    public Message parseMessage(String messageString,String locURI) {     
        Message msg = new Message();
        Hashtable ht = new Hashtable();

        try {           
            messageString = extractMIMEContent(messageString);
            ht = CreateHashTable(messageString);

            MailContent mailContent = new MailContent((String)ht.get("CONTENT-TYPE"));
            mailContent.parseContent();

            MailBody body = mailContent.getMailBody();

            Vector attachment = mailContent.getAttachmentList();

            msg.addHeader("FROM", (String)ht.get("FROM"));
            msg.addHeader("TO", (String)ht.get("TO"));
            msg.setHeader("DATE", (String)ht.get("DATE"));
            msg.setSentDate(parseRfc822Date((String)ht.get("DATE")));
            // Set sent date, shown when message is opened
            msg.addHeader("MIME-Version", (String)ht.get("MIME-VERSION"));
            // FIXME: should be set the received date too, if possible.
            MessageIDStore msgIdStore = new MessageIDStore();

            msgIdStore.setMessageMappingKey();

            msgIdStore.add(locURI, (String)ht.get("MESSAGE-ID"));

            msg.setSubject((String)ht.get("SUBJECT"));

            if ((String)ht.get("FROM") != null) {               
                String strFrom = (String)ht.get("FROM");

                if(strFrom.indexOf("<") >= 0){
                    strFrom = strFrom.substring(strFrom.indexOf("<") + 1, strFrom.indexOf(">"));
                }
                Address from = new Address(strFrom.trim(), " ");
                msg.setFrom(from);
            }

            // FIXME: What if there is more than one To: ??
            if ((String)ht.get("TO") != null) {

                String strTo = (String)ht.get("TO");

                if (strTo.indexOf("<") >= 0) {
                    strTo = strTo.substring(strTo.indexOf("<") + 1, strTo.indexOf(">"));
                }
                Address to[] = new Address[1];
                to[0] = new Address(strTo.trim(), " ");
                msg.addRecipients(Message.RecipientType.TO, to);
            }

            /*
                while (strTo.indexOf("<") >= 0) {
                    addr = strTo.substring(strTo.indexOf("<") + 1, strTo.indexOf(">"));
                   
                    Arrays.add(to, new Address(addr.trim(), " "));
                }
                msg.addRecipients(Message.RecipientType.TO, to);
            */
           
            if (attachment.size() == 0) {
                if (body.getActualContent() != null) {                  
                    String encoding = (String)ht.get("CONTENT-TRANSFER-ENCODING");

                    if (encoding != null && encoding.trim().equalsIgnoreCase("base64")) {
                        msg.setContent(new String(Base64.decode(body.getActualContent().getBytes())));
                    } else {    
                        msg.setContent(body.getActualContent());
                    }
                }
            } else {

                Multipart mp = new Multipart();
                TextBodyPart bodypart = new TextBodyPart(mp);
                int bodyPartIndex = 0;

                if (body.getActualContent() != null) {

                    String encoding = body.getContentTransferEncoding();

                    bodypart.setContentType(body.getContentType());

                    String content = null;
                    if (encoding != null && encoding.trim().equalsIgnoreCase("base64")){
                        content = new String(Base64.decode(body.getActualContent().getBytes()));
                    } else {
                        content = body.getActualContent();
                    }

                    bodypart.setContent(content);
                    mp.addBodyPart(bodypart,bodyPartIndex);
                    ++bodyPartIndex;
                }

                for (int i = 0; i < attachment.size(); i++) {

                    SupportedAttachmentPart sp =  new SupportedAttachmentPart(mp);

                    MailAttachment mailAttachment = (MailAttachment)attachment.elementAt(i);
                    sp.setFilename(mailAttachment.getFileName());

                    if (mailAttachment.getContentTransferEncoding().equals("base64")){
                        sp.setContent(new String(Base64.decode(mailAttachment.getactualContent().getBytes())));
                    }
                    else {
                        sp.setContent(mailAttachment.getactualContent());
                    }

                    sp.setContentType(mailAttachment.getContentType());
                    mp.addBodyPart(sp,bodyPartIndex);
                    ++bodyPartIndex;
                }

                msg.setContent(mp);
            }

        } catch(FolderNotFoundException f) {
            StaticDataHelper.log("FolderNotFoundException in MIMEParser.parseMessage(): "
                                 + f.getMessage());
View Full Code Here


        MessageIDStore messageIDStore = new MessageIDStore();
       
        if (! (messageIDStore.checkIsDataThere(locURI) ||
               checkIsDataThere(messageID, Constants.ITEMSKEY)) ) {
              
            Message msg = mailParser.parseMessage(mail, locURI);

            // Set the message flags depending on the message type
            switch(type) {
                case 'I':       // Inbox
                    /*
                     * Sets the message status as an inbound (received) message:
                     * without using this method, the incoming message is shown
                     * in the Outbox instead of the Inbox
                     */
                    msg.setInbound(true);
                   
                    /*
                     * Indicates that the message has been received successfully
                     */
                    msg.setStatus(Message.Status.RX_RECEIVED, 1);
                   
                    /*
                     * This lets the user to answer to the message
                     */
                    msg.setFlag(Message.Flag.REPLY_ALLOWED, true);
                    break;
                case 'S':       // Sent
                    msg.setStatus(Message.Status.TX_SENT, 1);
                    break;
                case 'D':       // Draft
                    msg.setFlag(Message.Flag.SAVED_THEN_ORPHANED, true);
                    break;
                case 'T':       // Trash
                    // FIXME: is it possible to store a message in the trash?
                    //        Shall we just silently discard this?
                    break;
            }
            // Save the message to the correct folder
            folder.appendMessage(msg);
            // XXX: why this conversion?
            Integer msgId = new Integer(msg.getMessageId());
            String key = new String(type + "/" + msgId.toString());
            // Set record key
            record.setKey(key);
            // Add GUID/LUID mapping
            messageIDStore.add(locURI, msgId.toString());
View Full Code Here

        final Enumeration msg_enum = _messageVector.elements();
        final Vector partsToBeDownloaded = new Vector();

        // Get all messages
        while (msg_enum.hasMoreElements()) {
            final Message m = (Message) msg_enum.nextElement();

            // Get body parts containing attachments
            final BodyPart[] bodyParts =
                    _downloadManager.getAttachmentBodyParts(m);
View Full Code Here

                PIMDemo.errorDialog("Error: Email service not available, could not send notification to invitees");
                return;
            }
        }

        final Message msg = new Message(outbox);

        // Add the date, subject
        final Date currentTime = Calendar.getInstance().getTime();
        msg.setSentDate(currentTime);
        msg.setSubject(_subject.getText());

        // Add invitees as message recipients
        to = new Address[_invitees.size()];

        for (int i = 0; i < _invitees.size(); ++i) {
            try {
                final Contact c = (Contact) _invitees.elementAt(i);
                final String name = PIMDemo.getDisplayName(c);

                if (c.countValues(Contact.EMAIL) > 0) {
                    to[i] = new Address(c.getString(Contact.EMAIL, 0), name);
                }
            } catch (final AddressException e) {
                PIMDemo.errorDialog("Address(String, String) threw "
                        + e.toString());
            }
        }
        try {
            msg.addRecipients(Message.RecipientType.TO, to);
        } catch (final MessagingException me) {
            PIMDemo.errorDialog("Message#addRecipients(int, Address[]) threw "
                    + me.toString());
        }

        // Create a new multipart object to hold the calendar attachment
        final Multipart multipart = new Multipart("mixed");

        // Create a new calendar attachment with meeting request as body
        final ByteArrayOutputStream bouts = new ByteArrayOutputStream();
        try {
            final String[] formats =
                    PIM.getInstance().supportedSerialFormats(PIM.EVENT_LIST);

            for (int i = 0;; ++i) {
                if (formats[i].indexOf("2.0") != -1) {
                    PIM.getInstance().toSerialFormat(_event, bouts, null,
                            formats[i]); // Use the 2.0 format.
                    break;
                }
            }
        } catch (final PIMException e) {
            PIMDemo.errorDialog(e.toString());
        } catch (final UnsupportedEncodingException e) {
            PIMDemo.errorDialog("Serial format conversion failure: "
                    + e.toString()); // We couldn't find the proper format for
                                     // encoding!

        }

        final SupportedAttachmentPart bodypart =
                new SupportedAttachmentPart(multipart,
                        "text/calendar; component=vevent", "event.ics", bouts
                                .toByteArray());

        // Add attachment to multipart
        multipart.addBodyPart(new TextBodyPart(multipart, bouts.toString()));
        multipart.addBodyPart(bodypart);

        try {
            // Set multipart as message content
            msg.setContent(multipart);

            // Send the message using transport
            Transport.send(msg);
        } catch (final MessagingException e) {
            PIMDemo.errorDialog(e.toString());
View Full Code Here

        final TextBodyPart tbp = new TextBodyPart(mp, messageData);
        mp.addBodyPart(tbp);
        mp.addBodyPart(sap);
        final Folder folders[] =
                Session.getDefaultInstance().getStore().list(Folder.SENT);
        final Message message = new Message(folders[0]);
        final Address[] toAdds = new Address[1];
        toAdds[0] = new Address(email, email);
        message.addRecipients(Message.RecipientType.TO, toAdds);
        message.setContent(mp);
        message.setSubject("Message with attachment "
                + fileHolder.getFileName() + ".");
        Transport.send(message);
    }
View Full Code Here

    /**
     * Tests the email API
     */
    private void testEmail() {
        try {
            Transport.send(new Message());
        } catch (final MessagingException e) {
            errorDialog("Transport.send(Message) threw " + e.toString());
        }

    }
View Full Code Here

                    final Object context) {
                final int choice =
                        Dialog.ask(Dialog.D_YES_NO, "Delete message?",
                                Dialog.YES);
                if (choice == Dialog.YES) {
                    final Message message = (Message) getSelectedItem();
                    final Folder folder = message.getFolder();
                    folder.deleteMessage(message);
                    updateScreen();
                }
            }
        }));
View Full Code Here

            }

            // Populate the list with the messages in the current folder and
            // add a message listener to them.
            for (int messageIndex = 0; messageIndex < messages.length; messageIndex++) {
                final Message message = messages[messageIndex];
                message.addMessageListener(this);
                _messages.addElement(message);
            }
        }
    }
View Full Code Here

     * Opens the selected message
     *
     * @see com.rim.samples.device.blackberrymaildemo.BlackBerryMailDemoScreen#openAction()
     */
    protected void openAction() {
        final Message selectedMessage = (Message) getSelectedItem();
        if (selectedMessage != null) {
            openMessage(selectedMessage);
        }
    }
View Full Code Here

    public void close() {
        // De-register listeners before closing
        _store.removeFolderListener(this);

        for (int index = _messages.size() - 1; index >= 0; index--) {
            final Message msg = (Message) _messages.elementAt(index);
            msg.removeMessageListener(this);
        }

        super.close();
    }
View Full Code Here

TOP

Related Classes of net.rim.blackberry.api.mail.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.