View Javadoc

1   /*
2    * $Id: SmtpConnector.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.email;
12  
13  import org.mule.umo.UMOComponent;
14  import org.mule.umo.lifecycle.InitialisationException;
15  import org.mule.umo.endpoint.UMOEndpoint;
16  import org.mule.umo.provider.UMOMessageReceiver;
17  
18  import java.util.Properties;
19  
20  /**
21   * <code>SmtpConnector</code> is used to connect to and send data to an SMTP mail
22   * server
23   */
24  public class SmtpConnector extends AbstractMailConnector
25  {
26      public static final String DEFAULT_SMTP_HOST = "localhost";
27      public static final int DEFAULT_SMTP_PORT = 25;
28      public static final String DEFAULT_CONTENT_TYPE = "text/plain";
29  
30      private String host = DEFAULT_SMTP_HOST;
31      private int port = DEFAULT_SMTP_PORT;
32      private String username;
33      private String password;
34  
35      /**
36       * Holds value of bcc addresses.
37       */
38      private String bcc;
39  
40      /**
41       * Holds value of cc addresses.
42       */
43      private String cc;
44  
45      /**
46       * Holds value of replyTo addresses.
47       */
48      private String replyTo;
49  
50      /**
51       * Holds value of default subject
52       */
53      private String defaultSubject = "[No Subject]";
54  
55      /**
56       * Holds value of the from address.
57       */
58      private String from;
59  
60      /**
61       * Any custom headers to be set on messages sent using this connector
62       */
63      private Properties customHeaders = new Properties();
64  
65      private String contentType = DEFAULT_CONTENT_TYPE;
66  
67      
68      public SmtpConnector()
69      {
70          this(DEFAULT_SMTP_PORT);
71      }
72      
73      SmtpConnector(int defaultPort)
74      {
75          super(defaultPort, null);
76      }
77      
78      public String getProtocol()
79      {
80          return "smtp";
81      }
82  
83      //@java.lang.Override
84      protected void doInitialise() throws InitialisationException
85      {
86          //If the User ID for SMTP is an email address and the from address is not set, then
87          //use the username
88          if(getFromAddress()==null && getUsername()!=null && getUsername().indexOf('@') > -1)
89          {
90              setFromAddress(getUsername());
91          }
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see org.mule.providers.UMOConnector#registerListener(javax.jms.MessageListener,
98       *      java.lang.String)
99       */
100     public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception
101     {
102         throw new UnsupportedOperationException("Listeners cannot be registered on a SMTP endpoint");
103     }
104 
105     /**
106      * @return The default from address to use
107      */
108     public String getFromAddress()
109     {
110         return from;
111     }
112 
113     /**
114      * @return the default comma separated list of BCC addresses to use
115      */
116     public String getBccAddresses()
117     {
118         return bcc;
119     }
120 
121     /**
122      * @return the default comma separated list of CC addresses to use
123      */
124     public String getCcAddresses()
125     {
126         return cc;
127     }
128 
129     /**
130      * @return the default message subject to use
131      */
132     public String getSubject()
133     {
134         return defaultSubject;
135     }
136 
137     public void setBccAddresses(String string)
138     {
139         bcc = string;
140     }
141 
142     public void setCcAddresses(String string)
143     {
144         cc = string;
145     }
146 
147     public void setSubject(String string)
148     {
149         defaultSubject = string;
150     }
151 
152     public void setFromAddress(String string)
153     {
154         from = string;
155     }
156 
157     public String getReplyToAddresses()
158     {
159         return replyTo;
160     }
161 
162     public void setReplyToAddresses(String replyTo)
163     {
164         this.replyTo = replyTo;
165     }
166 
167     public Properties getCustomHeaders()
168     {
169         return customHeaders;
170     }
171 
172     public void setCustomHeaders(Properties customHeaders)
173     {
174         this.customHeaders = customHeaders;
175     }
176 
177     public String getContentType()
178     {
179         return contentType;
180     }
181 
182     public void setContentType(String contentType)
183     {
184         this.contentType = contentType;
185     }
186 
187     public int getDefaultPort()
188     {
189         return DEFAULT_SMTP_PORT;
190     }
191 
192     public String getHost()
193     {
194         return host;
195     }
196 
197     public void setHost(String host)
198     {
199         this.host = host;
200     }
201 
202     public String getPassword()
203     {
204         return password;
205     }
206 
207     public void setPassword(String password)
208     {
209         this.password = password;
210     }
211 
212     public int getPort()
213     {
214         return port;
215     }
216 
217     public void setPort(int port)
218     {
219         this.port = port;
220     }
221 
222     public String getUsername()
223     {
224         return username;
225     }
226 
227     public void setUsername(String username)
228     {
229         this.username = username;
230     }
231 
232 }