EMailMessage.java revision 0e14e9936c06004713c19ec22098978c38ce7766
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
* Portions copyright 2015 Edan Idzerda
*/
/**
* This class defines an e-mail message that may be sent to one or more
* recipients via SMTP. This is a wrapper around JavaMail to make this process
* more convenient and fit better into the Directory Server framework.
*/
mayInstantiate=true,
mayExtend=false,
mayInvoke=true)
public final class EMailMessage
{
/** The addresses of the recipients to whom this message should be sent. */
/** The set of attachments to include in this message. */
/** The MIME type for the message body. */
private String bodyMIMEType;
/** The address of the sender for this message. */
/** The subject for the mail message. */
/** The body for the mail message. */
private LocalizableMessageBuilder body;
/**
* Creates a new e-mail message with the provided information.
*
* @param sender The address of the sender for the message.
* @param recipient The address of the recipient for the message.
* @param subject The subject to use for the message.
*/
{
body = new LocalizableMessageBuilder();
attachments = new LinkedList<>();
bodyMIMEType = "text/plain";
}
/**
* Creates a new e-mail message with the provided information.
*
* @param sender The address of the sender for the message.
* @param recipients The addresses of the recipients for the message.
* @param subject The subject to use for the message.
*/
{
this.recipients = recipients;
body = new LocalizableMessageBuilder();
attachments = new LinkedList<>();
bodyMIMEType = "text/plain";
}
/**
* Retrieves the sender for this message.
*
* @return The sender for this message.
*/
{
return sender;
}
/**
* Specifies the sender for this message.
*
* @param sender The sender for this message.
*/
{
}
/**
* Retrieves the set of recipients for this message. This list may be
* directly manipulated by the caller.
*
* @return The set of recipients for this message.
*/
{
return recipients;
}
/**
* Specifies the set of recipients for this message.
*
* @param recipients The set of recipients for this message.
*/
{
this.recipients = recipients;
}
/**
* Adds the specified recipient to this message.
*
* @param recipient The recipient to add to this message.
*/
{
}
/**
* Retrieves the subject for this message.
*
* @return The subject for this message.
*/
public String getSubject()
{
return subject;
}
/**
* Specifies the subject for this message.
*
* @param subject The subject for this message.
*/
{
}
/**
* Retrieves the MIME Type for the body of this message.
*
* @return The MIME Type for this message.
*/
public String getBodyMIMEType()
{
return bodyMIMEType;
}
/**
* Specifies the MIME Type for the body of this message.
*
* @param bodyMIMEType The MIME Type for this message.
*/
{
this.bodyMIMEType = bodyMIMEType;
}
/**
* Retrieves the body for this message. It may be directly manipulated by the
* caller.
*
* @return The body for this message.
*/
public LocalizableMessageBuilder getBody()
{
return body;
}
/**
* Specifies the body for this message.
*
* @param body The body for this message.
*/
{
}
/**
* Specifies the body for this message.
*
* @param body The body for this message.
*/
{
}
/**
* Appends the provided text to the body of this message.
*
* @param text The text to append to the body of the message.
*/
{
}
/**
* Retrieves the set of attachments for this message. This list may be
* directly modified by the caller if desired.
*
* @return The set of attachments for this message.
*/
{
return attachments;
}
/**
* Adds the provided attachment to this mail message.
*
* @param attachment The attachment to add to this mail message.
*/
{
}
/**
* Adds an attachment to this mail message with the provided text.
*
* @param attachmentText The text to include in the attachment.
*
* @throws MessagingException If there is a problem of some type with the
* attachment.
*/
throws MessagingException
{
}
/**
* Adds the provided attachment to this mail message.
*
* @param attachmentFile The file containing the attachment data.
*
* @throws MessagingException If there is a problem of some type with the
* attachment.
*/
throws MessagingException
{
}
/**
* Attempts to send this message to the intended recipient(s). This will use
* the mail server(s) defined in the Directory Server mail handler
* configuration. If multiple servers are specified and the first is
* unavailable, then the other server(s) will be tried before returning a
* failure to the caller.
*
* @throws MessagingException If a problem occurred while attempting to send
* the message.
*/
public void send()
throws MessagingException
{
}
/**
* Attempts to send this message to the intended recipient(s). If multiple
* servers are specified and the first is unavailable, then the other
* server(s) will be tried before returning a failure to the caller.
*
* @param mailServerPropertySets A list of property sets providing
* information about the mail servers to use
* when sending the message.
*
* @throws MessagingException If a problem occurred while attempting to send
* the message.
*/
throws MessagingException
{
// Get information about the available mail servers that we can use.
{
// Get a session and use it to create a new message.
// Add the sender address. If this fails, then it's a fatal problem we'll
// propagate to the caller.
try
{
}
catch (MessagingException me)
{
}
// Add the recipient addresses. If any of them fail, then that's a fatal
// problem we'll propagate to the caller.
{
try
{
}
catch (MessagingException me)
{
}
}
// If we have any attachments, then the whole thing needs to be
// multipart. Otherwise, just set the text of the message.
if (attachments.isEmpty())
{
}
else
{
{
}
}
// Try to send the message. If this fails, it can be a complete failure
// or a partial one. If it's a complete failure then try rolling over to
// the next server. If it's a partial one, then that likely means that
// the message was sent but one or more recipients was rejected, so we'll
// propagate that back to the caller.
try
{
return;
}
catch (SendFailedException sfe)
{
// We'll ignore this and hope that another server is available. If not,
// then at least save the exception so that we can throw it if all else
// fails.
if (sendException == null)
{
sendException = sfe;
}
}
// FIXME -- Are there any other types of MessagingException that we might
// want to catch so we could try again on another server?
}
// If we've gotten here, then we've tried all of the servers in the list and
// still failed. If we captured an earlier exception, then throw it.
// Otherwise, throw a generic exception.
if (sendException == null)
{
}
else
{
throw sendException;
}
}
/**
* Provide a command-line mechanism for sending an e-mail message via SMTP.
*
* @param args The command-line arguments provided to this program.
*/
{
description, false);
try
{
null,
}
catch (ArgumentException ae)
{
}
try
{
}
catch (ArgumentException ae)
{
}
{
return;
}
{
Properties p = new Properties();
p.setProperty(SMTP_PROPERTY_HOST, s);
}
{
try
{
if (! f.exists())
{
}
while (true)
{
{
break;
}
}
}
catch (Exception e)
{
getExceptionMessage(e)));
}
}
if (attachFile.isPresent())
{
{
if (! f.exists())
{
}
try
{
message.addAttachment(f);
}
catch (Exception e)
{
getExceptionMessage(e)));
}
}
}
try
{
}
catch (Exception e)
{
getExceptionMessage(e)));
}
}
}