SMimeMessage

public class SMimeMessage

SMimeMessage represents a Secure MIME Message. It encapsulates a MIME body part and provides methods for digital signing, signature verification, encryption, decryption, compression, and decompression.

Author:Hugo Y. K. Lam

Fields

CONTENT_TRANSFER_ENC_BASE64

public static final String CONTENT_TRANSFER_ENC_BASE64

Content transfer encoding: Base 64

CONTENT_TRANSFER_ENC_BINARY

public static final String CONTENT_TRANSFER_ENC_BINARY

Content transfer encoding: Binary

DIGEST_ALG_MD5

public static final String DIGEST_ALG_MD5

Digest algorithm: MD5

DIGEST_ALG_SHA1

public static final String DIGEST_ALG_SHA1

Digest algorithm: SHA

ENCRYPT_ALG_DES_EDE3_CBC

public static final String ENCRYPT_ALG_DES_EDE3_CBC

Encryption algorithm: DES EDE3

ENCRYPT_ALG_RC2_CBC

public static final String ENCRYPT_ALG_RC2_CBC

Encryption algorithm: RC2

Constructors

SMimeMessage

public SMimeMessage(MimeBodyPart bodyPart)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.

SMimeMessage

public SMimeMessage(MimeBodyPart bodyPart, X509Certificate cert)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.
  • cert – the certificate for signature verification or encryption.

SMimeMessage

public SMimeMessage(MimeBodyPart bodyPart, X509Certificate cert, Session session)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.
  • cert – the certificate for signature verification or encryption.
  • session – the mail session.

SMimeMessage

public SMimeMessage(MimeBodyPart bodyPart, X509Certificate cert, PrivateKey privateKey)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.
  • cert – the certificate for signature verification or encryption.
  • privateKey – the private key for digital signing or decryption.

SMimeMessage

public SMimeMessage(MimeBodyPart bodyPart, X509Certificate cert, PrivateKey privateKey, Session session)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.
  • cert – the certificate for signature verification or encryption.
  • privateKey – the private key for digital signing or decryption.
  • session – the mail session.

SMimeMessage

protected SMimeMessage(MimeBodyPart bodyPart, SMimeMessage smime)

Creates a new instance of SMimeMessage.

Parameters:
  • bodyPart – the original MIME body part.
  • smime – the S/MIME message from which the configuration is copied.

Methods

compress

public SMimeMessage compress()

Compresses the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the compressed MIME body part.

decompress

public SMimeMessage decompress()

Decompresses the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the decompressed MIME body part.

decrypt

public SMimeMessage decrypt()

Decrypts the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the decrypted MIME body part.

decrypt

public SMimeMessage decrypt(PrivateKey privateKey)

Decrypts the encapsulated MIME body part.

Parameters:
  • privateKey – the private key for decryption.
Throws:
Returns:

an S/MIME message encapsulating the decrypted MIME body part.

digest

public String digest()

Digests the encapsulated MIME body part.

Throws:
Returns:

the digested value in Base 64 format.

digest

public String digest(String digestAlg, boolean isHeadersIncluded)

Digests the encapsulated MIME body part.

Parameters:
  • digestAlg – digest algorithm.
  • isHeadersIncluded – true if the digest should be computed on both the headers and the content of the encapsulated body part.
Throws:
Returns:

the digested value in Base 64 format.

encrypt

public SMimeMessage encrypt()

Encrypts the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the encrypted MIME body part.

encrypt

public SMimeMessage encrypt(X509Certificate cert)

Encrypts the encapsulated MIME body part.

Parameters:
  • cert – the certificate for encryption.
Throws:
Returns:

an S/MIME message encapsulating the encrypted MIME body part.

getBodyPart

public MimeBodyPart getBodyPart()

Gets the encapsulated MIME body part.

Returns:the encapsulated MIME body part.

getContentTransferEncoding

public String getContentTransferEncoding()

Gets the content transfer encoding which will be used in encryption, digital signing, and compression.

Returns:the content transfer encoding.

getDigestAlgorithm

public String getDigestAlgorithm()

Gets the digest algorithm which will be used in digital signing.

Returns:the digest algorithm.

getEncryptAlgorithm

public String getEncryptAlgorithm()

Gets the encryption algorithm which will be used in encryption.

Returns:the encryption algorithm.

isCompressed

public boolean isCompressed()

Checks if the encapsulated MIME body part is compressed.

Throws:
Returns:

true if the encapsulated MIME body part is compressed.

isEncrypted

public boolean isEncrypted()

Checks if the encapsulated MIME body part is encrypted.

Throws:
Returns:

true if the encapsulated MIME body part is encrypted.

isSigned

public boolean isSigned()

Checks if the encapsulated MIME body part is signed.

Throws:
Returns:

true if the encapsulated MIME body part is signed.

setContentTransferEncoding

public void setContentTransferEncoding(String contentTransferEncoding)

Sets the content transfer encoding to used in encryption, digital signing, and compression.

Parameters:
  • contentTransferEncoding – the content transfer encoding.

setDigestAlgorithm

public void setDigestAlgorithm(String digestAlgorithm)

Sets the digest algorithm to used in digital signing.

Parameters:
  • digestAlgorithm – the digest algorithm.

setEncryptAlgorithm

public void setEncryptAlgorithm(String encryptAlgorithm)

Sets the encryption algorithm to be used in encryption.

Parameters:
  • encryptAlgorithm – the encryption algorithm.

sign

public SMimeMessage sign()

Signs the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the signed MIME body part.

unsign

public SMimeMessage unsign()

Unsigns the encapsulated MIME body part.

Throws:
Returns:

the an S/MIME message encapsulating the signed content.

verify

public SMimeMessage verify()

Verifies the encapsulated MIME body part.

Throws:
Returns:

an S/MIME message encapsulating the signed content.

verify

public SMimeMessage verify(X509Certificate cert)

Verifies the encapsulated MIME body part.

Parameters:
  • cert – the certificate for verification.
Throws:
Returns:

an S/MIME message encapsulating the signed content.