w3c//dtd html 4.0 transitional//en"> : Class Mailer

cylikon.MailerBean
Class Mailer

java.lang.Object
  |
  +--cylikon.MailerBean.Mailer
All Implemented Interfaces:
java.io.Serializable

public class Mailer
extends java.lang.Object
implements java.io.Serializable
Mailer class.
See Also:
Serialized Form


Simple Example:

cylikon.MailerBean.Mailer mail=new cylikon.MailerBean.Mailer();
 
 // set the TO field
 mail.addTO("myFriend@myhost.com");
 // set the FROM field
 mail.setFROM("me@someplace.com")
 mail.addCC("myOtherFriend@myhost.com");
 mail.addBCC("PrivateInvestigator@secrethost.com");
 mail.setSubject("Hi Friend!");
 mail.setMessage("Just wanted to say Hi, and see how you were doing....");
 
 //Now, let's get status string which will tell us if the mail has been sent..
 // call the ConstructAndSend(boolean ConstructOnly_NoSend)  method and
 // set ConstructOnly_NoSend = FALSE, because we DO want the message sent

 String status=mail.ConstructAndSend(false);
 System.out.println(status);
 
 // If you don't to create the string "status" you could just call the method,
 // ConstructAndSend(false), then check the boolean to see if it was sent...
 boolean mailCheck=mail.MailSent();

  if (mailCheck) {System.out.print("Mail was sent");} else {System.out.print("Sending failed");}
 

SEE THE MailerBeanExample.txt file for hints on using more advanced features.



 

Constructor Summary
Mailer()

 
Method Summary
 void addAttachment(java.io.File data_to_be_attached, java.lang.String mimetype, java.lang.String file_name, java.lang.String file_description)

Add attachments by providing a File, mime type, file name, and short description.

Example: 
   mail.addAttachment(new File("C:\\pic.gif") , "image/gif","pic.gif", "My summer pictures"); 

This method should be VERY convienient for use in GUIs which will be using a JFileChooser to add attachments.  In addition, using URLs for attachments should be quite easy as well, since the developer do the following:
   URL u = new URL("http://www.somesite.com/mypic.gif"); 
   File f = u.getFile();

 void addBCC(java.lang.String new_BCC)

Adds individual Blind Carbon Copy addresses to the MimeMessage. 
Example:  mail.addBCC("AFriend@mail.com"); 

 void addCC(java.lang.String new_CC)

Adds individual Carbon Copy addresses to the MimeMessage. 
Example:  mail.addCC("AnotherFriend@mail.com"); 

 void addTO(java.lang.String new_TO)

Adds individual TO addresses to the MimeMessage. 
Example:  mail.addTO("AFriend@mail.com"); 

 void ClearOldMessage()

Invoke this method after a message has been sent, or otherwise no longer needed.  This method releases all resources no longer in use, and creates a clean slate for sending the next MimeMessage.    This method removes all attachments, all addresses, clears all messages and headers, and creates a fresh new Session and MimeMessage.  This method is automatically invoked if a message is successfully sent.

 java.lang.String ConstructAndSend(boolean ConstructOnly_NoSend)

This method is responsible for, at a minimum, constructing the MimeMessage to be sent, and should be called after *all* other methods affecting this message have been called.  All necessary Set and Add methods need to be called prior to attempting to send.

The ConstructOnly_NoSend  boolean allows this method to operate in one of two modes.  

IMPORTANT:
If ConstructOnly_NoSend is set to FALSE, the message WILL be constructed and SENT as normal, provided "all systems go".  

If ConstructOnly_NoSend is set to TRUE, the message will NOT be sent.  Instead, it will be constructed WITHOUT sending.  This would allow you to make a call to getRawMimeMessage(). NOTE: This is the *only* instance in which getRawMimeMessage() should be called.

This method returns a string, which , if read, will be determined by either the getMailSuccessMessage, or any one of the "warning" methods, suchs as getNoFROMWarning(), or getBadTOWarning.  It is up to use to make use of the appropriate Set methods to adapt these messages to your liking.  If you simply want a basic "yes" or "no" answer as to whether sending succeeded, invoke the MailSent() method, which returns a boolean.

 java.lang.String getAddressException()

Returns the the message to be shown in the event a javax.mail.internet.AddressException occurs with one or more addresses.

 java.lang.String getBadBCCWarning()

Gets the message to be shown the client if an invalid BCC address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 java.lang.String getBadCCWarning()

Gets the message to be shown the client if an invalid CC address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 java.lang.String getBadFROMWarning()

Gets the message to be shown the client if an invalid FROM address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 java.lang.String getBadTOWarning()

Gets the message to be shown the client if an invalid TO address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 java.util.Vector getBcc()

Returns a Vector of all BCC addresses currently associated with this MimeMessage.  The method will return an empty Vector (not a null), if no address have yet been set.

 java.util.Vector getCc()

Returns a Vector of all CC addresses currently associated with this MimeMessage.  The method will return an empty Vector (not a null), if no address have yet been set.

 java.lang.String getDefaultMimeType()

Returns the default message ime type currently asoociated with this message.

CHANGING THE MIME-TYPE (for HTML Mail etc.): 
  mail.setDefaultMimeType("text/html"); 
  String dmt = mail.getDefaultMimeType();

The 4 supported formats are: text/plain, text/html, multipart/mixed, message/rfc822

Please note, that as of this writing, only JavaMail version 1.1.3 and above supports text/html. 

 java.lang.String getExtraHeaderLine()

Returns  any extra mail header line you have added to the current message.  NOTE: Not all mail clients show message headers.

 java.lang.String getFrom()

Returns the FROM address for the current MimeMessage, or null if the value has not been set.

 boolean getGreenLight()

Not implimented.

 java.lang.String getMailSuccessMessage()

Returns the text message to be shown the client upon successful sending of a message.

 java.lang.String getMessage()

Gets the text message to be included in  the current MimeMessage, or null if no message has been set.

 java.lang.String getMessageException()

Gets the message to be displayed to the client in the event  a javax.mail.MessagingException occurs.

 javax.mail.Session getMessageSession()

Returns the Session object used to construct the current MimeMessage.

 java.lang.String getMSMailPriority()

Returns the mail priority understood by most Microsoft mail clients. 
   Examples: 
     mail.setMSMailPriority("high"); 
     mail.setMSMailPriority("low"); 
     mail.setMSMailPriority("normal"); 

    String pr = mail.getMSMailPriority();

 java.lang.String getNetscapeMailPriority()

Returns the mail priority understood by most Netscape mail clients. 
   Examples: 
     mail.setNetscapeMailPriority("1");   // high priority
     mail.setNetscapeMailPriority("2");   // lower
     mail.setNetscapeMailPriority("3");    // low

    String pr = mail.getNetscapeMailPriority();

 java.lang.String getNoFROMWarning()

Gets the message to be shown the client if no FROM address has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 java.lang.String getNoMessageWarning()

Gets the message to be shown the client if no message body  has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 java.lang.String getNoTOWarning()

Gets the message to be shown the client if no TO address has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 javax.mail.internet.MimeMessage getRawMimeMessage()

This method should only be called after ConstructAndSend(true) has been called.
This method should NOT be called if ConstructAndSend(false) has been called, since the current message is cleared from memory immediately after it has been sent.  This method should also NOT be called before calling ConstructAndSend(), since no MimeMessages are constructed until this a request has been made to send the message.

 java.lang.String getSenderInfo()

Returns any extra sender information you have chosen to attach to the bottom of the current message.

 java.util.Properties getSessionProperties()

Returns the Properties object used to construct the Session for the current MimeMessage.

 java.lang.String getSMTP_Host()

Returns the SMTP Host  to be used for sending the current message.

 int getSMTP_Port()

Returns the SMTP Port  to be used for sending the current message.

 java.lang.String getSubject()

Returns the Subject header associated with the current message.

 java.util.Vector getTo()

Returns a Vector of all TO addresses currently associated with this MimeMessage.  The method will return an empty Vector (not a null), if no address have yet been set.

 java.lang.String getXMailerHeader()

Gets the X-Mailer header for this message.  Generally, this header is used to provide information about the kind of software used to send the message.  NOTE: Some mail client programs do not show these headers.

 java.util.Vector listAttachments()

Returns a collection of java.io.File objects included in this message's attachments.
      Vector v = mail.listAttachments(): 
      File.myFile = (File) v.elementAt(2); 

 java.util.Vector listAttachmentsDescriptions()

Returns a collection of Strings which are the file descriptions associated with  this message's attachments.
      Vector v = mail.listAttachments(): 
      String string = v.elementAt(2).toString(); 

 java.util.Vector listAttachmentsMimes()

Returns a collection of Strings which are the file mime types associated with  this message's attachments.
      Vector v = mail.listAttachments(): 
      String string = v.elementAt(2).toString(); 

 java.util.Vector listAttachmentsNames()

Returns a collection of Strings which are the file file names associated with  this message's attachments.
      Vector v = mail.listAttachments(): 
      String string = v.elementAt(2).toString(); 

 boolean MailSent()

A boolean to be called after ConstructAndSend() to determine whether the message was actually sent.  This method can be invoked if you wish to avoid having to read the String returned by the ConstructAndSend() method.

 void removeAllAttachments()

Removes all attachments from the current message.

 java.lang.String removeAttachment(int attIndex)

Removes the attachment located at index attIndex from this message's Vector of attachments.  This should provide an easy to follow parallel for attachments managed through JComboBox or JList  interfaces.

 void setAddressException(java.lang.String aes)

Sets the the message to be shown in the event a javax.mail.internet.AddressException occurs with one or more addresses.

 void setBadBCCWarning(java.lang.String bbw)

Sets the message to be shown the client if an invalid BCC address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 void setBadCCWarning(java.lang.String bcw)

Sets the message to be shown the client if an invalid CC address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message. This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 void setBadFROMWarning(java.lang.String bdfw)

Sets the message to be shown the client if an invalid FROM address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message. This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 void setBadTOWarning(java.lang.String btw)

Sets the message to be shown the client if an invalid TO address has been provided, and an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message. This method helps avoid unnecessary AddressExceptions and provides the client with a specific explanation for why his/her email was rejected.

 void setBcc(java.util.Vector Bcc)

Adds the specified Vector to the message's list of BCC addresses: 

 Vector adds=new Vector();
 adds.addElement("myFriend@aol.com");
 adds.addElement("YourFriend@aol.com");
 mailer.setBcc(adds);   //That's it!

This method could prove very useful for easily parsing a long list of BCC addresses into the message's BCC  field.

 void setCc(java.util.Vector Cc)

Adds the specified Vector to the message's list of CC addresses: 

 Vector adds=new Vector();
 adds.addElement("myFriend@aol.com");
 adds.addElement("YourFriend@aol.com");
 mailer.setCc(adds);   //That's it!

This method could prove very useful for easily parsing a long list of CC addresses into the message's CC  field.

 void setDefaultMimeType(java.lang.String dmt)

CHANGING THE MIME-TYPE (for HTML Mail etc.): 
  mail.setDefaultMimeType("text/html"); 
        The 4 supported formats are:   text/plain, text/html, multipart/mixed, message/rfc822

Please note, that as of this writing, only JavaMail version 1.1.3 and above supports text/html.

 void setExtraHeaderLine(java.lang.String exl)

Adds an extra mail header line of your choose to the message.  NOTE: Not all mail clients show message headers.

 void setFrom(java.lang.String fr)

Sets the FROM field of the mail message. 

 void setGreenLight(boolean green)

 Not implimented.

 void setMailSuccessMessage(java.lang.String msm)

Sets the text message to be shown the client upon successful sending of a message.

 void setMessage(java.lang.String msg)

Sets the text message to be included in the current MimeMessage.

 void setMessageException(java.lang.String mes)

Sets the message to be displayed to the client in the event  a javax.mail.MessagingException occurs.

 void setMSMailPriority(java.lang.String msmail)

Sets the mail priority understood by most Microsoft mail clients. 
   Examples: 
     mail.setMSMailPriority("high"); 
     mail.setMSMailPriority("low"); 
     mail.setMSMailPriority("normal"); 

 void setNetscapeMailPriority(java.lang.String nmail)

Sets the mail priority understood by most Netscape mail clients. 
   Examples: 
     mail.setNetscapeMailPriority("1");   // high priority
     mail.setNetscapeMailPriority("2");   // lower
     mail.setNetscapeMailPriority("3");    // low

 void setNoFROMWarning(java.lang.String nfw)

Sets the message to be shown the client if no FROM address has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 void setNoMessageWarning(java.lang.String nmw)

Sets the message to be shown the client if no message body has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 void setNoTOWarning(java.lang.String ntw)

Sets the message to be shown the client if no TO address has been provided, but an attempt was made to send the mail.  If this error occurs, the ConstructAndSend() method will return  THIS string instead of a success message.

 void setSenderInfo(java.lang.String si)

Sets any extra sender information you have choose to attach to the bottom of the current message.  This method is useful for, say, attaching information about the sender's IP address, or web browser type to the message.  The information  will appear beloew the main message body.  This value is NOT null by default, so if you don't wish to see a default message below the email's body, you should adjust this information to your liking.

 void setSessionProperties(java.util.Properties prp, javax.mail.Authenticator au)

     ALLOWS FOR OVERIDDING THE SESSION COMPLETELY / USING AUTHENTICATORS: 
     You can then create your own properties, and Authenticator if necessary:

     mail.setSessionProperties(MyProperties, MyAuthenticator); 

     This method will automatically create a new Session to accommodate your changes.
     If the don't want to use an Authenticator, but just want to set the Properties, 
     set Authenticator to null.  NOTE: Properties should *never* be set to null.

 void setSMTP_Host(java.lang.String hst)

Sets the SMTP host from which to send the email message.

 void setSMTP_Port(int prt)

Sets the SMTP port from which to send the email message.

 void setSubject(java.lang.String subj)

Sets the message's Subject field.

 void setTo(java.util.Vector To)

Adds the specified Vector to the message's list of TO addresses: 

 Vector adds=new Vector();
 adds.addElement("myFriend@aol.com");
 adds.addElement("YourFriend@aol.com");
 mailer.setTo(adds);   //That's it!

This method could prove very useful for easily parsing a long list of TO addresses into the message's TO field.

 void setXMailerHeader(java.lang.String xmail)

Sets the X-Mailer header for this message.  Generally, this header is used to provide information about the kind of software used to send the message.  NOTE: Some mail client programs do not show these headers.

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

Mailer

public Mailer()
Method Detail

MailSent

public boolean MailSent()

setSMTP_Host

public void setSMTP_Host(java.lang.String hst)

getSMTP_Host

public java.lang.String getSMTP_Host()

setSMTP_Port

public void setSMTP_Port(int prt)

getSMTP_Port

public int getSMTP_Port()

setTo

public void setTo(java.util.Vector To)

getTo

public java.util.Vector getTo()

setFrom

public void setFrom(java.lang.String fr)

getFrom

public java.lang.String getFrom()

setCc

public void setCc(java.util.Vector Cc)

getCc

public java.util.Vector getCc()

setBcc

public void setBcc(java.util.Vector Bcc)

getBcc

public java.util.Vector getBcc()

setSubject

public void setSubject(java.lang.String subj)

getSubject

public java.lang.String getSubject()

setMessage

public void setMessage(java.lang.String msg)

getMessage

public java.lang.String getMessage()

setBadFROMWarning

public void setBadFROMWarning(java.lang.String bdfw)

getBadFROMWarning

public java.lang.String getBadFROMWarning()

setBadTOWarning

public void setBadTOWarning(java.lang.String btw)

getBadTOWarning

public java.lang.String getBadTOWarning()

setBadCCWarning

public void setBadCCWarning(java.lang.String bcw)

getBadCCWarning

public java.lang.String getBadCCWarning()

setBadBCCWarning

public void setBadBCCWarning(java.lang.String bbw)

getBadBCCWarning

public java.lang.String getBadBCCWarning()

setNoFROMWarning

public void setNoFROMWarning(java.lang.String nfw)

getNoFROMWarning

public java.lang.String getNoFROMWarning()

setNoMessageWarning

public void setNoMessageWarning(java.lang.String nmw)

getNoMessageWarning

public java.lang.String getNoMessageWarning()

setNoTOWarning

public void setNoTOWarning(java.lang.String ntw)

getNoTOWarning

public java.lang.String getNoTOWarning()

setXMailerHeader

public void setXMailerHeader(java.lang.String xmail)

getXMailerHeader

public java.lang.String getXMailerHeader()

setGreenLight

public void setGreenLight(boolean green)

getGreenLight

public boolean getGreenLight()

setMSMailPriority

public void setMSMailPriority(java.lang.String msmail)

getMSMailPriority

public java.lang.String getMSMailPriority()

setNetscapeMailPriority

public void setNetscapeMailPriority(java.lang.String nmail)

getNetscapeMailPriority

public java.lang.String getNetscapeMailPriority()

setMailSuccessMessage

public void setMailSuccessMessage(java.lang.String msm)

getMailSuccessMessage

public java.lang.String getMailSuccessMessage()

setMessageException

public void setMessageException(java.lang.String mes)

getMessageException

public java.lang.String getMessageException()

setAddressException

public void setAddressException(java.lang.String aes)

getAddressException

public java.lang.String getAddressException()

setSenderInfo

public void setSenderInfo(java.lang.String si)

getSenderInfo

public java.lang.String getSenderInfo()

setDefaultMimeType

public void setDefaultMimeType(java.lang.String dmt)

getDefaultMimeType

public java.lang.String getDefaultMimeType()

setExtraHeaderLine

public void setExtraHeaderLine(java.lang.String exl)

getExtraHeaderLine

public java.lang.String getExtraHeaderLine()

ConstructAndSend

public java.lang.String ConstructAndSend(boolean ConstructOnly_NoSend)

getRawMimeMessage

public javax.mail.internet.MimeMessage getRawMimeMessage()

getMessageSession

public javax.mail.Session getMessageSession()

addAttachment

public void addAttachment(java.io.File data_to_be_attached,
                          java.lang.String mimetype,
                          java.lang.String file_name,
                          java.lang.String file_description)

removeAllAttachments

public void removeAllAttachments()

removeAttachment

public java.lang.String removeAttachment(int ai)

listAttachments

public java.util.Vector listAttachments()

listAttachmentsMimes

public java.util.Vector listAttachmentsMimes()

listAttachmentsNames

public java.util.Vector listAttachmentsNames()

listAttachmentsDescriptions

public java.util.Vector listAttachmentsDescriptions()

setSessionProperties

public void setSessionProperties(java.util.Properties prp,
                                 javax.mail.Authenticator au)

getSessionProperties

public java.util.Properties getSessionProperties()

ClearOldMessage

public void ClearOldMessage()

addTO

public void addTO(java.lang.String new_TO)

addBCC

public void addBCC(java.lang.String new_BCC)

addCC

public void addCC(java.lang.String new_CC)