Java Code Examples for javax.mail.internet.MimeMessage#isMimeType()

The following examples show how to use javax.mail.internet.MimeMessage#isMimeType() . 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: Message.java    From OrigamiSMTP with MIT License 6 votes vote down vote up
/** Processes the message
 */
public void process()
{
  System.out.println("Process message");
  try
  {
    Session session = Session.getDefaultInstance(new Properties());
    InputStream inputStream = new ByteArrayInputStream(message.getBytes());
    MimeMessage mimeMessage = new MimeMessage(session, inputStream);
    if (mimeMessage.isMimeType(Constants.PLAIN_MIME))
    {
      plainMessage = mimeMessage.getContent().toString();
    }
    else if (mimeMessage.isMimeType(Constants.MULTIPART_MIME))
    {
      MimeMultipart mimeMultipart = (MimeMultipart) mimeMessage.getContent();
      processMimeMultipart(mimeMultipart);
    }
    subject = mimeMessage.getSubject();
    System.out.println("Message processed");
  }
  catch (Exception ex)
  {
    ex.printStackTrace(System.err);
  }
}
 
Example 2
Source File: IsSMIMEEncrypted.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MailAddress> match(Mail mail) throws MessagingException {
    if (mail == null) {
        return null;
    }
    
    MimeMessage message = mail.getMessage();
    if (message == null) {
        return null;
    }
    
    if ((message.isMimeType("application/x-pkcs7-mime") 
            || message.isMimeType("application/pkcs7-mime")) && (message.getContentType().contains("smime-type=enveloped-data"))) {
        return mail.getRecipients();
    } else {
        return null;
    }
}
 
Example 3
Source File: IsSMIMESigned.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MailAddress> match(Mail mail) throws MessagingException {
    if (mail == null) {
        return null;
    }
    
    MimeMessage message = mail.getMessage();
    if (message == null) {
        return null;
    }
    
    
    if (message.isMimeType("multipart/signed") 
            || message.isMimeType("application/pkcs7-signature")
            || message.isMimeType("application/x-pkcs7-signature")
            || ((message.isMimeType("application/pkcs7-mime") || message.isMimeType("application/x-pkcs7-mime")) 
                    && message.getContentType().contains("signed-data"))) {
        return mail.getRecipients();
    } else {
        return null;
    }
}
 
Example 4
Source File: MailUtil.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean emlContainsAttachments(InputStream is) {
	try {
		MimeMessage msg = readMime(is);
		if (msg.isMimeType("multipart/*")) {
			Multipart mp = (Multipart) msg.getContent();
			int count = mp.getCount();
			return count > 1;
		}
	} catch (Throwable t) {
		log.warn(t.getMessage(), t);
	}
	return false;
}
 
Example 5
Source File: TextCalendarBodyToAttachment.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public void service(Mail mail) throws MessagingException {
    MimeMessage mimeMessage = mail.getMessage();
    if (mimeMessage.isMimeType(TEXT_CALENDAR_TYPE)) {
        processTextBodyAsAttachment(mimeMessage);
    }
}
 
Example 6
Source File: EmailAccount.java    From teammates with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the email message body as text.
 */
private String getTextFromEmail(MimeMessage email) throws MessagingException, IOException {
    if (email.isMimeType("text/*")) {
        return (String) email.getContent();
    } else {
        return getTextFromPart(email);
    }
}
 
Example 7
Source File: AbstractSign.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * <P>Checks if the mail can be signed.</P>
 * <P>Rules:</P>
 * <OL>
 * <LI>The reverse-path != null (it is not a bounce).</LI>
 * <LI>The sender user must have been SMTP authenticated.</LI>
 * <LI>Either:</LI>
 * <UL>
 * <LI>The reverse-path is the postmaster address and {@link #isPostmasterSigns} returns <I>true</I></LI>
 * <LI>or the reverse-path == the authenticated user
 * and there is at least one "From:" address == reverse-path.</LI>.
 * </UL>
 * <LI>The message has not already been signed (mimeType != <I>multipart/signed</I>
 * and != <I>application/pkcs7-mime</I>).</LI>
 * </OL>
 * @param mail The mail object to check.
 * @return True if can be signed.
 */
protected boolean isOkToSign(Mail mail) throws MessagingException {
    // Is it a bounce?
    if (!mail.hasSender()) {
        LOGGER.info("Can not sign: no sender");
        return false;
    }

    MailAddress reversePath = mail.getMaybeSender().get();

    Optional<String> fetchedAuthUser = AttributeUtils.getValueAndCastFromMail(mail, Mail.SMTP_AUTH_USER, String.class);
    // was the sender user SMTP authorized?
    if (!fetchedAuthUser.isPresent()) {
        LOGGER.info("Can not sign mail for sender <{}> as he is not a SMTP authenticated user", mail.getMaybeSender().asString());
        return false;
    }

    Username authUser = Username.of(fetchedAuthUser.get());

    // The sender is the postmaster?
    if (Objects.equal(getMailetContext().getPostmaster(), reversePath)) {
        // should not sign postmaster sent messages?
        if (!isPostmasterSigns()) {
            LOGGER.info("Can not sign mails for postmaster");
            return false;
        }
    } else {
        // is the reverse-path user different from the SMTP authorized user?
        Username username = getUsername(reversePath);
        if (!username.equals(authUser)) {
            LOGGER.info("SMTP logged in as <{}> but pretend to be sender <{}>", authUser.asString(), username.asString());
            return false;
        }
        // is there no "From:" address same as the reverse-path?
        if (!fromAddressSameAsReverse(mail)) {
            LOGGER.info("Can not sign mails with empty FROM header field");
            return false;
        }
    }

    // if already signed return false
    MimeMessage mimeMessage = mail.getMessage();
    boolean isAlreadySigned = mimeMessage.isMimeType("multipart/signed")
        || mimeMessage.isMimeType("application/pkcs7-mime");
    if (isAlreadySigned) {
        LOGGER.info("Can not sign a mail already signed");
    }
    return !isAlreadySigned;

}