commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From quint...@apache.org
Subject cvs commit: jakarta-commons-sandbox/email/src/java/org/apache/commons/mail Email.java
Date Mon, 17 Mar 2003 21:41:56 GMT
quintonm    2003/03/17 13:41:56

  Modified:    email/src/java/org/apache/commons/mail Email.java
  Log:
  - Added support for authentication.
  - Changed the value of the SMTP constant to "smtp".  This value has to be lowercase...
  
  Revision  Changes    Path
  1.11      +67 -18    jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java
  
  Index: Email.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Email.java	9 Mar 2003 23:42:16 -0000	1.10
  +++ Email.java	17 Mar 2003 21:41:56 -0000	1.11
  @@ -54,13 +54,14 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Date;
  +import java.util.Enumeration;
   import java.util.Hashtable;
   import java.util.Properties;
  -import java.util.ArrayList;
  -import java.util.Enumeration;
  -import java.util.Collection;
   
  +import javax.mail.Authenticator;
   import javax.mail.Message;
   import javax.mail.MessagingException;
   import javax.mail.Session;
  @@ -97,8 +98,9 @@
   
       public static final String MAIL_HOST = "mail.host";
       public static final String MAIL_SMTP_FROM = "mail.smtp.from";
  +    public static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
       public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
  -    public static final String SMTP = "SMTP";
  +    public static final String SMTP = "smtp";
       public static final String TEXT_HTML = "text/html";
       public static final String TEXT_PLAIN = "text/plain";
       public static final String ATTACHMENTS = "attachments";
  @@ -136,6 +138,12 @@
       /** Sent date */
       private Date sentDate;
   
  +    /**
  +     * Instance of an <code>Authenticator</code> object that will be used
  +     * when authentication is requested from the mail server.
  +     */
  +    private Authenticator authenticator;
  +
      /**
       * The hostname of the mail server with which to connect. If null will try
       * to get property from system.properties. If still null, quit
  @@ -174,6 +182,40 @@
       }
   
       /**
  +     * Sets the userName and password if authentication is needed.  If this
  +     * method is not used, no authentication will be performed.
  +     * <p>
  +     * This method will create a new instance of
  +     * <code>DefaultAuthenticator</code> using the supplied parameters.
  +     *
  +     * @param userName User name for the SMTP server
  +     * @param password password for the SMTP server
  +     * @see DefaultAuthenticator
  +     * @see #setAuthenticator
  +     */
  +    public void setAuthentication(String userName, String password)
  +    {
  +        Authenticator authenticator = new DefaultAuthenticator(
  +                userName, password);
  +        setAuthenticator(authenticator);
  +    }
  +
  +    /**
  +     * Sets the <code>Authenticator</code> to be used when authentication
  +     * is requested from the mail server.
  +     * <p>
  +     * This method should be used when your outgoing mail server requires
  +     * authentication.  Your mail server must also support RFC2554.
  +     *
  +     * @param authenticator the <code>Authenticator</code> object.
  +     * @see Authenticator
  +     */
  +    public void setAuthenticator(Authenticator authenticator)
  +    {
  +        this.authenticator = authenticator;
  +    }
  +
  +    /**
        * Set the charset of the message.
        *
        * @param charset A String.
  @@ -237,7 +279,7 @@
        * @return A Session.
        */
       private Session getMailSession()
  -        throws MessagingException
  +            throws MessagingException
       {
           Properties properties = System.getProperties();
           properties.setProperty (MAIL_TRANSPORT_PROTOCOL, SMTP);
  @@ -250,13 +292,19 @@
           if (hostName == null)
           {
               throw new MessagingException(
  -                "Cannot find valid hostname to mail session");
  +                "Cannot find valid hostname for mail session");
           }
   
           properties.setProperty(MAIL_HOST, hostName);
           properties.setProperty(MAIL_DEBUG, Boolean.toString(this.debug));
   
  -        Session session = Session.getDefaultInstance(properties, null);
  +        if (this.authenticator!= null)
  +        {
  +            properties.setProperty(MAIL_SMTP_AUTH, "true");
  +        }
  +
  +        Session session = Session.getDefaultInstance(
  +                properties, this.authenticator);
   
           return session;
       }
  @@ -269,7 +317,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email setFrom(String email)
  -        throws MessagingException
  +            throws MessagingException
       {
           return setFrom(email, null);
       }
  @@ -283,7 +331,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email setFrom(String email, String name)
  -        throws MessagingException
  +            throws MessagingException
       {
           try
           {
  @@ -316,7 +364,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addTo(String email)
  -        throws MessagingException
  +            throws MessagingException
       {
           return addTo(email, null);
       }
  @@ -330,7 +378,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addTo(String email, String name)
  -        throws MessagingException
  +            throws MessagingException
       {
           try
           {
  @@ -373,7 +421,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addCc(String email)
  -        throws MessagingException
  +            throws MessagingException
       {
           return addCc(email, null);
       }
  @@ -387,7 +435,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addCc(String email, String name)
  -        throws MessagingException
  +            throws MessagingException
       {
           try
           {
  @@ -431,7 +479,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addBcc(String email)
  -        throws MessagingException
  +            throws MessagingException
       {
           return addBcc(email, null);
       }
  @@ -445,7 +493,7 @@
        * @exception MessagingException Indicates an invalid email address
        */
       public Email addBcc(String email, String name)
  -        throws MessagingException
  +            throws MessagingException
       {
           try
           {
  @@ -590,9 +638,10 @@
        * @exception MessagingException if there was an error.
        */
       public void send()
  -        throws MessagingException
  +            throws MessagingException
       {
  -        MimeMessage message = new MimeMessage(getMailSession());
  +        Session session = getMailSession();
  +        MimeMessage message = new MimeMessage(session);
   
           if (subject != null)
           {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message