It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.
Program Examples:
(1) First in the application under the Webroot/meta-inf directory to create a file named Context.xml (name can not be changed)
<Context>
<resource name= "Mail/dog" auth= "
Container" type= "Javax.mail.Session" Mail.smtp.host= "smtp.163.com" mail.smtp.auth= "true"
mail.transport.protocol= "smtp"
/>
</ Context>
As shown in figure:
(2) Copy the Mail.jar package (mail development-related JAR file) to the Apache Software foundation\tomcat 6.0\lib directory.
Note: Do not copy the Mail.jar package to the Lib directory of the Application project, otherwise the execution of the following sentence will be thrown java.lang.ClassCastException
Session Sessions = (session) envctx.lookup ("Mail/dog");
(3) Write mail sending program code in servlet
Package edu.servlet.mail;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Mailjndiservlet extends HttpServlet { public void doget (HttpServletRequest request, HttpS Ervletresponse response) throws servletexception, IOException { try{
Context Initctx = new InitialContext (); //TOMCAT the created environment container in the initialization container, you need to find by "java:comp/env" (fixed)
Context Envctx = (context) initctx.lookup ("java:comp/env"); //Find the Session object through "Mail/dog" in the Environment parameter container
Session Sessions = (session) envctx.lookup ("Mail/dog"); /* * validation here Tomcat class loader (Envctx.lookup ("Mail /dog ")) differs from the application's class loader (session) * But here do not know why is the same result, ordinarily is not the same, anyway is not very important * are Org.apache.catalina.loader.StandardClassLoader * SyStem.out.println (Envctx.lookup ("Mail/dog"). GetClass (). getClassLoader (). GetClass (). GetName ()); System.out.println (Session.class.getClassLoader ().
GetClass (). GetName ()); //Set debug way to print execution
Session.setdebug (TRUE);
//Create a Message object that represents the content of your messages. Message msg = new MimeMessage (session,new fileinputstream ("C:\\Documents
and settings\\administrator\\ Desktop \\SendMultipartMessageDemo3.eml ")); Transport transport =
Session.gettransport ();
transport.connect ("smtp.163.com", "xh216319", "Xiaoxiao"); transport.sendmessage (msg, internetaddress.parse ( Mimeutility.encodetext ("Itstar") + "<ITSTAR1965@SINA.COM>," +
Mimeutility.encodetext ("Xiao Hua") + "<xh216319@163.com>");
response.getwriter (). Print ("ok!"); }catch (Exception e) { &
nbsp }finally{ }  } public void DoPost (HttpServletRequest request , httpservletresponse response) throws servletexception,
IOException { doget (request, response); }}
Related notes:
(1) JavaMail support and conflict in Tomcat