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>
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:
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.
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.