Java Doc for MailUtil.java in  » Wiki-Engine » JSPWiki » com » ecyrd » jspwiki » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Wiki Engine » JSPWiki » com.ecyrd.jspwiki.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   com.ecyrd.jspwiki.util.MailUtil

MailUtil
final public class MailUtil (Code)

Contains static methods for sending e-mails to recipients using JNDI-supplied JavaMail Sessions supplied by a web container (preferred) or configured via jspwiki.properties; both methods are described below. Because most e-mail servers require authentication, for security reasons implementors are strongly encouraged to use container-managed JavaMail Sessions so that passwords are not exposed in jspwiki.properties.

To enable e-mail functions within JSPWiki, administrators must do three things: ensure that the required JavaMail JARs are on the runtime classpath, configure JavaMail appropriately, and (recommdended) configure the JNDI JavaMail session factory.

JavaMail runtime JARs

The first step is easy: JSPWiki bundles recent versions of the required JavaMail mail.jar and activation.jar into the JSPWiki WAR file; so, out of the box this is already taken care of. However, when using JNDI-supplied Session factories, these should be moved, not copied, to a classpath location where the JARs can be shared by both the JSPWiki webapp and the container. For example, Tomcat 5 provides the directory $CATALINA_HOME>/common/lib for storage of shared JARs; move mail.jar and activation there instead of keeping them in /WEB-INF/lib.

JavaMail configuration

Regardless of the method used for supplying JavaMail sessions (JNDI container-managed or via jspwiki.properties, JavaMail needs certain properties set in order to work correctly. Configurable properties are these:

Property Default Definition
jspwiki.mail.jndiname mail/Session The JNDI name of the JavaMail session factory
mail.smtp.host 127.0.0.1 The SMTP mail server from which messages will be sent.
mail.smtp.port 25 The port number of the SMTP mail service.
mail.smtp.account (not set) The user name of the sender. If this value is supplied, the JavaMail session will attempt to authenticate to the mail server before sending the message. If not supplied, JavaMail will attempt to send the message without authenticating (i.e., it will use the server as an open relay). In real-world scenarios, you should set this value.
mail.smtp.password (not set) The password of the sender. In real-world scenarios, you should set this value.
mail.from ${user.name}@${mail.smtp.host}* The e-mail address of the sender.
mail.smtp.timeout 5000* Socket I/O timeout value, in milliseconds. The default is 5 seconds.
mail.smtp.connectiontimeout 5000* Socket connection timeout value, in milliseconds. The default is 5 seconds.
mail.smtp.starttls.enable true* If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Note that an appropriate trust store must configured so that the client will trust the server's certificate. By default, the JRE trust store contains root CAs for most public certificate authorities.

*These defaults apply only if the stand-alone Session factory is used (that is, these values are obtained from jspwiki.properties). If using a container-managed JNDI Session factory, the container will likely supply its own default values, and you should probably override them (see the next section).

Container JNDI Session factory configuration

You are strongly encouraged to use a container-managed JNDI factory for JavaMail sessions, rather than configuring JavaMail through jspwiki.properties. To do this, you need to two things: uncomment the <resource-ref> block in /WEB-INF/web.xml that enables container-managed JavaMail, and configure your container's JavaMail resource factory. The web.xml part is easy: just uncomment the section that looks like this:

<resource-ref>
 <description>Resource reference to a container-managed JNDI JavaMail factory for sending e-mails.</description>
 <res-ref-name>mail/Session</res-ref-name>
 <res-type>javax.mail.Session</res-type>
 <res-auth>Container</res-auth>
 </resource-ref>

To configure your container's resource factory, follow the directions supplied by your container's documentation. For example, the Tomcat 5.5 docs state that you need a properly configured <Resource> element inside the JSPWiki webapp's <Context> declaration. Here's an example shows how to do it:

<Context ...>
 ...
 <Resource name="mail/Session" auth="Container"
 type="javax.mail.Session"
 mail.smtp.host="127.0.0.1"/>
 mail.smtp.port="25"/>
 mail.smtp.account="your-account-name"/>
 mail.smtp.password="your-password"/>
 mail.from="Snoop Dogg <snoop@dogg.org>"/>
 mail.smtp.timeout="5000"/>
 mail.smtp.connectiontimeout="5000"/>
 mail.smtp.starttls.enable="true"/>
 ...
 </Context>

Note that with Tomcat (and most other application containers) you can also declare the JavaMail JNDI factory as a global resource, shared by all applications, instead of as a local JSPWiki resource as we have done here. For example, the following entry in $CATALINA_HOME/conf/server.xml creates a global resource:

<GlobalNamingResources>
 <Resource name="mail/Session" auth="Container"
 type="javax.mail.Session"
 ...
 mail.smtp.starttls.enable="true"/>
 </GlobalNamingResources>

This approach — creating a global JNDI resource — yields somewhat decreased deployment complexity because the JSPWiki webapp no longer needs its own JavaMail resource declaration. However, it is slightly less secure because it means that all other applications can now obtain a JavaMail session if they want to. In many cases, this is what you want.

NOTE: Versions of Tomcat 5.5 later than 5.5.17, and up to and including 5.5.23 have a b0rked version of $CATALINA_HOME/common/lib/naming-factory.jar that prevents usage of JNDI. To avoid this problem, you should patch your 5.5.23 version of naming-factory.jar with the one from 5.5.17. This is a known issue and the bug report (#40668) is here.
author:
   Christoph Sauer
author:
   Dan Frankowski
author:
   Andrew Jaquith


Inner Class :protected static class SmtpAuthenticator extends Authenticator

Field Summary
final protected static  StringDEFAULT_MAIL_HOST
    
final protected static  StringDEFAULT_MAIL_JNDI_NAME
    
final protected static  StringDEFAULT_MAIL_PORT
    
final protected static  StringDEFAULT_MAIL_TIMEOUT
    
final protected static  StringDEFAULT_SENDER
    
final protected static  StringPROP_MAIL_ACCOUNT
    
final public static  StringPROP_MAIL_AUTH
    
final protected static  StringPROP_MAIL_CONNECTION_TIMEOUT
    
final protected static  StringPROP_MAIL_HOST
    
final protected static  StringPROP_MAIL_JNDI_NAME
    
final protected static  StringPROP_MAIL_PASSWORD
    
final protected static  StringPROP_MAIL_PORT
    
final protected static  StringPROP_MAIL_SENDER
    
final protected static  StringPROP_MAIL_STARTTLS
    
final protected static  StringPROP_MAIL_TIMEOUT
    
final protected static  StringPROP_MAIL_TRANSPORT
    
final protected static  Loggerlog
    


Method Summary
protected static  SessiongetJNDIMailSession(String jndiName)
     Returns a JavaMail Session instance from a JNDI container-managed factory.
Parameters:
  jndiName - the JNDI name for the resource.
protected static  SessiongetStandaloneMailSession(Properties props)
     Returns a stand-alone JavaMail Session by looking up the correct mail account, password and host from a supplied set of properties.
public static  voidsendMessage(WikiEngine engine, String to, String subject, String content)
    

Sends an e-mail to a specified receiver using a JavaMail Session supplied by a JNDI mail session factory (preferred) or a locally initialized session based on properties in jspwiki.properties.

public static  voidsendMessage(WikiEngine engine, String to, String from, String subject, String content)
    

Sends an e-mail to a specified receiver from a specified sender, using a JavaMail Session supplied by a JNDI mail session factory (preferred) or a locally initialized session based on properties in jspwiki.properties.


Field Detail
DEFAULT_MAIL_HOST
final protected static String DEFAULT_MAIL_HOST(Code)



DEFAULT_MAIL_JNDI_NAME
final protected static String DEFAULT_MAIL_JNDI_NAME(Code)



DEFAULT_MAIL_PORT
final protected static String DEFAULT_MAIL_PORT(Code)



DEFAULT_MAIL_TIMEOUT
final protected static String DEFAULT_MAIL_TIMEOUT(Code)



DEFAULT_SENDER
final protected static String DEFAULT_SENDER(Code)



PROP_MAIL_ACCOUNT
final protected static String PROP_MAIL_ACCOUNT(Code)



PROP_MAIL_AUTH
final public static String PROP_MAIL_AUTH(Code)



PROP_MAIL_CONNECTION_TIMEOUT
final protected static String PROP_MAIL_CONNECTION_TIMEOUT(Code)



PROP_MAIL_HOST
final protected static String PROP_MAIL_HOST(Code)



PROP_MAIL_JNDI_NAME
final protected static String PROP_MAIL_JNDI_NAME(Code)



PROP_MAIL_PASSWORD
final protected static String PROP_MAIL_PASSWORD(Code)



PROP_MAIL_PORT
final protected static String PROP_MAIL_PORT(Code)



PROP_MAIL_SENDER
final protected static String PROP_MAIL_SENDER(Code)



PROP_MAIL_STARTTLS
final protected static String PROP_MAIL_STARTTLS(Code)



PROP_MAIL_TIMEOUT
final protected static String PROP_MAIL_TIMEOUT(Code)



PROP_MAIL_TRANSPORT
final protected static String PROP_MAIL_TRANSPORT(Code)



log
final protected static Logger log(Code)





Method Detail
getJNDIMailSession
protected static Session getJNDIMailSession(String jndiName) throws NamingException(Code)
Returns a JavaMail Session instance from a JNDI container-managed factory.
Parameters:
  jndiName - the JNDI name for the resource. If null, the default valueof mail/Session will be used the initialized JavaMail Session
throws:
  NamingException - if the Session cannot be obtained; for example, if the factory is not configured



getStandaloneMailSession
protected static Session getStandaloneMailSession(Properties props)(Code)
Returns a stand-alone JavaMail Session by looking up the correct mail account, password and host from a supplied set of properties. If the JavaMail property is set to a value that is non-null and of non-zero length, the Session will be initialized with an instance of javax.mail.Authenticator .
Parameters:
  props - the properties that contain mail session properties the initialized JavaMail Session



sendMessage
public static void sendMessage(WikiEngine engine, String to, String subject, String content) throws AddressException, MessagingException(Code)

Sends an e-mail to a specified receiver using a JavaMail Session supplied by a JNDI mail session factory (preferred) or a locally initialized session based on properties in jspwiki.properties. See the top-level JavaDoc for this class for a description of required properties and their default values.

The e-mail address used for the to parameter must be in RFC822 format, as described in the JavaDoc for javax.mail.internet.InternetAddress and more fully at http://www.freesoft.org/CIE/RFC/822/index.htm. In other words, e-mail addresses should look like this:

Snoop Dog <snoop.dog@shizzle.net>
snoop.dog@shizzle.net

Note that the first form allows a "friendly" user name to be supplied in addition to the actual e-mail address.


Parameters:
  engine - the WikiEngine for the current wiki
Parameters:
  to - the receiver
Parameters:
  subject - the subject line of the message
Parameters:
  content - the contents of the mail message, as plain text



sendMessage
public static void sendMessage(WikiEngine engine, String to, String from, String subject, String content) throws MessagingException(Code)

Sends an e-mail to a specified receiver from a specified sender, using a JavaMail Session supplied by a JNDI mail session factory (preferred) or a locally initialized session based on properties in jspwiki.properties. See the top-level JavaDoc for this class for a description of required properties and their default values.

The e-mail addresses used for the to and from parameters must be in RFC822 format, as described in the JavaDoc for javax.mail.internet.InternetAddress and more fully at http://www.freesoft.org/CIE/RFC/822/index.htm. In other words, e-mail addresses should look like this:

Snoop Dog <snoop.dog@shizzle.net>
snoop.dog@shizzle.net

Note that the first form allows a "friendly" user name to be supplied in addition to the actual e-mail address.


Parameters:
  engine - the WikiEngine for the current wiki
Parameters:
  to - the receiver
Parameters:
  from - the address the email will be from
Parameters:
  subject - the subject line of the message
Parameters:
  content - the contents of the mail message, as plain text



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.