Tomcat JavaMail Support and conflict--tomcat create a mail delivery environment object Session__java

Source: Internet
Author: User
Tags auth sessions

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") + "&LT;ITSTAR1965@SINA.COM&GT;," +
Mimeutility.encodetext ("Xiao Hua") + "<xh216319@163.com>");
          response.getwriter (). Print ("ok!");         }catch (Exception e) {          &
nbsp         }finally{                      }    &nbsp}     public void DoPost (HttpServletRequest request , httpservletresponse response)             throws servletexception,

 IOException {         doget (request, response);     }}

Related notes:

(1) JavaMail support and conflict in Tomcat





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.