MDi JLib
1.7.4

com.microdeveloper.util.net.mail
Class JMail

java.lang.Object
  extended by com.microdeveloper.util.net.mail.JMail

public class JMail
extends Object

Provides functions for sending E-mail with various MIME type encodings and optional attachements. The class can be used in static form where the variables are all passed in, or as a bean where the properties of the E-mail are first set, then the message is sent.

Since:
1.3
Version:
1.2
Author:
MicroDeveloper, Inc.

Field Summary
static String DEFAULTHOST
           
static String HTML
           
static String HTML_MIMETYPE
           
static String MULTIPART
           
static String PLAIN
           
static String PLAIN_MIMETYPE
           
 
Constructor Summary
JMail()
          Creates a new JMail object using the properties defined in the defaut mail properties file
 
Method Summary
 String getAttachment()
          Returns the currently attachment.
 String getBcc()
          Returns the currently established BCC list
 String getBody()
          Returns the message body
 String getCc()
          Returns the currently established CC list
 String getCharacterSet()
          Returns the current character set being used
 javax.mail.BodyPart getFileBodyPart(String filename, String contentType)
          Fetches a particular body part from the current message.
 String getFrom()
          Returns the currently established from address
 String getHost()
          Returns the currenty SMTP host.
 String getHtmlBody()
          Returns the HTML message body
 JMailInterface getjMailInterface()
          Returns the interfacce to the class
 String getMailPropertiesPath()
          Fetches the currently established properties path.
 String getPlainBody()
          Returns the plain message body
 String getSmtpHost()
          Returns the currenty SMTP host
 String getSubject()
          Returns the message subject
 String getTo()
          Returns the currently established To list
 boolean isDebugging()
          Returns the current state of debugging.
 void sendMail()
          Sends the current mail message using a plain text format or the format the mime type is currently set to
static void sendMail(String from, String to, String cc, String bcc, String subject, String body)
          Sends a plain text message.
static void sendMail(String from, String to, String cc, String bcc, String subject, String body, String mimeType)
          Sends a message of the type specified in mimeType.
static void sendMail(String from, String to, String cc, String bcc, String subject, String body, String mimeType, String host, boolean debug)
          Sends a plain text message.
 void sendMultipart()
          Sends the current mail message using a multipart format.
static void sendMultipart(String from, String to, String cc, String bcc, String subject, String body, String altBody)
          Sends a multi-part E-mail message.
static void sendMultipart(String from, String to, String cc, String bcc, String subject, String body, String altBody, String attachments, String host, boolean debug)
          Sends a multi-part E-mail message.
 void setBcc(String bcc)
          Sets the message recipients in the BCC (blind carbon copy) segment of the E-mail.
 void setBody(String body)
          Sets the body segment of the E-mail using the format defined in the mime type property.
 void setCc(String cc)
          Sets the message recipients in the CC (carbon copy) segment of the E-mail.
 void setCharacterSet(String value)
          Sets the character set used for the message
 void setDebug(boolean debug)
          Sets the debug state
 void setFrom(String from)
          Sets the message senders in the From segment of the E-mail.
 void setHost(String host)
          Sets the SMTP host using either an IP address or a host name.
 void setHtmlBody(String htmlBody)
          Sets the HTML body segment of the E-mail for rich text messages.
 void setMailPropertiesPath(String value)
          Sets the name and location of the JMail properties file
 void setMimeType(String value)
          Sets the specific mime type for the message
 void setPlainBody(String plainBody)
          Sets the text body segment of the E-mail using plain text.
 void setSmtpHost(String host)
          Sets the SMTP host using either an IP address or a host name.
 void setSubject(String subject)
          Sets the subject of the message.
 void setTo(String to)
          Sets the message recipients in the To segment of the E-mail.
 void setType(String type)
          Sets the type of message.
static String vectorToString(Vector v)
          Converts the given vector to a String object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULTHOST

public static final String DEFAULTHOST
See Also:
Constant Field Values

HTML

public static final String HTML
See Also:
Constant Field Values

HTML_MIMETYPE

public static final String HTML_MIMETYPE
See Also:
Constant Field Values

MULTIPART

public static final String MULTIPART
See Also:
Constant Field Values

PLAIN

public static final String PLAIN
See Also:
Constant Field Values

PLAIN_MIMETYPE

public static final String PLAIN_MIMETYPE
See Also:
Constant Field Values
Constructor Detail

JMail

public JMail()
Creates a new JMail object using the properties defined in the defaut mail properties file

Method Detail

sendMail

public static void sendMail(String from,
                            String to,
                            String cc,
                            String bcc,
                            String subject,
                            String body)
Sends a plain text message. The given e-mail addresses may be separated by commas and will be parsed when sent. The content should not contain characters that are not legal for text based messages or the E-mail may be rejected. The properties for the mail server and mime types are set via the default properties but may be overriden prior to a call to this variant. Alternatively you can use the other variant that allows the host name to be passed in at the time of call.

Parameters:
from - from sender e-mail address(es)
to - to recipient e-mail address(es)
cc - cc recipient e-mail address(es)
bcc - bcc recipient e-mail address(es)
subject - subject of the message
body - content of the message (message body)

sendMail

public static void sendMail(String from,
                            String to,
                            String cc,
                            String bcc,
                            String subject,
                            String body,
                            String mimeType)
Sends a message of the type specified in mimeType. The given e-mail addresses may be separated by commas and will be parsed when sent. The content should not contain characters that are not legal for text based messages or the E-mail may be rejected. The properties for the mail server and mime types are set via the default properties but may be overriden prior to a call to this variant. Use the constants to specify alternative types of pass in the String of the mime type directly.

Parameters:
from - from sender e-mail address(es)
to - to recipient e-mail address(es)
cc - cc recipient e-mail address(es)
bcc - bcc recipient e-mail address(es)
subject - subject of the message
body - content of the message (message body)
mimeType - mime type to use when encoding the message

sendMail

public static void sendMail(String from,
                            String to,
                            String cc,
                            String bcc,
                            String subject,
                            String body,
                            String mimeType,
                            String host,
                            boolean debug)
Sends a plain text message. The given e-mail addresses may be separated by commas and will be parsed when sent. The content should not contain characters that are not legal for text based messages or the E-mail may be rejected. The properties for the mail server and mime types are set via the default properties but may be overriden prior to a call to this variant. Alternatively you can use the other variant that allows the host name to be passed in at the time of call.

Parameters:
from - from sender e-mail address(es)
to - to recipient e-mail address(es)
cc - cc recipient e-mail address(es)
bcc - bcc recipient e-mail address(es)
subject - subject of the message
body - content of the message (message body)
mimeType - mime type to use when encoding the message
host - smtp mail server host IP or name
debug - set to true for debugging output or false (default)

sendMultipart

public static void sendMultipart(String from,
                                 String to,
                                 String cc,
                                 String bcc,
                                 String subject,
                                 String body,
                                 String altBody)
                          throws IOException,
                                 javax.mail.MessagingException
Sends a multi-part E-mail message. A multi-part E-mail contains an HTML and text version in the same E-mail. This is useful when you would like to send a rich text formatted message but don't know if the mail client will be able to handle it, such as when read from a unix terminal or hand held device. In the later case, a multi-part e-mail will be interpretted by the client in the way the mail client is configured. If the mail client supports HTML mail, and the configuration dictates that it can be used, the HTML message is shown, otherwise the text version of the message is displayed. In clients that cannot understand HTML, only the text portion of the message is displayed. This is a standard all mail clients follow from Microsoft Outlook to Unix Pine variants. If the alternate body is not given, the message is assumed to be a text message.

Parameters:
from - from sender e-mail address(es)
to - to recipient e-mail address(es)
cc - cc recipient e-mail address(es)
bcc - bcc recipient e-mail address(es)
subject - subject of the message
body - content of the message as text (message body)
altBody - content of the message as HTML (rich text message body)
Throws:
javax.mail.internet.AddressException - thrown if any of the addresses are improperly formatted
FileNotFoundException - thrown when the configuration file cannot be found
IOException - thrown when the IO cannot be opened or accessed
javax.mail.MessagingException - thrown if the mail message cannot be sent for some reason

sendMultipart

public static void sendMultipart(String from,
                                 String to,
                                 String cc,
                                 String bcc,
                                 String subject,
                                 String body,
                                 String altBody,
                                 String attachments,
                                 String host,
                                 boolean debug)
                          throws IOException,
                                 javax.mail.MessagingException
Sends a multi-part E-mail message. A multi-part E-mail contains an HTML and text version in the same E-mail. This is useful when you would like to send a rich text formatted message but don't know if the mail client will be able to handle it, such as when read from a unix terminal or hand held device. In the later case, a multi-part e-mail will be interpretted by the client in the way the mail client is configured. If the mail client supports HTML mail, and the configuration dictates that it can be used, the HTML message is shown, otherwise the text version of the message is displayed. In clients that cannot understand HTML, only the text portion of the message is displayed. This is a standard all mail clients follow from Microsoft Outlook to Unix Pine variants. If the alternate body is not given, the message is assumed to be a text message.

This variant also allows the user to specify the host, set debug settings and add attachments*.

*not enabled

Parameters:
from - from sender e-mail address(es)
to - to recipient e-mail address(es)
cc - cc recipient e-mail address(es)
bcc - bcc recipient e-mail address(es)
subject - subject of the message
body - content of the message as text (message body)
altBody - content of the message as HTML (rich text message body)
attachments - attachment file list
host - smtp mail server host IP address or name
debug - set to true to enable debugging output
Throws:
javax.mail.internet.AddressException - thrown if any of the addresses are improperly formatted
FileNotFoundException - thrown when the configuration file cannot be found
IOException - thrown when the IO cannot be opened or accessed
javax.mail.MessagingException - thrown if the mail message cannot be sent for some reason
javax.mail.internet.AddressException - thrown if any of the addresses are improperly formatted
FileNotFoundException - thrown when the configuration file cannot be found
IOException - thrown when the IO cannot be opened or accessed
javax.mail.MessagingException - thrown if the mail message cannot be sent for some reason

vectorToString

public static String vectorToString(Vector v)
Converts the given vector to a String object

Parameters:
v - vector to convert
Returns:
String result

getjMailInterface

public JMailInterface getjMailInterface()
Returns the interfacce to the class

Returns:
JMailInterface

sendMail

public void sendMail()
Sends the current mail message using a plain text format or the format the mime type is currently set to


sendMultipart

public void sendMultipart()
Sends the current mail message using a multipart format.


setTo

public void setTo(String to)
Sets the message recipients in the To segment of the E-mail. Multiple E-mail addresses should be comma separated.

Parameters:
to - one or more E-mail addresses

setFrom

public void setFrom(String from)
Sets the message senders in the From segment of the E-mail. Multiple E-mail addresses should be comma separated.

Parameters:
from - one or more E-mail addresses

setCc

public void setCc(String cc)
Sets the message recipients in the CC (carbon copy) segment of the E-mail. Multiple E-mail addresses should be comma separated.

Parameters:
cc - one or more E-mail addresses

setBcc

public void setBcc(String bcc)
Sets the message recipients in the BCC (blind carbon copy) segment of the E-mail. Multiple E-mail addresses should be comma separated.

Parameters:
bcc - one or more E-mail addresses

setHtmlBody

public void setHtmlBody(String htmlBody)
Sets the HTML body segment of the E-mail for rich text messages.

Parameters:
htmlBody - the body of the message using HTML formatting

setPlainBody

public void setPlainBody(String plainBody)
Sets the text body segment of the E-mail using plain text.

Parameters:
plainBody - the body of the message using plain text formatting

setBody

public void setBody(String body)
Sets the body segment of the E-mail using the format defined in the mime type property.

Parameters:
body - the body of the message

setSubject

public void setSubject(String subject)
Sets the subject of the message. Usually less than 100 characters, try not to exceed that length.

Parameters:
subject - text to use for the subject

setType

public void setType(String type)
Sets the type of message. Use the constants to properly set the type.

Parameters:
type - one of the constant types

setSmtpHost

public void setSmtpHost(String host)
Sets the SMTP host using either an IP address or a host name.

Parameters:
host - host name or address

setHost

public void setHost(String host)
Sets the SMTP host using either an IP address or a host name. This is the same as using setSmtpHost(String host).

Parameters:
host - host name or address

setMailPropertiesPath

public void setMailPropertiesPath(String value)
Sets the name and location of the JMail properties file

Parameters:
value - name and path to the JMail properties file

setMimeType

public void setMimeType(String value)
Sets the specific mime type for the message

Parameters:
value - a supported mimetype

setCharacterSet

public void setCharacterSet(String value)
Sets the character set used for the message

Parameters:
value - a supported character set

setDebug

public void setDebug(boolean debug)
Sets the debug state

Parameters:
debug - true for debugging otherwise false

getFileBodyPart

public javax.mail.BodyPart getFileBodyPart(String filename,
                                           String contentType)
                                    throws javax.mail.MessagingException
Fetches a particular body part from the current message. This is pertinent to multipart messages only with attachements.

Parameters:
filename - filename of the attachment
contentType - content type to fetch
Returns:
BodyPart segment
Throws:
javax.mail.MessagingException - thrown if the body part cannot be fetched

getMailPropertiesPath

public String getMailPropertiesPath()
Fetches the currently established properties path.

Returns:
path to the properties file

getTo

public String getTo()
Returns the currently established To list

Returns:
list of e-mail addresses

getFrom

public String getFrom()
Returns the currently established from address

Returns:
from address

getCc

public String getCc()
Returns the currently established CC list

Returns:
list of e-mail addresses

getBcc

public String getBcc()
Returns the currently established BCC list

Returns:
list of e-mail addresses

getAttachment

public String getAttachment()
Returns the currently attachment. Not implemented yet.

Returns:
the attachment

isDebugging

public boolean isDebugging()
Returns the current state of debugging.

Returns:
true if debugging otherwise false

getCharacterSet

public String getCharacterSet()
Returns the current character set being used

Returns:
the character set being used

getSubject

public String getSubject()
Returns the message subject

Returns:
message subject

getBody

public String getBody()
Returns the message body

Returns:
message body

getPlainBody

public String getPlainBody()
Returns the plain message body

Returns:
plain message body

getHtmlBody

public String getHtmlBody()
Returns the HTML message body

Returns:
HTML message body

getSmtpHost

public String getSmtpHost()
Returns the currenty SMTP host

Returns:
SMTP host

getHost

public String getHost()
Returns the currenty SMTP host. This is the same as getSmtpHost().

Returns:
SMTP host

MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.