Java Code Examples for javax.mail.Message#getContentType()

The following examples show how to use javax.mail.Message#getContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowedMessageUtils.java    From james-project with Apache License 2.0 6 votes vote down vote up
/**
 * If the message is <code>format=flowed</code> 
 * set the encoded version as message content.
 */
public static void deflowMessage(Message m) throws MessagingException, IOException {
    ContentType ct = new ContentType(m.getContentType());
    String format = ct.getParameter("format");
    if (ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed")) {
        String delSp = ct.getParameter("delsp");
        String deflowed = deflow((String) m.getContent(), delSp != null && delSp.equalsIgnoreCase("yes"));
        
        ct.getParameterList().remove("format");
        ct.getParameterList().remove("delsp");
        
        if (ct.toString().contains("flowed")) {
            LOGGER.error("FlowedMessageUtils dind't remove the flowed correctly");
        }

        m.setContent(deflowed, ct.toString());
        m.saveChanges();
    }
}
 
Example 2
Source File: FlowedMessageUtils.java    From james-project with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the message content (if text/plain).
 */
public static void flowMessage(Message m, boolean delSp, int width) throws MessagingException, IOException {
    ContentType ct = new ContentType(m.getContentType());
    if (!ct.getBaseType().equals("text/plain")) {
        return;
    }
    String format = ct.getParameter("format");
    String text = format != null && format.equals("flowed") ? deflow(m) : (String) m.getContent();
    String coded = flow(text, delSp, width);
    ct.setParameter("format", "flowed");
    if (delSp) {
        ct.setParameter("delsp", "yes");
    }
    m.setContent(coded, ct.toString());
    m.saveChanges();
}
 
Example 3
Source File: ArdulinkMailMessageCountListener.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private void validateContentType(Message message) throws MessagingException, IOException {
	boolean isValid = false;
	if(message.isMimeType("text/plain")) { // TODO validare anche text/html
		isValid = true;
	} else if(message.isMimeType("multipart/*")) {
		isValid = isMultipartValid(message);
	}
	
	if(!isValid) {
		throw new MessagingException("MimeType " + message.getContentType() + " is not recognized");
	}
}
 
Example 4
Source File: ArdulinkMailMessageCountAdapter.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
private void validateContentType(Message message) throws MessagingException, IOException {
	boolean isValid = false;
	if(message.isMimeType("text/plain")) { // TODO validare anche text/html
		isValid = true;
	} else if(message.isMimeType("multipart/*")) {
		isValid = isMultipartValid(message);
	}
	
	if(!isValid) {
		throw new MessagingException("MimeType " + message.getContentType() + " is not recognized");
	}
}
 
Example 5
Source File: FlowedMessageUtils.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the content of the encoded message, if previously encoded as <code>format=flowed</code>.
 */
public static String deflow(Message m) throws IOException, MessagingException {
    ContentType ct = new ContentType(m.getContentType());
    String format = ct.getParameter("format");
    if (ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed")) {
        String delSp = ct.getParameter("delsp");
        return deflow((String) m.getContent(), delSp != null && delSp.equalsIgnoreCase("yes"));
        
    } else if (ct.getPrimaryType().equals("text")) {
        return (String) m.getContent();
    } else {
        return null;
    }
}
 
Example 6
Source File: MailInput.java    From hop with Apache License 2.0 4 votes vote down vote up
Object[] parseToArray( Object[] r, Message message ) throws Exception {

      // Execute for each Input field...
      for ( int i = 0; i < data.nrFields; i++ ) {
        int index = data.totalpreviousfields + i;

        try {

          switch ( meta.getInputFields()[ i ].getColumn() ) {
            case MailInputField.COLUMN_MESSAGE_NR:
              r[ index ] = new Long( message.getMessageNumber() );
              break;
            case MailInputField.COLUMN_SUBJECT:
              r[ index ] = message.getSubject();
              break;
            case MailInputField.COLUMN_SENDER:
              r[ index ] = StringUtils.join( message.getFrom(), ";" );
              break;
            case MailInputField.COLUMN_REPLY_TO:
              r[ index ] = StringUtils.join( message.getReplyTo(), ";" );
              break;
            case MailInputField.COLUMN_RECIPIENTS:
              r[ index ] = StringUtils.join( message.getAllRecipients(), ";" );
              break;
            case MailInputField.COLUMN_DESCRIPTION:
              r[ index ] = message.getDescription();
              break;
            case MailInputField.COLUMN_BODY:
              r[ index ] = data.mailConn.getMessageBody( message );
              break;
            case MailInputField.COLUMN_RECEIVED_DATE:
              Date receivedDate = message.getReceivedDate();
              r[ index ] = receivedDate != null ? new Date( receivedDate.getTime() ) : null;
              break;
            case MailInputField.COLUMN_SENT_DATE:
              Date sentDate = message.getSentDate();
              r[ index ] = sentDate != null ? new Date( sentDate.getTime() ) : null;
              break;
            case MailInputField.COLUMN_CONTENT_TYPE:
              r[ index ] = message.getContentType();
              break;
            case MailInputField.COLUMN_FOLDER_NAME:
              r[ index ] = data.mailConn.getFolderName();
              break;
            case MailInputField.COLUMN_SIZE:
              r[ index ] = new Long( message.getSize() );
              break;
            case MailInputField.COLUMN_FLAG_DRAFT:
              r[ index ] = new Boolean( data.mailConn.isMessageDraft( message ) );
              break;
            case MailInputField.COLUMN_FLAG_FLAGGED:
              r[ index ] = new Boolean( data.mailConn.isMessageFlagged( message ) );
              break;
            case MailInputField.COLUMN_FLAG_NEW:
              r[ index ] = new Boolean( data.mailConn.isMessageNew( message ) );
              break;
            case MailInputField.COLUMN_FLAG_READ:
              r[ index ] = new Boolean( data.mailConn.isMessageRead( message ) );
              break;
            case MailInputField.COLUMN_FLAG_DELETED:
              r[ index ] = new Boolean( data.mailConn.isMessageDeleted( message ) );
              break;
            case MailInputField.COLUMN_ATTACHED_FILES_COUNT:
              r[ index ] = new Long( data.mailConn.getAttachedFilesCount( message, null ) );
              break;
            case MailInputField.COLUMN_HEADER:
              String name = meta.getInputFields()[ i ].getName();
              // *only one name
              String[] arr = { name };
              // this code was before generic epoch
              Enumeration<?> en = message.getMatchingHeaders( arr );
              if ( en == null ) {
                r[ index ] = "";
                break;
              }
              List<String> headers = new ArrayList<>();
              while ( en.hasMoreElements() ) {
                Header next = Header.class.cast( en.nextElement() );
                headers.add( next.getValue() );
              }
              // [PDI-6532] if there is no matching headers return empty String
              r[ index ] = headers.isEmpty() ? "" : StringUtils.join( headers, ";" );
              break;
            case MailInputField.COLUMN_BODY_CONTENT_TYPE:
              r[ index ] = data.mailConn.getMessageBodyContentType( message );
              break;
            default:

              break;
          }
        } catch ( Exception e ) {
          String errMsg = "Error adding value for field " + meta.getInputFields()[ i ].getName();
          throw new Exception( errMsg, e );
        }
      }
      return r;
    }
 
Example 7
Source File: FlowedMessageUtils.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Checks whether the input message is <code>format=flowed</code>.
 */
public static boolean isFlowedTextMessage(Message m) throws MessagingException {
    ContentType ct = new ContentType(m.getContentType());
    String format = ct.getParameter("format");
    return ct.getBaseType().equals("text/plain") && format != null && format.equalsIgnoreCase("flowed");
}
 
Example 8
Source File: cfMailMessageData.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
private boolean extractMessage( Message Mess, long messageID, String attachURI, String attachDIR ){
cfArrayData ADD	= cfArrayData.createArray(1);

try{

	setData( "subject", 	new cfStringData( Mess.getSubject() ) );
	setData( "id",				new cfNumberData( messageID ) );
	
	//--- Pull out all the headers
	cfStructData	headers	= new cfStructData();
	Enumeration<Header> eH	= Mess.getAllHeaders();
	String headerKey;
	while (eH.hasMoreElements()){
		Header hdr	= eH.nextElement();
		
		headerKey = hdr.getName().replace('-','_').toLowerCase();
		if ( headers.containsKey(headerKey) ){
			headers.setData( headerKey, new cfStringData( headers.getData(headerKey).toString() + ";" + hdr.getValue() ) );
		}else
			headers.setData( headerKey, new cfStringData( hdr.getValue() ) );
	}
	
	setData( "headers",  headers );

	// Get the Date
    Date DD = Mess.getReceivedDate();
    if ( DD == null )
			setData( "rxddate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			setData( "rxddate", 	new cfDateData( DD.getTime() ) );


    DD = Mess.getSentDate();
    if ( DD == null )
			setData( "sentdate", 	new cfDateData( System.currentTimeMillis() ) );
	  else
			setData( "sentdate", 	new cfDateData( DD.getTime() ) );
	
	// Get the FROM field
	Address[] from	= Mess.getFrom();
	if ( from != null && from.length > 0 ){
		cfStructData sdFrom	= new cfStructData();
		String name =  ((InternetAddress)from[0]).getPersonal();
		if ( name != null )
				sdFrom.setData( "name", new cfStringData(name) );
				
		sdFrom.setData( "email", new cfStringData( ((InternetAddress)from[0]).getAddress() ) );
		setData( "from", sdFrom );
	}
	
	//--[ Get the TO/CC/BCC field
	cfArrayData	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.TO) );
	if ( AD != null )
		setData( "to", AD );

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.CC) );
	if ( AD != null )
		setData( "cc", AD );		

	AD	= extractAddresses( Mess.getRecipients(Message.RecipientType.BCC) );
	if ( AD != null )
		setData( "bcc", AD );
		
	AD	= extractAddresses( Mess.getReplyTo() );
	if ( AD != null )
		setData( "replyto", AD );
		
	//--[ Set the flags
	setData( "answered",	cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.ANSWERED ) ) );
	setData( "deleted",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DELETED ) ) );
	setData( "draft",			cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.DRAFT ) ) );
	setData( "flagged",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.FLAGGED ) ) );
	setData( "recent",		cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.RECENT ) ) );
	setData( "seen",			cfBooleanData.getcfBooleanData( Mess.isSet( Flags.Flag.SEEN ) ) );

	setData( "size",			new cfNumberData( Mess.getSize() ) );
		setData( "lines",			new cfNumberData( Mess.getLineCount() ) );
	
	String tmp	= Mess.getContentType();
	if ( tmp.indexOf(";") != -1 )
		tmp	= tmp.substring( 0, tmp.indexOf(";") );

	setData( "mimetype", new cfStringData( tmp ) );
	
	// Get the body of the email
	extractBody( Mess, ADD, attachURI, attachDIR );

}catch(Exception E){
	return false;
}
	
setData( "body",	ADD );
return true;
}
 
Example 9
Source File: MailInput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
Object[] parseToArray( Object[] r, Message message ) throws Exception {

      // Execute for each Input field...
      for ( int i = 0; i < data.nrFields; i++ ) {
        int index = data.totalpreviousfields + i;

        try {

          switch ( meta.getInputFields()[i].getColumn() ) {
            case MailInputField.COLUMN_MESSAGE_NR:
              r[index] = new Long( message.getMessageNumber() );
              break;
            case MailInputField.COLUMN_SUBJECT:
              r[index] = message.getSubject();
              break;
            case MailInputField.COLUMN_SENDER:
              r[index] = StringUtils.join( message.getFrom(), ";" );
              break;
            case MailInputField.COLUMN_REPLY_TO:
              r[index] = StringUtils.join( message.getReplyTo(), ";" );
              break;
            case MailInputField.COLUMN_RECIPIENTS:
              r[index] = StringUtils.join( message.getAllRecipients(), ";" );
              break;
            case MailInputField.COLUMN_DESCRIPTION:
              r[index] = message.getDescription();
              break;
            case MailInputField.COLUMN_BODY:
              r[index] = data.mailConn.getMessageBody( message );
              break;
            case MailInputField.COLUMN_RECEIVED_DATE:
              Date receivedDate = message.getReceivedDate();
              r[index] = receivedDate != null ? new Date( receivedDate.getTime() ) : null;
              break;
            case MailInputField.COLUMN_SENT_DATE:
              Date sentDate = message.getSentDate();
              r[index] = sentDate != null ? new Date( sentDate.getTime() ) : null;
              break;
            case MailInputField.COLUMN_CONTENT_TYPE:
              r[index] = message.getContentType();
              break;
            case MailInputField.COLUMN_FOLDER_NAME:
              r[index] = data.mailConn.getFolderName();
              break;
            case MailInputField.COLUMN_SIZE:
              r[index] = new Long( message.getSize() );
              break;
            case MailInputField.COLUMN_FLAG_DRAFT:
              r[index] = new Boolean( data.mailConn.isMessageDraft( message ) );
              break;
            case MailInputField.COLUMN_FLAG_FLAGGED:
              r[index] = new Boolean( data.mailConn.isMessageFlagged( message ) );
              break;
            case MailInputField.COLUMN_FLAG_NEW:
              r[index] = new Boolean( data.mailConn.isMessageNew( message ) );
              break;
            case MailInputField.COLUMN_FLAG_READ:
              r[index] = new Boolean( data.mailConn.isMessageRead( message ) );
              break;
            case MailInputField.COLUMN_FLAG_DELETED:
              r[index] = new Boolean( data.mailConn.isMessageDeleted( message ) );
              break;
            case MailInputField.COLUMN_ATTACHED_FILES_COUNT:
              r[index] = new Long( data.mailConn.getAttachedFilesCount( message, null ) );
              break;
            case MailInputField.COLUMN_HEADER:
              String name = meta.getInputFields()[i].getName();
              // *only one name
              String[] arr = { name };
              // this code was before generic epoch
              Enumeration<?> en = message.getMatchingHeaders( arr );
              if ( en == null ) {
                r[index] = "";
                break;
              }
              List<String> headers = new ArrayList<String>();
              while ( en.hasMoreElements() ) {
                Header next = Header.class.cast( en.nextElement() );
                headers.add( next.getValue() );
              }
              // [PDI-6532] if there is no matching headers return empty String
              r[index] = headers.isEmpty() ? "" : StringUtils.join( headers, ";" );
              break;
            case MailInputField.COLUMN_BODY_CONTENT_TYPE:
              r[index] = data.mailConn.getMessageBodyContentType( message );
              break;
            default:

              break;
          }
        } catch ( Exception e ) {
          String errMsg = "Error adding value for field " + meta.getInputFields()[i].getName();
          throw new Exception( errMsg, e );
        }
      }
      return r;
    }