JAVA utility -- javamail and utility -- javamail
Before implementing javamail, you must first set up an email server.
James needs to use the Tomcat server when developing WEB programs, but the Tomcat server does not support Mail processing. Therefore, to send emails, you need to configure a separate Java Mail server, james is an enterprise-level Mail server. It fully implements SMTP (Simple Mail Transfer Protocol, Simple Mail Transfer Protocol) and POP3 (Post Office Protocol 3, the third version of Post Office Protocol) and the NNTP (Network News Transport Protocol, Network News transmission Protocol) Protocol. The James server is also a mail application platform. It allows users to easily implement powerful Mail applications. You can use Apache's James server to directly log on to http://james.apache.org/and download the jamesserver. You can search for and configure the James server on the Internet. If you have a large number of blog posts for reference, I will not repeat them here. It is worth noting that the configuration process involves
telnet localhost 4555
If there is no response, you must go to the control panel --> Programs and functions --> enable or disable the Telnet service in the windows function.
Introduction and configuration of JavaMail
JavaMail is a set of specifications used by Java to handle email processing. It can be used to conveniently send mails, or JavaMail can be used to develop applications similar to Microsoft Outlook.
JavaMail core class:
Example: sending a common mail is completed by using the previously configured James server and JavaMail (JAF) class. However, you must note that, since all mailbox users are now saved on the James server, you must first write an operation class that can be used to verify the user name and password on the server. This class must inherit javax. mail. authenticator class, And the getPasswordAuthentication () method must be overwritten.
<% @ Page contentType = "text/html; charset = GBK "language =" java "%>
Mydeal. jsp
<% @ Page contentType = "text/html; charset = GBK" language = "java" errorPage = "" %> <% @ page import = "java. util. * "%> <% @ page import =" javax. mail. * "%> <% @ page import =" javax. mail. internet. * "%> <% @ page import =" javax. activation. * "%> <% @ page import =" org. apache. commons. mail. * "%> <% try {request. setCharacterEncoding ("GBK"); String from = request. getParameter ("from"); String to = request. getParameter ("to"); String Subject = request. getParameter ("subject"); String messageText = request. getParameter ("content"); String password = request. getParameter ("password"); String mailserver = "localhost"; // SMTP server SimpleEmail = new SimpleEmail (); email when sending an email over the LAN. setHostName (mailserver); // set the email address of the email server. setAuthentication (from, password); // set the email user name and password email. setFrom (from); // set the sender address email. addTo (to); // set the email address of the recipient. setSubject (s Ubject); // set the subject email. setSentDate (new Date (); // set the sending time email. setMsg (messageText); // sets the sent message email. send (); // send the email out. println ("<script language = 'javascript '> alert ('email sent! '); Window. location. href = 'index. jsp '; </script> ");} catch (Exception e) {e. printStackTrace (); System. out. println ("error generated when sending an email:" + e. getMessage (); out. println ("<script language = 'javascript '> alert ('email sending failed! '); Window. location. href = 'index. jsp'; </script> ") ;}%>
Package to be imported: mail. jar