Java Mail Development--The writing of 6-web email __web

Source: Internet
Author: User
Tags auth java web

1. A lot of email from the client, the actual application of the actual value of the small, real life is the most used in the Web page to send email function, here, such as a Web application with the mail delivery function.

2. The following is an example of a Web email using Comcast with its own jndi and its analysis:

Package com.web.servlet;

Import java.io.IOException;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
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; /** * Use a content.xml configuration file to write a Web email */public class Sendmailservlet extends HttpServlet {public void doget (httpse Rvletrequest request, HttpServletResponse response) throws Servletexception, IOException {try {context Contex
			T = (context) new InitialContext (). Lookup ("java:comp/env");
			
			Session Sessions = (session) context.lookup ("Mail/context");
			msg = new MimeMessage (session);
			Msg.setfrom (New InternetAddress ("itcast_test@sina.com")); Internetaddress[] to = {new InternetAddress ("itcast_test@sina.com")};
			Msg.setrecipients (message.recipienttype.to,to);
			Msg.setsubject ("web email");
			
			Msg.settext ("This is a Java Web written email");
			Transport transport = Session.gettransport ();
			Transport.connect ("Itcast_test", "123456");
			Transport.sendmessage (msg,to);
			
			Transport.close ();
		Response.getwriter (). println ("Send ok!");
		catch (Exception e) {e.printstacktrace (Response.getwriter ()); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexce
	ption {doget (request, response); } The resources defined in this elements may is referenced by the following elements in the Web application Deploymen T Descriptor (/web-inf/web.xml) of the Your WEB application; Important profile Context.xml, the file is placed under Web-inf Meta-inf, which is detailed below: < context> <resource name= "Mail/context" auth= "Container" type= "Javax.mail.Session" mail.smt P.host= "smtp.sina.com" mail.smtp.auth= "true" MAIL.TRANSPort.protocol= "SMTP"/> </Context>
 


 

Related Article

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.