iaik.smime
Class SMimeMailer

java.lang.Object
  extended by iaik.utils.SmtpMailer
      extended by iaik.smime.SMimeMailer

public class SMimeMailer
extends SmtpMailer

Simple S/MIME mailing tool based on the IAIK-JCE SmtpMailer utility. It may be used for sending signed, encrypted, or signed and encrypted text mails without usage of the JavaMail API. Only the application/pkcs7-mime type is used for both, signed (smime-type "signed-data") or encrypted (smime-type "enveloped-data") mails.

The following example shows how to use this class for sending a

S/MIME message:


Field Summary
static int ENCRYPTED
          Value for creating encrypted S/MIME messages.
static int SIGNED
          Value for creating signed S/MIME messages.
static int SIGNED_AND_ENCRYPTED
          Value for creating signed and encrypted S/MIME messages.
 
Constructor Summary
SMimeMailer(java.lang.String mailhost, int mode)
          Creates a new SMimeMailer.
 
Method Summary
 void addAdditionalField(java.lang.String line)
          Sets any additional field according to RFC822.
 void addBcc(java.lang.String realName, java.lang.String emailAddress, X509Certificate recipientCert, AlgorithmID keyEncAlg)
          Adds one receiver (Bcc: field) of the S/MIME email message.
 void addCc(java.lang.String realName, java.lang.String emailAddress, X509Certificate recipientCert, AlgorithmID keyEncAlg)
          Adds one receiver (Cc: field) of the S/MIME email message.
 void addDate()
          Sets the "Date:" field to the current date.
 void addDate(java.util.Date date)
          Sets the "Date:" field to the specified date.
 void addTo(java.lang.String realName, java.lang.String emailAddress, X509Certificate recipientCert, AlgorithmID keyEncAlg)
          Adds one receiver (To: field) of the S/MIME email message.
 boolean sendMail()
          Encrypt and send the mail that was just created.
 void setEncryption(AlgorithmID contentEncAlg, int keyLength)
          Sets the symmetric algorithm for encrypting the message.
 void setFrom(java.lang.String realName, java.lang.String emailAddress, X509Certificate[] certificates, X509Certificate signerCertificate, X509Certificate encryptionCertOfSender, AlgorithmID keyEncAlg, java.security.PrivateKey signerKey, AlgorithmID hashAlgorithm, AlgorithmID signatureAlgorithm)
          Sets the from field of the S/MIME email message.
 void setFrom(java.lang.String realName, java.lang.String emailAddress, X509Certificate senderCert, AlgorithmID keyEncAlg)
          Sets the from field of the S/MIME email message.
 void setSecureRandom(java.security.SecureRandom random)
          Sets the source of randomness for the key generation.
 java.lang.String toString()
          Returns a string representation of this SMimeMailer.
 
Methods inherited from class iaik.utils.SmtpMailer
addBcc, addCc, addText, addTo, checkResponse, send, sendCommands, setDebug, setFrom, setLineSeparator, setReplyTo, setSender, setSubject, setText, setVerify, writeBody, writeHeaders
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENCRYPTED

public static final int ENCRYPTED
Value for creating encrypted S/MIME messages.

See Also:
Constant Field Values

SIGNED

public static final int SIGNED
Value for creating signed S/MIME messages.

See Also:
Constant Field Values

SIGNED_AND_ENCRYPTED

public static final int SIGNED_AND_ENCRYPTED
Value for creating signed and encrypted S/MIME messages.

See Also:
Constant Field Values
Constructor Detail

SMimeMailer

public SMimeMailer(java.lang.String mailhost,
                   int mode)
Creates a new SMimeMailer.

Parameters:
mailhost - the name of the SMTP server (e.g. "mailhost.domain.com")
mode - the S/MIME message mode (ENCRYPTED, SIGNED, or SIGNED_AND_ENCRYPTED)
Method Detail

setFrom

public void setFrom(java.lang.String realName,
                    java.lang.String emailAddress,
                    X509Certificate senderCert,
                    AlgorithmID keyEncAlg)
Sets the from field of the S/MIME email message. Use this method to set the from field for sending an encrypted message where the sender also should be able to decrypt the encrypted message. If the sender doesn't need the possibility to encrypt the message you can use the method void setFrom(String realName, String emailAddress); from the superclass SmtpMailer.

Parameters:
realName - the real name of the sender (e.g. "John Doe")
emailAddress - the email address of the sender (e.g. "John.Doe@test.at")
senderCert - the certificate of the sender
keyEncAlg - the algorithm used to encrypt the temporary symmetric key for the sender

setFrom

public void setFrom(java.lang.String realName,
                    java.lang.String emailAddress,
                    X509Certificate[] certificates,
                    X509Certificate signerCertificate,
                    X509Certificate encryptionCertOfSender,
                    AlgorithmID keyEncAlg,
                    java.security.PrivateKey signerKey,
                    AlgorithmID hashAlgorithm,
                    AlgorithmID signatureAlgorithm)
             throws SMimeException
Sets the from field of the S/MIME email message. Use this method to set the from field for sending a signed or signed and encrypted message. If the message should be encrypted, too, and the sender should also be able to read (decrypt) the message, the encryption certificate of the sender and the key encryption algorithm (to be used to encrypt the temporary content encryption key) has to be not null.

Parameters:
realName - the real name of the sender (e.g. "John Doe")
emailAddress - the email address of the sender (e.g. "John.Doe@test.at")
certificates - the certificates to be set for the SignedData signature object (may also contain the encryption cert of the sender)
signerCertificate - the certificate of the signer (sender)
encryptionCertOfSender - the encryption cert of the sender (if to be included into the certificates field and/or the message should be encrypted for the sender, too; otherwise null)
keyEncAlg - the algorithm used to encrypt the temporary symmetric key for the sender (if the message should be encrypted, too; otherwise null)
signerKey - the private key of the sender used for signing the message
hashAlgorithm - the hash algorithm to be use (e.g. AlgorithmID.sha256)
signatureAlgorithm - the signature algorithm to be used (e.g. AlgorithmID.rsaEncryption)
Throws:
SMimeException - if the signer setup fails

addTo

public void addTo(java.lang.String realName,
                  java.lang.String emailAddress,
                  X509Certificate recipientCert,
                  AlgorithmID keyEncAlg)
Adds one receiver (To: field) of the S/MIME email message.

Parameters:
realName - the real name of the sender (e.g. "John Doe")
emailAddress - the email address of the sender (e.g. "John.Doe@test.at")
recipientCert - the certificate of the sender
keyEncAlg - the algorithm used to encrypt the temporary symmetric key for a receiver

addCc

public void addCc(java.lang.String realName,
                  java.lang.String emailAddress,
                  X509Certificate recipientCert,
                  AlgorithmID keyEncAlg)
Adds one receiver (Cc: field) of the S/MIME email message.

Parameters:
realName - the real name of the sender (e.g. "John Doe")
emailAddress - the email address of the sender (e.g. "John.Doe@test.at")
recipientCert - the certificate of the sender
keyEncAlg - the algorithm used to encrypt the temporary symmetric key for a receiver

addBcc

public void addBcc(java.lang.String realName,
                   java.lang.String emailAddress,
                   X509Certificate recipientCert,
                   AlgorithmID keyEncAlg)
Adds one receiver (Bcc: field) of the S/MIME email message.

Parameters:
realName - the real name of the sender (e.g. "John Doe")
emailAddress - the email address of the sender (e.g. "John.Doe@test.at")
recipientCert - the certificate of the sender
keyEncAlg - the algorithm used to encrypt the temporary symmetric key for a receiver

addAdditionalField

public void addAdditionalField(java.lang.String line)
Sets any additional field according to RFC822. For example: "MIME-Version: 1.0". This method overwrites the corresponding method in SmtpMailer to ensure that this additional headers are also encrypted.

Overrides:
addAdditionalField in class SmtpMailer
Parameters:
line - a complete line

addDate

public void addDate()
Sets the "Date:" field to the current date.


addDate

public void addDate(java.util.Date date)
Sets the "Date:" field to the specified date.

Parameters:
date - the date

setEncryption

public void setEncryption(AlgorithmID contentEncAlg,
                          int keyLength)
Sets the symmetric algorithm for encrypting the message. For RC2 the key length in bits can be: 40, 64, 128. If the encryption algorithm is not RC2 this parameter is ignored.

Examples for possible encryption algorithms are:

Parameters:
contentEncAlg - the algorithm for encrypting the content
keyLength - the key length for RC2

setSecureRandom

public void setSecureRandom(java.security.SecureRandom random)
Sets the source of randomness for the key generation.

Parameters:
random - a random number generator

sendMail

public boolean sendMail()
                 throws SmtpException
Encrypt and send the mail that was just created.

Overrides:
sendMail in class SmtpMailer
Returns:
true if the response codes from the server were the expected (if verify is switched on), false otherwise
Throws:
SmtpException - if there is a problem while sending the EMail
See Also:
SmtpMailer

toString

public java.lang.String toString()
Returns a string representation of this SMimeMailer.

Overrides:
toString in class SmtpMailer
Returns:
the string representation

This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note).

IAIK-CMS 6.0, (c) 2002 IAIK, (c) 2003, 2023 SIC