It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
Because sending messages with attachments can take a lot of time, so if you simply send the message in the form of an access servlet, it will cause the client page to stay, which is not very good, so we generally use AJAX to send the page, to achieve a page without refreshing, or by creating another thread , the main thread returns to the client and lets the child thread send the message, which enables the page to return in advance.
Program code
Mailbythreadservletservlet.java
Package edu.servlet.mail;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Mailbythreadservlet extends HttpServlet {public
void doget (HttpServletRequest request, HttpServletResponse response)
throws Servletexception, IOException {
response.setcontenttype ("text/html; Charset=utf-8 ");
Sendmailthread SMT = new Sendmailthread ();
Thread thread = new Thread (SMT);
Thread.Start ();
Response.getwriter (). Print ("The message is being sent, please wait a moment ...");
public void DoPost (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
doget (request, response);
}
}
Child thread Sendmailthread.java
Package edu.servlet.mail;
Import Java.io.FileInputStream;
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; public class Sendmailthread implements runnable{@Override public void Run () {try{Context initctx = new
InitialContext ();
Tomcat places the created environment container in the initialization container and needs to find the context Envctx = (context) initctx.lookup ("java:comp/env") Through "java:comp/env" (fixed);
In the Environment parameter container through "Mail/dog" to find the Session object Session session = (session) envctx.lookup ("Mail/dog"); /* * Verify that Tomcat's ClassLoader (Envctx.lookup ("Mail/dog")) differs from the application's class loader (Session) * But I don't know. Tao Why is the same result, supposedly is not the same, anyway is not very important * are org.apache.catalina.loader.StandardClassLoader * */S Ystem.out.println (Envctx.looKup ("Mail/dog"). GetClass (). getClassLoader (). GetClass (). GetName ());
System.out.println (Session.class.getClassLoader (). GetClass (). GetName ());
Set debug mode to print execution process session.setdebug (true);
Create a Message object that represents the content of the 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> ");
}catch (Exception e) {}finally{}}}
Visit: Http://localhost:8080/JavaMail_02/servlet/MailByThreadServlet, you will be back soon