Learning --- e-mail activation

2019/10/21

    Project - online store SSH (Struts + Spring + Hinberate) modules: Mail registration module is activated in the absence of networking, using MailServer server + FoxMail clients achieve-mail account to send mail

     Use tools: MailServer Mail Server + Foxmail Client 6.5

 Process: Users fill out registration forms, were randomly assigned registration code Code, by sending mail to a user's mailbox, the user clicks on the link to activate, thereby completing registration

  1.MailServer mail server configuration :( easily mail server), preferably mounted to the disk C

    Set single domain name server domain name xxx.com

    Account -> New Account (an account of which will be set to send a verification email in the late official coding)

  2.FoxMail client configuration

   In the absence of networking, fill out the account has been registered in the MailServer using the account to accept verification email

    Mailbox -> New Mailbox 2. Account! ! ! Will be one of the server and the client's address changed to localhost !!

 

 coding

  MailUtils.java

	
    public static void sendMail(String to,String code){
		/**
		 * 1. Obtain a Session object.
		 * 2. Create a message on behalf of the object Message.
		 * 3. Send Mail Transport
		 */
		// 1. Obtain the connection object
		Properties props = new Properties();
		props.setProperty("mail.host", "localhost");
		Session session = Session.getInstance(props, new Authenticator() {

			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				new new PasswordAuthentication return ( " [email protected]", "123" );  // modify the system of official mail account
			}
			
		});
		// 2. Create a message object:
		Message message = new MimeMessage(session);
		// Set From:
		try {
			message.setFrom (new new InternetAddress ( "[email protected]" ));   // Note to modify the official mailbox for their needs
			// Set To:
			message.addRecipient(RecipientType.TO, new InternetAddress(to));
			// CC CC BCC BCC
			// set title
			message.setSubject ( "XXXX from the official activation email");
			// set the message body:
			message.setContent ( "XXXX official activation <h1> Mail! the link below to complete the activation! </ h1> <h3> <A href = ' http://127.0.0.1:8083/ Shop / user_active.action? code = "+ code +" '> http://127.0.0.1:8083/shop/user_active.action?code= "+ code +" </a> </ h3> "," text / html; charset = UTF-8 ") ;
			// 3. Send e-mail:                                        // path to a label, to write according to their own circumstances
			Transport.send(message);
		} catch (AddressException e) {
			e.printStackTrace ();
		} catch (MessagingException e) {
			e.printStackTrace ();
		}
		
	}
	// test code
	public static void main(String[] args) {
		sendMail("[email protected]","11111111111111");
	}

  

 // modify the user database active state (0 | 1)

/ ** 
* user activation methods
* /
public String Active () {
// The activation code to query the user:
the User existUser = userService.findByCode (user.getCode ());
// Analyzing
IF (existUser == null) {
/ / activation code error
this.addActionMessage ( "activation failure:! error activation");
} the else {
// activate successfully
// modify its status
existUser.setState (. 1);
existUser.setCode (null);
userService.update (existUser);
this.addActionMessage ( "activate success: please login go!");
}
return "msg";
}

 

 

 

 

 

 

 state state: 0/1 0 indicates already active, indicates activation code, when the user registration is not active when the randomly generated code, but when the user completes the activation, to be NULL code

 

Guess you like

Origin www.cnblogs.com/who-am-i/p/11715652.html