Java registered account Mailbox activation verification implementation

Source: Internet
Author: User
Tags stringbuffer

Java registered account Mailbox activation verification implementation

1. Need to join the Mail.jar to send mail:

Http://www.oracle.com/technetwork/java/javamail/index-138643.html

?

2. A tool class that encrypts strings through MD5:

Class= "Java" >import java.io.unsupportedencodingexception;import Java.security.messagedigest;import java.security.nosuchalgorithmexception;/** * Created by: Zy * Created: October 22, 2014 2:36:19 * Class Description: A tool class that encrypts a string */public class Md5ut   Il {/** * encrypts the source string through MD5 to a byte array * @param source * @return */public static byte[] Encodetobytes (string source) {byte[] Result = null;try {MessageDigest MD = messagedigest.getinstance ("MD5"); Md.reset ();//Reset Md.update (Source.getbytes ("UTF-8")) ;//Add source that needs to be encrypted result = Md.digest ();//encryption} catch (NoSuchAlgorithmException e) {e.printstacktrace ();} catch ( Unsupportedencodingexception e) {e.printstacktrace ();} return result;} /** * Encrypts the source string to a 32-bit 16 binary number by MD5 * @param source * @return */public static string Encodetohex (string source) {byte[] data = ENC Odetobytes (source);//first encrypted as byte array stringbuffer hexsb = new StringBuffer (); for (int i = 0; i < data.length; i++) {String hex = Integer.tohexstring (0xFF & Data[i]); if (hex.length () = = 1) {hexsb.append ("0");} Hexsb.append (hex);} Return HEXSB.TOSTRing ();} /** * Verify string matches * @param unknown string to be validated * @param okhex using MD5 encrypted 16 binary string * @return */public static Boolean validate (String u Nknown, String Okhex) {return okhex.equals (Encodetohex (unknown));}}

?

?

3. The tool class for sending mail:

Import Java.util.date;import java.util.properties;import Javax.mail.authenticator;import Javax.mail.Message;import Javax.mail.messagingexception;import Javax.mail.passwordauthentication;import Javax.mail.Session;import Javax.mail.transport;import Javax.mail.internet.addressexception;import javax.mail.internet.InternetAddress; Import javax.mail.internet.mimemessage;/** * Created by: Zy * Created: October 22, 2014 3:04:11 * class Description: Send Mail Tool class */public class Sendmailut Il {public static final string HOST = "smtp.163.com";p ublic static final String PROTOCOL = "SMTP";p ublic static final int PORT = 25;public static final String SENDER = "***@163.com";//This requires the same site as host static final string senderpwd = "****** * * ";/** * Gets the session used to send mail * @return */public static session getsession () {Properties props = new Properties ();p rops.put (" Mail.smtp.host ", host);//Set server address Props.put (" Mail.store.protocol ", protocol);//Set Protocol Props.put (" Mail.smtp.por    T ", port);//Set Port Props.put (" Mail.smtp.auth ", true);            Authenticator Authenticator = new Authenticator () {@Override protected Passwordauthenticati            On Getpasswordauthentication () {return new passwordauthentication (SENDER, senderpwd);        }};        Session session = Session.getdefaultinstance (Props,authenticator); return session;} /** * Send mail * @param receiver * @param content */public static void Send (string receiver, string content) {Session session = GetSession (); try {System.out.println ("-------Start sending-------"); Message msg = new MimeMessage (session);//Set Message property Msg.setfrom (new InternetAddress (SENDER)); internetaddress[] Addrs = {New InternetAddress (receiver)};msg.setrecipients (Message.RecipientType.TO, Addrs); Msg.setsubject ("Happy Net-account activation");                        Msg.setsentdate (New Date ()); Msg.setcontent (content, "text/html;charset=utf-8");//Start sending transport.send (msg); System.out.println ("-------Send complete-------");} catch (Addressexception e) {e.printstacktrace ();} catch (Messagingexception e) {E. Printstacktrace ();}}} 

?

? 4. The servlet or action that sent the message:

User user = Userdao.getuser (); if (user! = null && USER.GETQYBJ ()! = 1) {String email = user.getemail ();  StringBuffer content = new StringBuffer ("

?

5. Click the link to verify the servlet or action:

String userName = request.getparameter ("name"); String email = request.getparameter ("email"); Userpostdao Userdao = new Userpostdao (); User user = Userdao.getuserbyemail (email), if (user = = null) {destination = "/register/activefail.jsp"; Request.setattribute ("msg", "link Error");} else {String userNameMd5 = Md5util.encodetohex (user.getusername () + "Audit Pass"); Username.equals (USERNAMEMD5)) {Request.setattribute ("msg", "link Error");d estination = "/register/activefail.jsp";} else {Boolean done = Userdao.updateuserstatusbyemail (e-mail, 1), if (done) {Request.setattribute ("UserId", User.getuserid ()); Request.setattribute ("UserName", User.getusername ());d estination = "/register/activesuccess.jsp ";} else {Request.setattribute ("msg", "Activation Error");d estination = "/register/activefail.jsp";}}}

?



?

?

      • View Picture Attachments

Java registered account Mailbox activation verification implementation

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.