JSP and JavaMail 3 (try writing the first program)

Source: Internet
Author: User
Tags date html form mail connect reset
js| Program 4. Try to write the first dispatcher

In the front we have made some introductions to javamail, we can try to write our own program.

First, we'll write an HTML program index.htm that writes the message, as follows:
-------------------------------------------------------------------------------------------
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Compose Mail </title>

<body>
<form name= "Form1" method= "Post" action= "testmail.jsp" >
<table width= "border=" "0" align= "center" cellspacing= "1" bgcolor= "#006600" class= "Black" >
<tr bgcolor= "#FFFFFF" >
&LT;TD width= "24%" > Delivery address:</td>
&LT;TD width= "76%" >
<input name= "to" type= "text" id= "to" ></td>
</tr>
<tr bgcolor= "#FFFFFF" >
<td> Theme:</td>
<td>
<input name= "title" type= "text" id= "title" ></td>
</tr>
<tr>
&LT;TD height= "colspan=" "2" bgcolor= "#FFFFFF" >
<textarea name= "Content" cols= "rows=" "5" id= "Content" ></textarea></td>
</tr>
&LT;TR align= "center" >
&LT;TD colspan= "2" bgcolor= "#FFFFFF" >
<input type= "Submit" name= "submit" value= "Send" >
<input type= "reset" name= "Submit2" value= "reset" >
</td>
</tr>
</table>
</form>
</body>


Next, we'll write a handler testmail.jsp, as follows:
-----------------------------------------------------------------------------------------
<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%><!--Chinese processing Code-->

<!--introduces the class libraries to be used-->
<%@ page import= "java.util.*,javax.mail.*"%>
<%@ page import= "javax.mail.internet.*"%>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Send Success </title>

<body>
<%
try{

Get message information from an HTML form
String Tto=request.getparameter ("to");
String Ttitle=request.getparameter ("title");
String tcontent=request.getparameter ("content");

Properties Props=new Properties ()///can also be used for properties props = System.getproperties ();
Props.put ("Mail.smtp.host", "smtp.163.net");//store information for sending mail server
Props.put ("Mail.smtp.auth", "true");
Session s=session.getinstance (props);//Create a new mail conversation based on properties
S.setdebug (TRUE);

MimeMessage message=new MimeMessage (s);//Create a new Message object from a mail session

Set up messages
InternetAddress from=new internetaddress ("boy@163.net");
Message.setfrom (from);/Set Sender
InternetAddress to=new internetaddress (tto);
Message.setrecipient (message.recipienttype.to,to);/Set the recipient, and set its receive type to
Message.setsubject (ttitle);/Set Theme
Message.settext (tcontent)//Set letter content
Message.setsentdate (New Date ());//Set the time of the letter

Send mail
Message.savechanges ()//storing mail information
Transport Transport=s.gettransport ("SMTP");
Transport.connect ("Smtp.163.net", "Boy", "iloveyou");//Logon to mailbox by SMTP
Transport.sendmessage (Message,message.getallrecipients ());//Send mail, where the second parameter is all
A set of recipient addresses
Transport.close ();

%>
<div align= "center" >
<p><font color= "#FF6600" > Sent successfully!</font></p>
<p><a href= "recmail.jsp" > Take a look at my mailbox </a><br>
<br>
<a href= "index.htm" > re-send a letter </a> </p>
</div>
<%
}catch (Messagingexception e) {
Out.println (E.tostring ());
}
%>
</body>

Pay attention to ***************************************

There are a lot of books and online articles that are written testmail.jsp in the key sections, as follows:

String Tto=request.getparameter ("to");
String Ttitle=request.getparameter ("title");
String tcontent=request.getparameter ("content");
Properties Props=new properties ();
Props.put ("Mail.smtp.host", "smtp.163.net");
Session s=session.getinstance (props);
MimeMessage message=new MimeMessage (s);

InternetAddress from=new internetaddress ("boy@163.net");
Message.setfrom (from);
InternetAddress to=new internetaddress (tto);
Message.setrecipient (message.recipienttype.to,to);

Message.setsubject (Ttitle);
Message.settext (tcontent);
Message.setsentdate (New Date ());

Store Store=s.getstore ("POP3");
Store.connect ("Pop.163.net", "Boy", "iloveyou");//log in to the mailbox in POP3 manner
Transport Transport=s.gettransport ("SMTP");
Transport.send (message);
Store.close ();

In fact, this is not reliable, because many electronic post Office SMTP server requires us to pass the authentication, so in this way send mail, only to the same mailbox (that is, the same SMTP mailbox), and sometimes even the same mailbox can not send out. I've tried many times in both ways, It turns out that the first method is the most reliable.


Well, I'm sure you'll be able to write the simplest email sending program. OK, next time we'll talk about how to write messages in HTML format.

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.