com.objectxp.msg
Class SmtpService

java.lang.Object
  extended by com.objectxp.msg.SmtpService

public class SmtpService
extends java.lang.Object

This class may be used to send a Internet mail message using a Smtp mail server.

Example

   ...
   SmtpMessage msg = new SmtpMessage();
   SmtpService service = new SmtpService();
   service.setMailhost("mailhost");
   service.setSenderDomain("mydomain");
   service.init(jsmsProperties);
   
   msg.addRecipient("user1@domain1.com");
   msg.addRecipient("user2@domain2.com", Recipient.RT_CC);
   msg.addRecipient("myself@mydomain",Recipient.RT_BCC);
   msg.setSubject("Business lunch");
   msg.setMessage("Business lunch today at the Restaurant 'Chez Max'");
   
   try {
    service.sendMessage(msg);
    System.out.println("Message sent successfuly, ID is "+msg.getMessageId());
   } catch( MessageException me ) {
     System.err.println("Message could not be sent: "+me.getMessage());
   }
 

See Also:
SmtpMessage

Constructor Summary
SmtpService()
          Create a new Smtp service.
SmtpService(java.io.File conf)
          Create a new Smtp Service.
SmtpService(java.util.Properties conf)
          Create a new Smtp Service.
SmtpService(java.lang.String mailhost, java.lang.String maildomain)
          Create a new Smtp service.
 
Method Summary
 java.lang.String getDefaultSender()
          Get the default sender.
 java.lang.String getDefaultSubject()
          Get the default subject.
 java.lang.String getMailhost()
          Get the address of the smtp server.
 int getMailport()
          Get the port where the smtp-server is listening.
 java.lang.String getSenderDomain()
          Get the sender domain name
 void init()
          Initialize this service.
 void init(java.io.File file)
          Initialize the SMTP Service.
 void init(java.util.Properties p)
          Initialize the SMTP Service.
static void main(java.lang.String[] args)
          Send a Internet mail message from the command line.
 void sendMessage(Message message)
          Sends the message via mail.
 void sendMessage(SmtpMessage message)
          Send a SmtpMessage to one or multiple recipients.
 void setDefaultSender(java.lang.String sender)
          Set the default sender address
 void setDefaultSubject(java.lang.String subject)
          Set the default subject.
 void setMailhost(java.lang.String mailhost)
          Set the address of the smtp-server.
 void setMailport(int mailport)
          Set the port of the smtp-server
 void setSenderDomain(java.lang.String domain)
          Set the sender domain.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SmtpService

public SmtpService()
Create a new Smtp service. The service must be initialized by calling init() before it can be used to send messages.


SmtpService

public SmtpService(java.lang.String mailhost,
                   java.lang.String maildomain)
Create a new Smtp service. The service must be initialized by calling init() before it can be used to send messages.

Parameters:
mailhost - the Smtp Server to use for sending mails.
maildomain - the senders domain name.

SmtpService

public SmtpService(java.util.Properties conf)
Create a new Smtp Service. Besides your jSMS license key properties, the specified Properties may contain the following configuration settings:

smtp.hostthe Smtp server to use for sending mail
smtp.domainthe senders domain name.
smtp.subjectthe default subject that will be used when sending messages that have no (null) subject
smtp.senderthe default sender address that will be used when sending messages that have no sender

Parameters:
conf - configuration data

SmtpService

public SmtpService(java.io.File conf)
            throws java.io.FileNotFoundException,
                   java.io.IOException
Create a new Smtp Service. The specified file may contain configuration data for the service in the format key=value and must contain a valid jSMS license key.

Throws:
java.io.FileNotFoundException
java.io.IOException
See Also:
SmtpService(Properties conf)
Method Detail

init

public void init()
          throws MessageException
Initialize this service. When calling this method it tries to locate and load the jSMS properties needed for service initialization and initializes the service by calling init(Properties).

The location of the jSMS properties can be specified by defining the System property "jsms.conf". If the System property is not set, jSMS uses the value "jsms.conf" as default. The System property be set by e.g. passing it as parameter to the Java VM (java -Djsms.conf=...).

If the jsms.conf location is non-absolute, the method tries to locate it by...

  1. looking in the current working directory
  2. looking in the users home directory
  3. Using the Classloader to resolve the resource
  4. Converting the location to a URL and reading from it

Example values for jsms.conf are:

Throws:
MessageException

init

public void init(java.io.File file)
          throws java.io.FileNotFoundException,
                 java.io.IOException
Initialize the SMTP Service. See SmtpService(Properties conf) for a list of available configuration properties. The properties passed must contain a valid jSMS license key.

Throws:
java.io.FileNotFoundException
java.io.IOException

init

public void init(java.util.Properties p)
Initialize the SMTP Service. See SmtpService(Properties conf) for a list of available configuration properties. The properties passed must contain a valid jSMS license key.


getMailhost

public java.lang.String getMailhost()
Get the address of the smtp server.

Returns:
address of the smtp-server

setMailhost

public void setMailhost(java.lang.String mailhost)
Set the address of the smtp-server.

Parameters:
mailhost - the mailhost to use for delivering mail

setSenderDomain

public void setSenderDomain(java.lang.String domain)
Set the sender domain.

Parameters:
domain - the domain name

getSenderDomain

public java.lang.String getSenderDomain()
Get the sender domain name


getMailport

public int getMailport()
Get the port where the smtp-server is listening.

Returns:
port nr

setMailport

public void setMailport(int mailport)
Set the port of the smtp-server

Parameters:
mailport -

getDefaultSender

public java.lang.String getDefaultSender()
Get the default sender. This default sender is used only if in the message doesn't define one

Returns:
default sender

setDefaultSender

public void setDefaultSender(java.lang.String sender)
Set the default sender address

Parameters:
sender -
See Also:
getDefaultSender()

getDefaultSubject

public java.lang.String getDefaultSubject()
Get the default subject. This subject os only used if the message doesn't define one.

Returns:
default subject

setDefaultSubject

public void setDefaultSubject(java.lang.String subject)
Set the default subject.

Parameters:
subject -
See Also:
getDefaultSubject()

sendMessage

public void sendMessage(SmtpMessage message)
                 throws MessageException
Send a SmtpMessage to one or multiple recipients. The method uses the currently set mailhost to deliver the mail. If the mail does not contain a sender, the default Sender is used. If the mail does not contain a Subject (null), the default Subject is used.

Parameters:
message - the message to send
Throws:
MessageException - if the message could not be sent or the service has not been initialized properly

sendMessage

public void sendMessage(Message message)
                 throws MessageException
Sends the message via mail.

Parameters:
message - the message to send
Throws:
MessageException - if the message could not be sent
See Also:
sendMessage(SmtpMessage)

main

public static void main(java.lang.String[] args)
Send a Internet mail message from the command line.

  Usage: SmtpService <-c configfile> [-V] [-h host] [-d domain] [-s subject] [-f sender] [-m file]  
    Options:
      -c configuration file containing at least a valid jSMS license key
      -V display version information
      -h Smtp Host to use
      -s Subject
      -d Sender Domain
      -m read message from file
      -f Your mail adress
  

If you do not specify a file (-m file), the mail message will be read from standard input (STDIN).



object XP, Inc. © 2000-2008. All rights reserved object XP