S2sh email registration successful after activation

Source: Internet
Author: User
Tags mailmessage

First, I thought like this: ① receiving data received from the client (password, user name, email)

② Encrypt the password with MD5, and connect the information with "_" (used for subsequent decomposition)

③ Send the information to a tool class to generate Mail Information and send mail (the information provided by the Action class is a long string consisting of the encrypted password and user name and mailbox)

④ The tool class used to generate Mail Information and send mail previously mentioned used another tool class (here we will note that our activation connection is composed of "http: // "+" ip address "+":/port "+"/Action "+" "), so we still need to obtain the real IP address, post Code later

⑤ Click to write data to the database after some verification in the action class (not to mention some details)

First, run the Code. This is one of my test actions,

Package com. mailactive;

Import com. commonutils. commomutils;
Import com. interceptorprivilege. employee;
Import com. interceptorprivilege. employeeservice;
Import com. utils. commonutils. base64de;
Import com. utils. commonutils. javamailactive;
Import com. opensymphony. xwork2.modeldriven;
Import org. Apache. commons. codec. Digest. digestutils;
Import org. Apache. struts2.servletactioncontext;
Import org. springframework. Beans. Factory. annotation. autowired;
Import org. springframework. Context. annotation. scope;
Import org. springframework. Mail. javamail. javamailsenderimpl;
Import org. springframework. stereotype. Controller;
Import sun. Misc. base64decoder;

Import javax. servlet. http. httpservletrequest;
Import java.net. inetaddress;
Import java.net. networkinterface;
Import java. util. enumeration;


/**
* Created by etund on 2014/10/29.
*/
@ Controller
@ Scope ("prototype ")
Public class registeraction implements modeldriven <employee> {

@ Autowired
Private employeeservice;
Private Employee model;
Private string register;

Public String getregister (){
Return register;
}

Public void setregister (string register ){
This. Register = register;
}

@ Autowired
Private javamailactive;


Public String createmail () throws exception {
// Receives and encapsulates data
// String user49f290d6e8459c53f31f97de00001086 = model. GetPassword () + "_" + model. GetUserName ();
String Password = model. GetPassword ();
String username = model. GetUserName ();
String email = model. getemail ();
// Base64 Encryption
Password = digestutils. md5hex (password );
Register = password + "_" + username + "_" + email;
System. Out. println (register + "============= register =========== ");
// Use the last string to verify whether the user was registered.
// Add the encrypted information to the email
Javamailactive. mailactive (register, email );
// Send an email
Return "createmail ";
}

Public String confirmmail (){
String [] info = new string [3];
Info = register. Split ("_");
Model. setpassword (info [0]);
Model. setusername (info [1]);
Model. setemail (info [2]);
Employeeservice. Save (model );
System. Out. println (model. tostring ());
Return "confirmmail ";
}
@ Override
Public Employee GetModel (){
If (model = NULL ){
Model = new employee ();
}
Return Model;
}
}

// The following describes two tool classes.

 

// This is the class that encapsulates Mail Information and creates mail.

Package com. utils. commonutils;

 

Import com. commonutils. commomutils;
Import org. JUnit. Runner. runwith;
Import org. springframework. Beans. Factory. annotation. autowired;
Import org. springframework. Mail. mailmessage;
Import org. springframework. Mail. simplemailmessage;
Import org. springframework. Mail. javamail. javamailsenderimpl;
Import org. springframework. Mail. javamail. mimemessagehelper;
Import org. springframework. stereotype. component;
Import org. springframework. Test. Context. junit4.springjunit4classrunner;

Import javax. annotation. resource;
Import javax. Mail. messagingexception;
Import javax. Mail. Internet. mimemessage;
Import java. util. properties;

/**
* Created by etund on 2014/10/29.
*/
@ Component
Public class javamailactive {
@ Autowired
Private javamailsenderimpl mailsender;

Public void mailactive (string register, string email ){
Try {
System. Out. println (mailsender );
System. Out. println (mailsender. GetUserName ());
// Href has IP address +: 8887 + action
String IP = commomutils. getrealip ();
// Create an email message. The difference between sending a simple email and an HTML email
Mimemessage mailmessage = mailsender. createmimemessage ();
Mimemessagehelper messagehelper = new mimemessagehelper (mailmessage );
Messagehelper. setfrom (mailsender. GetUserName ());

Messagehelper. setto (email );
Messagehelper. setsubject ("click the following information to activate your account ");
//? Register = "+ register
String href = "http: //" + IP + ": 8887" + "/mailactive_confirmmail.do? Register = "+ register;
System. Out. println (href + "---------");
Messagehelper. settext ("<HTML> <body> <a href =" + href + "> click the link below to activate" + href + "</a> </body> Properties Properties = new properties ();
Properties. Put ("mail. SMTP. Auth", "true ");
Properties. Put ("mail. SMTP. Timeout", "25000 ");
Mailsender. Send (mailmessage );
} Catch (messagingexception e ){
E. printstacktrace ();
}
}
}

// This is the class for obtaining the IP address

Public static string getrealip (){
Try {
String localip = NULL; // local IP address. If no Internet IP address is configured, it is returned.
String netip = NULL; // Internet IP Address

Enumeration <networkinterface> netinterfaces =
Networkinterface. getnetworkinterfaces ();
Inetaddress IP = NULL;
Boolean finded = false; // whether the Internet IP address is found
While (netinterfaces. hasmoreelements ()&&! Finded ){
Networkinterface ni = netinterfaces. nextelement ();
Enumeration <inetaddress> address = ni. getinetaddresses ();
While (address. hasmoreelements ()){
IP = address. nextelement ();
If (! IP. issitelocaladdress ()
&&! IP. isloopbackaddress ()
& IP. gethostaddress (). indexof (":") =-1) {// Internet IP
Netip = IP. gethostaddress ();
Finded = true;
Break;
} Else if (IP. issitelocaladdress ()
&&! IP. isloopbackaddress ()
& IP. gethostaddress (). indexof (":") =-1) {// intranet IP
Localip = IP. gethostaddress ();
}
}
}

If (netip! = NULL &&! "". Equals (netip )){
Return netip;
} Else {
Return localip;
}
} Catch (exception e ){
Throw new runtimeexception (E );
}
}

We will continue to update the data tomorrow. We have something to do today.

 

S2sh email registration successful after activation

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.