Because the project requires user mailbox registration, you first import the JavaMail package, and then add the following program to the project. Package Com.bikehui.db.daoimpls;import Java.util.properties;import Javax.mail.bodypart;import javax.mail.Message; Import Javax.mail.messagingexception;import Javax.mail.multipart;import Javax.mail.session;import Javax.mail.transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.mimebodypart;import Javax.mail.internet.mimemessage;import javax.mail.internet.mimemultipart;/** * * @author Songwenju * e-mail * */public Clas s SendMail {private String from = ***@163.com "; You need to use the email address, must be 163 mailbox Private String user = "* * *";//Sender title, guaranteed and previous @ the same content as String password = "* * *"; Sender Mailbox//Constructor public SendMail () {}/** * * @param to * @param text * @param title *//* send authentication message */public void SendMail (String To,string text,string title) {Properties props = new Properties ();p rops.setproperty ("Mail.smtp.host", "smtp.163.com"); Set the properties of the mail server to send mail (here using NetEase's SMTP server) props.put ("Mail.smtp.host", "smtp.163.com"); Need to be authorized, that is, the user name and password verification, so as to pass the verification(Must have this one) props.put ("Mail.smtp.auth", "true"); Build a session session session with the props object you just set up = session.getdefaultinstance (props); With this sentence, you can display process information at the console during the sending of the message for debugging purposes (you can see the process of sending the message on the console) Session.setdebug (true); Define the message object with the session parameter MimeMessage msg = new MimeMessage (session); Load Sender address Try{message.setfrom (new internetaddress (from)); Message.addrecipient (Message.RecipientType.TO, New internetaddress (to)); Load recipient address Message.setsubject (title); Load caption Multipart Multipart = new Mimemultipart (); Add various parts of the message to the multipart object, including text content and attachments bodypart Contentpart = new MimeBodyPart (); Set the text content of the message contentpart.setcontent (text, "Text/html;charset=utf-8"); Multipart.addbodypart (Contentpart); message.setcontent (multipart); Message.savechanges (); Save changes Transport Transport = Session.gettransport ("SMTP"); Connect the server's mailbox Transport.connect ("smtp.163.com", user, password); Send the message out transport.sendmessage (message, message.getallrecipients ()); Transport.cloSe ();} catch (Messagingexception e) {e.printstacktrace ();}} /** * * @param to * @param userName * @return */public string sendregistmail (String to,string userName) {string Registerid = "" + math.random () * Math.random (); String url = "http://***/registermailbackservlet?registerid=" + registerid+ "&username=" +username;// Click the link to return to the project in string text = "<p> Dear user, Hello!" Thank you for registering bike network, please click the link below to complete the email verification:</p> "+" <a href= ' "+url+" > "+url+" </a><br/><p></p > If the link click is invalid, you can copy the link to the browser and open it directly. <p>bike Bicycle Net </p> "; Send HTML-formatted mail to the mailbox string title = "[Bike] website registration"; SendMail (to,text,title);//wait for the user to click the link in the mailbox to return to your website return registerid; public static void Main (String []args) {//Do test with SendMail s = new SendMail (); S.sendmail ("[email protected ] "," Hello, Cloud Water "," [bike] Retrieve your account password ");}}
Jsp,java Project Mailbox Registration Send mail