SSH Online store---Mail sending

Source: Internet
Author: User

When registering the website account, all need to send the activation mail, then lets the registered user click activates the link to complete the registration, but then again, why need to send the mail when registering? Why not just activate it when you register? Be sure to receive an email with an activation account? What are the benefits of this website? Or what is the purpose of that? Small series thought of a few points, not very complete, but also please small partners to advise, first of all to ensure that the registered users use the correct mailbox; second, when the user password is lost, the password is more secure by retrieving or forgetting the password. Thirdly, through the effective mailbox to establish the user database, the loss users can expand two times marketing. In recent projects, encountered such a demand, registration, the need to send mail, today this blog, small series on the simple to summarize, how to achieve the message to send this function, I hope to have the need for a small partner has certain help, but also ask you a lot of advice.

First step, we need to build Mailbox server, install Mailbox server, small series has been uploaded, click to download, install service mailbox server, registered account, as shown:

The second step, we need to install the client software, the most common outlook and foxmail can, need to be aware of, must be configured Oh, such as small series on the server to create a new account [email protected], we need to configure in Foxmail, As shown in the following:

Then we write code to achieve mail delivery, is not a little bit excited nie ' (*∩_∩*), to achieve the function of mail delivery, First we need to copy the Mail.jar package and the Activation.jar package to the Lib file, the two jar package is sent by the Mail, the small series has been uploaded, click to download. The jar package has been imported, and then we can write the code, first we create a class Mailuitls under the Utils package, the code is as follows:

Package Cn.itcast.shop.utils;import Java.util.properties;import Javax.mail.authenticator;import javax.mail.Message ; Import Javax.mail.message.recipienttype;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;/** * Mail Sending tool class * @author Ding * */public class Mailuitls {/** * method of sending mail * @param to: Recipient * @p Aram Code: Activation Code */public static void SendMail (String to,string code) {/** * 1. Gets a Session object. * 2. Create an object that represents a message. * 3. Send mail Transport *///1. Get the Connection object Properties props = new properties ();p rops.setproperty ("mail.host", "localhost"); Session session = Session.getinstance (props, new Authenticator () {@Overrideprotected passwordauthentication Getpasswordauthentication () {return new Passwordauthentication ("[email protected]", "111");}}); /2. Create Message object: Message message = new MimeMessage (session);//Set Sender:try {message.setfrom (New internetaddress ("[email protected]"));//Set recipient: Message.addrecipient ( Recipienttype.to, New InternetAddress (To)),//cc cc BCC bcc//set title Message.setsubject ("Official activation mail from online store");//Set Message body: Message.setcontent ("
then we need to send an activation message in Userservice.java, as shown in the following code:

Package Cn.itcast.shop.user.service;import Org.springframework.transaction.annotation.transactional;import Cn.itcast.shop.user.dao.userdao;import Cn.itcast.shop.user.vo.user;import Cn.itcast.shop.utils.mailuitls;import cn.itcast.shop.utils.uuidutils;/** * User Module Business Layer code * @author Ding */@Transactionalpublic class UserService {// Injected userdaoprivate Userdao userdao;public void Setuserdao (Userdao userdao) {This.userdao =userdao;} Method of querying users by user name public user findbyusername (String username) {return userdao.findbyusername (username);} The business layer completes the user registration code public void Save (user user) {//data is stored in the database user.setstate (0);//0 on behalf of user activation  1 means the user has activated string code = Uuidutils.getuuid () +uuidutils.getuuid (); User.setcode (code); userdao.save (user);//Send activation mail mailuitls.sendmail ( User.getemail (), code);}}

The most critical code is the following sentence, send the activation email, at this point, our Code section has been written, we will register an account, as shown in:

Look at the small series of e-mail, have you received an email? As shown below:

Let's take a look at our database as shown in:

As we see, the data can be inserted into the database, but there is a Chinese garbled problem, how to solve it? We can set the following for JDBC, as shown in the following code:

Jdbc.driver = Com.mysql.jdbc.Driverjdbc.url = Jdbc:mysql:///shop?characterencoding=utf-8jdbc.user = Rootjdbc.password =123456
Let's re-register it again, as shown in:

Then, let's look at the database, what kind of miracle nie? As shown in the following:

Small Editor's note: this blog small part of the main simple introduction of the user registration of the time to send activation mail, generally divided into three steps, the first step of the user registration and why the registration needs to send activation mail; the second step is to send an activation email, including building a mailbox server, installing the client software, and so on; third step , the implementation of the code to send the message, in the implementation of this function, the small series made a mistake, that is, in the foxmail there is no configuration account, resulting in not receiving mail, so say must be information ah, in the next blog, the small series will continue to introduce the SSH online shopping related knowledge, wonderful not to be continued ' (*∩_∩*) ′!

SSH Online store---Mail sending

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.