public class SMimeMailer
extends iaik.utils.SmtpMailer
The following example shows how to use this class for sending a
// mailhost String mailhost = "mailhost"; // sender name String senderName = "John Doe"; // sender address String senderAddress = "John.Doe@test.at"; // certificate list to be included into the SignedData X509Certificate[] certificates = ...; // certificate of the signer X509Certificate senderCertificate = ...; // encryption cert of sender (if to be included into the certificates field) X509Certificate encryptionCertOfSender = ...; // the private signer key PrivateKey signerKey = ...; // create a SMimeMailer SMimeMailer mailer = new SMimeMailer(mailhost, SMimeMailer.SIGNED); // set subject header mailer.setSubject("..."); // sending date is now mailer.addDate(); // set sender (signer) information mailer.setFrom(senderName, senderAddress, certificates, signerCertificate, encryptionCertOfSender, null, signerKey, AlgorithmID.sha256, AlgorithmID.rsaEncryption); // set recipient name and recipient address String recipientName = ...; String recipientAddress = ...; mailer.addTo(recipientName, recipientAddress); // set the message text mailer.addText(...); // send mail mailer.sendMail();
// mailhost String mailhost = "mailhost"; // sender name String senderName = "John Doe"; // sender address String senderAddress = "John.Doe@test.at"; // encryption cert of sender (to encrypt the symmetric key for the sender, too) X509Certificate encryptionCertOfSender = ...; // key encryption algorithm for encrypting the symmetric key for the sender AlgorithmID keyEncAlgOfSender = AlgorithmID.rsaEncryption; // create a SMimeMailer SMimeMailer mailer = new SMimeMailer(mailhost, SMimeMailer.ENCRYPTED); // set subject header mailer.setSubject("..."); // sending date is now mailer.addDate(); // set sender information mailer.setFrom(senderName, senderAddress, encryptionCertOfSender, keyEncAlgOfSender); // encryption cert of the recipient X509Certificate recipientCert = ...; // key encryption algorithm for encrypting the symmetric key for the recipient AlgorithmID keyEncAlgOfRecipient = AlgorithmID.rsaEncryption; // set recipient information String recipientName = ...; String recipientAddress = ...; mailer.addTo(recipientName, recipientAddress, recipientCert, keyEncAlgOfRecipient); // set encryption algorithm information AlgorithmID contentEA = (AlgorithmID)AlgorithmID.aes256_CBC.clone(); int keyLength = 256; mailer.setEncryption(contentEA, keyLength); // set the message text mailer.addText(...); // send mail mailer.sendMail();
// mailhost String mailhost = "mailhost"; // sender name String senderName = "John Doe"; // sender address String senderAddress = "John.Doe@test.at"; // certificate list to be included into the SignedData X509Certificate[] certificates = ...; // certificate of the signer X509Certificate signerCertificate = ...; // encryption cert of signer (to encrypt the symmetric key for the sender, too) X509Certificate encryptionCertOfSender = ...; // key encryption algorithm for encrypting the symmetric key for the sender (signer) AlgorithmID keyEncAlgOfSender = AlgorithmID.rsaEncryption; // the private signer key PrivateKey signerKey = ...; // create a SMimeMailer SMimeMailer mailer = new SMimeMailer(mailhost, SMimeMailer.SIGNED_AND_ENCRYPTED); // set subject header mailer.setSubject("..."); // sending date is now mailer.addDate(); // set sender (signer) information mailer.setFrom(senderName, senderAddress, certificates, signerCertificate, encryptionCertOfSender, keyEncAlgOfSender, signerKey, AlgorithmID.sha256, AlgorithmID.rsaEncryption); // encryption cert of the recipient X509Certificate recipientCert = ...; // key encryption algorithm for encrypting the symmetric key for the recipient AlgorithmID keyEncAlgOfRecipient = AlgorithmID.rsaEncryption; // set recipient information String recipientName = ...; String recipientAddress = ...; mailer.addTo(recipientName, recipientAddress, recipientCert, keyEncAlgOfRecipient); // set encryption algorithm information AlgorithmID contentEA = (AlgorithmID)AlgorithmID.aes256_CBC.clone(); int keyLength = 256; mailer.setEncryption(contentEA, keyLength); // set the message text mailer.addText(...); // send mail mailer.sendMail();
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
SMimeMailer(java.lang.String mailhost,
int mode)
Creates a new SMimeMailer.
|
Modifier and Type | Method and Description |
---|---|
void |
addAdditionalField(java.lang.String line)
Sets any additional field according to RFC822.
|
void |
addBcc(java.lang.String realName,
java.lang.String emailAddress,
iaik.x509.X509Certificate recipientCert,
iaik.asn1.structures.AlgorithmID keyEncAlg)
Adds one receiver (Bcc: field) of the S/MIME email message.
|
void |
addCc(java.lang.String realName,
java.lang.String emailAddress,
iaik.x509.X509Certificate recipientCert,
iaik.asn1.structures.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,
iaik.x509.X509Certificate recipientCert,
iaik.asn1.structures.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(iaik.asn1.structures.AlgorithmID contentEncAlg,
int keyLength)
Sets the symmetric algorithm for encrypting the message.
|
void |
setFrom(java.lang.String realName,
java.lang.String emailAddress,
iaik.x509.X509Certificate[] certificates,
iaik.x509.X509Certificate signerCertificate,
iaik.x509.X509Certificate encryptionCertOfSender,
iaik.asn1.structures.AlgorithmID keyEncAlg,
java.security.PrivateKey signerKey,
iaik.asn1.structures.AlgorithmID hashAlgorithm,
iaik.asn1.structures.AlgorithmID signatureAlgorithm)
Sets the from field of the S/MIME email message.
|
void |
setFrom(java.lang.String realName,
java.lang.String emailAddress,
iaik.x509.X509Certificate senderCert,
iaik.asn1.structures.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.
|
public static final int ENCRYPTED
public static final int SIGNED
public static final int SIGNED_AND_ENCRYPTED
public SMimeMailer(java.lang.String mailhost, int mode)
mailhost
- the name of the SMTP server (e.g. "mailhost.domain.com")mode
- the S/MIME message mode (ENCRYPTED
,
SIGNED
, or
SIGNED_AND_ENCRYPTED
)public void setFrom(java.lang.String realName, java.lang.String emailAddress, iaik.x509.X509Certificate senderCert, iaik.asn1.structures.AlgorithmID keyEncAlg)
void setFrom(String realName, String emailAddress);
from the superclass SmtpMailer.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 senderkeyEncAlg
- the algorithm used to encrypt the temporary symmetric key for the senderpublic void setFrom(java.lang.String realName, java.lang.String emailAddress, iaik.x509.X509Certificate[] certificates, iaik.x509.X509Certificate signerCertificate, iaik.x509.X509Certificate encryptionCertOfSender, iaik.asn1.structures.AlgorithmID keyEncAlg, java.security.PrivateKey signerKey, iaik.asn1.structures.AlgorithmID hashAlgorithm, iaik.asn1.structures.AlgorithmID signatureAlgorithm) throws SMimeException
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 messagehashAlgorithm
- the hash algorithm to be use (e.g. AlgorithmID.sha256)signatureAlgorithm
- the signature algorithm to be used (e.g. AlgorithmID.rsaEncryption)SMimeException
- if the signer setup failspublic void addTo(java.lang.String realName, java.lang.String emailAddress, iaik.x509.X509Certificate recipientCert, iaik.asn1.structures.AlgorithmID keyEncAlg)
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 senderkeyEncAlg
- the algorithm used to encrypt the temporary symmetric key for a receiverpublic void addCc(java.lang.String realName, java.lang.String emailAddress, iaik.x509.X509Certificate recipientCert, iaik.asn1.structures.AlgorithmID keyEncAlg)
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 senderkeyEncAlg
- the algorithm used to encrypt the temporary symmetric key for a receiverpublic void addBcc(java.lang.String realName, java.lang.String emailAddress, iaik.x509.X509Certificate recipientCert, iaik.asn1.structures.AlgorithmID keyEncAlg)
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 senderkeyEncAlg
- the algorithm used to encrypt the temporary symmetric key for a receiverpublic void addAdditionalField(java.lang.String line)
addAdditionalField
in class iaik.utils.SmtpMailer
line
- a complete linepublic void addDate()
public void addDate(java.util.Date date)
date
- the datepublic void setEncryption(iaik.asn1.structures.AlgorithmID contentEncAlg, int keyLength)
Examples for possible encryption algorithms are:
contentEncAlg
- the algorithm for encrypting the contentkeyLength
- the key length for RC2public void setSecureRandom(java.security.SecureRandom random)
random
- a random number generatorpublic boolean sendMail() throws iaik.utils.SmtpException
sendMail
in class iaik.utils.SmtpMailer
iaik.utils.SmtpException
- if there is a problem while sending the EMailSmtpMailer
public java.lang.String toString()
toString
in class iaik.utils.SmtpMailer