Recently, when I was studying online malls, I came across the function of sending emails. I have been using my email address to send emails before, but I don’t know how it works. So what is going on with email?
Email sending process:
Process description: First, the sender sends the email to a specific server through the client software, and then transmits it through the Email Simple Transfer Protocol (SMTP). The recipient accepts the email from the server through the POP3 protocol. This enables mutual transmission between emails.
Implementation process:
In the case of Internet access, we can directly use Sohu, NetEase and other emails to send. So how can I realize internal transmission without a network?
First, you need to install and configure a server. The video uses the Easy Mail Server. The installation of this software is relatively simple. After successful installation, there will be a failure prompt, as follows:
This problem has not affected (no problems have been encountered yet) our function can be ignored. After the installation is completed, it needs to be configured. First perform server settings, open Tools -> Server Settings: Set the domain name of your own server address (similar to the domain name of NetEase Email @ 163.com we used)
Then add a new account: Set the name and password
The effect after addition is as follows:
After the server configuration is completed, the client is installed and configured. Because the latest version of foxmail has been installed, in order not to cause confusion, it is set up with foxmail6.5, and the installation process will no longer be described.
First, you need to add a new user account (if the user who has already set up the test during the installation process, you do not need to add it again), and fill in the test account, as follows:
Click Next to set the service type and server. Pay attention to the server configuration here. Since you are sending emails locally, the sending and sending mail servers are set to localhost: localhost.
After setting it up, the next step is to test:
After success, the configuration can be closed, and the configuration is basically completed
During this configuration process, you need to pay attention to:
1. Configure the server's domain name and add some accounts
2. When configuring the client, pay attention to the settings of the receiving server address, server type and other issues.
At this point, the environment we need to send emails has been configured, and the specific code implementation process will be introduced in detail in the next article. Through this study, I learned about the basic principles of email service and the process of sending emails. At the same time, it also gave me a new understanding of some server settings, domain names and types. I had encountered the problem that foxmail could not work before, and now I basically know where the problem lies. Knowledge is always obtained through continuous practice, and requires continuous repeated learning and training.
The above basically talks about the principle of sending and how to configure the email service. The following mainly talks about some specific implementation processes of sending emails.
The code implementation sending process is mainly divided into three steps:
The first step is to obtain the connection , read the configuration file through the Properties class in Java, and call some methods of the base class to perform basic settings and connection settings.
// 1. Get the connection Properties props=new Properties();//Properties is an important class in java, mainly used to read java configuration files // Set the connection server props.setProperty("mail.host", "localhost");//Call the Hashtable method put, and set the key-value pair by calling the put method of the base class Session session=Session.getInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { // TODO Auto-generated method stub //Set your own service email address and password return new PasswordAuthentication("[email protected]","1"); } });The second step is to create a mail object , mainly encapsulating information through the Message carrier. Message is an abstract class, and the known implementation classes include MimeMessage.
// 2. Create an email object Message message=new MimeMessage(session); // Set the sender try { // Set the server message.setFrom(new InternetAddress("[email protected]")); // Set the recipient message.addRecipient(RecipientType.TO, new InternetAddress(to)); // Set the title message.setSubject("Shopping Activation Email"); // Set the email body message.setContent("<h1>Shopping Activation Email, click the link below to complete the activation operation!</h1><h3><a href='http://192.168.21.84:8080/shop/user_active.action?code="+code+" '>http://192.168.21.84:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");The third step is to send emails
// 3. Send mail Transport.send(message); In this way, the main process of sending mail is implemented, and then the main method is defined in this class: public static void main(String[] args){ // The object to be sent and the verification code sendMail("[email protected]","111111111"); } Just call the method of sending mail in the Service layer: // Send the activation mail MailUitls.sendMail(user.getEmail(), code);Specific knowledge accumulation:
1. Properties mainly obtains configuration file information. The main methods include getProperty ( String key), load ( InputStream inStream), setProperty ( String key, String value), store ( OutputStream out, String comments), clear ()
2. Session is a collection of configuration information, and its main function is to receive various configuration attribute information and initialize the JavaMail environment.
3. Message is an abstract class that implements interface Part and defines some properties
4. Transport refers to the transmission of emails, which corresponds to the store storage function.
Although the functions have been implemented, some of the classes used still need in-depth learning and research. As of now, I have only gotten started and know that in JavaMail, there are several important classes such as Properties, Session, and Message.
The above is all about this article, I hope it will be helpful to everyone's learning.