Examples explain JavaMail

  Java Mail API for the development of the SUN Java developers with common API framework for the continuing efforts of a good example.    Promote common framework, opposed the limited supply solution 
  Herald a full increasingly open development environment in the future.    Java Mail API structure itself proved its developers one of the fundamental objectives 
  — Software development should depend on the workload of application itself, as well as the complexity of the development required by the degree of control. 
  In other words, Java Mail API to maintain simple as possible.    At first glance, Java Mail API owned by the total number of categories, as well as the relationship between the category may be misleading to have to spend a long time learning.    In fact, once the official start using, you will find the API may be included in the application robust mail / communications support of the simple tools 

  Examples of the e-mail is a simple example of a complete e-mail from the HTML page index.html.    Responsible for sending e-mail and JSP pages pose.    For beginners have a good reference, containing a detailed explanation 

  1. Index.html 
<html>
<head>
  <meta Http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  <title> Fill messages </ title> 
  <LINK REL="stylesheet" HREF="E:\tomcat 4.1.12\webapps\qds\css.css" TYPE="text/css"> 
  </ Head> 

  <body Bgcolor="#FFFFCC"> 
  <form Name="form1" method="post" action="SendMail.jsp"> 
  <table Width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" height="258"> 

  <div Align="center"> 

  <font Size="5" color="blue"> fill messages </ font> 
  <tr Bgcolor="#FFFFFF"> 
  <td Width="30%" height="34"> Addresser: </ td> 
  <td Width="70%" height="34"> 
  <input Name="from" type="text" id="from" CLASS="kuang-bg"> </ td> 
  </ Tr> 
  <tr Bgcolor="#FFFFFF"> 
  <td Width="30%" height="34"> recipient Address: </ td> 
  <td Width="70%" height="34"> 
  <input Name="to" type="text" id="to" CLASS="kuang-bg"> </ td> 
  </ Tr> 

  <tr Bgcolor="#FFFFFF"> 
  <td Width="30%" height="25"> theme: </ td> 
  <td Width="70%" height="25"> 
  <input Name="title" type="text" id="title" CLASS="kuang-bg"> </ td> 
  </ Tr> 
<tr>
  <td Height="119" colspan="2" bgcolor="#FFFFFF"> 
  <textarea Name="content" cols="50" rows="5" id="content" CLASS="kuang-bg"> </ textarea> </ td> 
  </ Tr> 
  <tr Align="center"> 
  <td Colspan="2" bgcolor="#FFFFFF" height="27"> 
  <input Type="submit" name="Submit" value=" the送" CLASS="botton"> 
  <input Type="reset" name="Submit2" value=" heavy写" CLASS="botton"> 
  </ Td> 
  </ Tr> 
  </ Table> 
  </ Form> 
  </ Body> 
  </ Html> 

  Second, sendMail.jsp 
  <% @ Page contentType = "text / html; charset = GB2312"%> 
  <!–% Request.setCharacterEncoding ( "gb2312 ");%–><!– Chinese handling code -> 
  <! - Introduction to the use of class libraries -> 
  <% @ Page import = "java.util .*"%> 
  <% @ Page import = "javax.mail .*"%> 
  <% @ Page import = "javax.mail.internet .*"%> 
<html>
<head>
  <meta Http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  <title> Email </ title> 
  </ Head> 
<body>

<%
  Try ( 
  / / Html e-mail access to information in the form 
  String tfrom = request.getParameter ( "from"); 
  String tto = request.getParameter ( "to"); 
  String ttitle = request.getParameter ( "title"); 
  String tcontent = request.getParameter ( "content"); 

  / / Properties JavaMail need to create a session object.    It will search for the string "mail.smtp.host" attribute value is the email host. 
  / / Object Properties such as access to the mail server, user name, password and other information, as well as other applications can share the information. 

  Properties props = new Properties ();// also available Properties props = System.getProperties (); 
  Props.put ( "mail.smtp.host", "smtp.21cn.com ");// email server storage information 
  Props.put ( "mail.smtp.auth", "true ");// validated at the same time 

  / / 2: (in the case of the configuration JavaMail weblogin: JNDI require designated Retrieval 
  / / Context ctx = new InitialContext (); 
  / / Session s = (Session) ctx.lookup ( "MailSession"); 
  / / Message msg = new MimeMessage (s); 

  / / This category representatives JavaMail Session in an e-mail session. JavaMail each one based on the application of at least one session but can be any number of session. 
  / / Session of the overall situation and the definition of each user with the mail-related attributes.    It described this property and server rooms to exchange information. 

  Session s = Session.getInstance (props, null); / / attributes according to a new e-mail conversation, a null parameter is Authenticator (the verification process) Object 
  S.setDebug (true); / / Set the debugging signs, it is necessary to see through the mail server mail order, the method can be used 

  / / Create their own once the Session object is created to go to the news sent the time.    Then it is necessary to use information type (MimeMessage is one type). 
  / / Message object storage we have actually sent e-mail messages, Message objects as a MimeMessage object need to know to create and should choose a JavaMail session. 
  / / Message of that individual email messages, its attributes, including type, address information and the definition of the directory structure. 

  Message message = new MimeMessage (s) / / e-mail conversation from a new source object 

  / / Message.setContent ( "hello", "test / plain ");// settings news content type, if the format is sent in HTML format must be set up, 
  / / Message.setText ( "Hello ");// general sent text format the news 

  / / Set up e-mail, once you create the Session and Message, and will fill the news content, we can use Address letters identify the address. 
  / / If want a name appears in the e-mail address, can also be passed to the constructor: 
  / / Address from = new InternetAddress ( "xmqds@21cn.com", "qdison ");// the sender's e-mail address 

  Address from = new InternetAddress (tfrom); / / sender's e-mail address 
  Message.setFrom (from) / / set the sender 

  Address to = new InternetAddress (tto); / / recipient's e-mail address 
  Message.setRecipient (Message.RecipientType.TO, to) / / recipient, and set its type to receive TO, there are three kinds of pre-defined types are as follows: 

  / / Message.RecipientType.TO 
  / / Message.RecipientType.CC 
  / / Message.RecipientType.BCC 

  Message.setSubject (ttitle) / / set the theme 
  Message.setText (tcontent); / / Set the content of the letter 
  Message.setSentDate (new Date ());// letter setting time 

  Message.saveChanges ();// stored messages 

  / / Transport is used to send information, 
  / / Transceiver for a mail operation. 
  Transport transport = s.getTransport ( "smtp"); 
  Transport.connect ( "smtp.21cn.com", "your user name", "your password ");//-mail to smtp Login 
  Transport.sendMessage (message, message.getAllRecipients ());// email, and the second parameter is all set good Addressee 
  Transport.close (); 

%>
  <div Align="center"> 
  <p> <font Color="#FF6600"> sent success! </ Font> </ p> 
  <p> <a Href="index.jsp"> recurrence of a </ a> </ p> 
  </ Div> 
<%
  ) Catch (MessagingException e) ( 
  Out.println (e.toString ()); 
  ) 
%>
  </ Body> 
  </ Html> 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply