Enviar email desde un Servlet

Alex
07 de Diciembre del 2004
Hola,

Mi problema es que estoy realizando un aplicación con Servlets en la cual quiero enviar un email sea html
o solo texto ya vi el Api JavaMail pero no me queda muy claro les agradeceria que me dijeran de algun turorial o que me den algun codigo que haga eso.

Agradecería su Ayuda.


Jes?eyes
07 de Diciembre del 2004
Tomado de la documentación del servidor Tomcat (JNDI Resources HOW-TO):

JavaMail Sessions

0. Introduction

In many web applications, sending electronic mail messages is a required part of the system's functionality. The Java Mail API makes this process relatively straightforward, but requires many configuration details that the client application must be aware of (including the name of the SMTP host to be used for message sending).

Tomcat 5 includes a standard resource factory that will create javax.mail.Session session instances for you, already connected to the SMTP server that is configured in server.xml. In this way, the application is totally insulated from changes in the email server configuration environment - it simply asks for, and receives, a preconfigured session whenever needed.

The steps required for this are outlined below.
1. Declare Your Resource Requirements

The first thing you should do is modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured sessions. By convention, all such names should resolve to the mail subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this:



<resource-ref>
<description>
Resource reference to a factory for javax.mail.Session
instances that may be used for sending electronic mail
messages, preconfigured to connect to the appropriate
SMTP server.
</description>
<res-ref-name>
mail/Session
</res-ref-name>
<res-type>
javax.mail.Session
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>




WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.
2. Code Your Application's Use Of This Resource

A typical use of this resource reference might look like this:



Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getParameter("from"));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(request.getParameter("to"));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(request.getParameter("subject"));
message.setContent(request.getParameter("content"), "text/plain");
Transport.send(message);




Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in $CATALINA_HOME/conf/server.xml, as described below.
3. Configure Tomcat's Resource Factory

To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application (or nested inside a DefaultContext element for the surrounding <Host> or <Engine> element.



<Context ...>
...
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"/>
<ResourceParams name="mail/Session">
<parameter>
<name>mail.smtp.host</name>
<value>localhost</value>
</parameter>
</ResourceParams>
...
</Context>




Note that the resource name (here, mail/Session) must match the value specified in the web application deployment descriptor. Customize the value of the mail.smtp.host parameter to point at the server that provides SMTP service for your network.

Por desgracia, nunca he probado hacer esto por mi mismo, así que no creo que podría darte un mejor consejo.

Jesús

alex
07 de Diciembre del 2004
Gracias Jesús me fue de ayuda.