Java Online Mall development of the mail delivery function (full) _java

Source: Internet
Author: User

Recently in the study of the online mall, access to a message to send the function. have been using the mailbox to send mail, but the specific how the operation is not know what is going on. So what exactly is the message sent?

Mail Send process:

Process Description: first of all, the sender sends the message through the client software to a specific server and then transmits it via the email Simple Transfer Protocol (SMTP), where the recipient accepts the message from the server via the POP3 protocol. This enables the message to be transmitted to each other.

Implementation process:

In the case of network, we can directly use Sohu, NetEase and other mailboxes to send. So how do you implement internal delivery without a network?

First need to install the configuration of a server, the video is using an easy mail server, the software installation is relatively simple, after the installation is successful will have a failure prompt, as follows:

This issue has no effect (for the time being there are no problems) our functionality can be ignored. After the installation is complete, you need to configure it. First server settings, open tools--> server settings: Set their own server address domain name (similar to the NetEase mailbox we use the domain name @ after 163.com)

Then add a new account: Set the name and password

The effect after adding is as follows:

After the server configuration is complete, the client installs and configures, because the machine has already installed the latest version of Foxmail, in order to not cause confusion, so set with foxmail6.5, where the installation process is no longer described.

To add a new user account first (if the user who has already set up the test during the installation process is not added), fill in the test account as follows:

Click Next, set the service type and server, here need to pay attention to the server configuration, because they are in the local mail delivery, so send, send mail server are set to Local: localhost.

After you set it up, Next, test:

After a successful shutdown configuration, the configuration is basically completed

You need to be aware of this during this configuration:

1, configure the server's domain name, and add some accounts

2, when configuring clients, pay attention to the server address settings, server types and so on.

At this point, the environment we need to send the message has been configured, the specific code implementation process, will be detailed in the next chapter. Through this study, understand the basic principle of mail service, know that we usually send mail process. At the same time, also let oneself to some server settings, domain names and types have a new understanding, before also encountered Foxmail can not work the problem, now basically know where the problem is. Knowledge, always in the continuous practice of the acquisition, but also the need for continuous learning and exercise.

The above basically will send the principle to say a bit, and how to configure the mail service. The following is mainly about the delivery of some specific implementation process.

The implementation of the code delivery process is divided into three main steps:

The first step is to get the connection , read the configuration file through the properties class in Java, and call some methods of the base class to base the settings and the connection settings.

 1, obtain 
  the connection Properties Props=new properties ();//properties is an important class in Java, mainly for reading Java profiles 
  //Setting up connected Servers 
  Props.setproperty ("Mail.host", "localhost");//Call Hashtable method put to set the key value pair session by calling the base class's Put method 
  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 ("service@pengbo.com", "1"); 
  }   
   
  ); 

The second step is to create the mail object , mainly through message this carrier to encapsulate information, message is an abstract class, the known implementation class has mimemessage.

2, create mail object message 
  message=new MimeMessage (session); 
Set sender 
  try { 
   //Set service end 
  Message.setfrom (New internetaddress ("service@pengbo.com"));  Set recipient 
  message.addrecipient (recipienttype.to, New internetaddress (to));  set 
  the title Message.setsubject ("Shopping activation Mail");  set the message body 
  message.setcontent (" 
 

The third step is to send the message

3, send mail 
transport.send (message); 
The main process for sending the message is implemented, and then the main method is defined in this class: public 

static void Main (string[] args) { 
  //objects to send and Authenticode 
   sendMail (" A@pengbo.com "," 11111111 "); 
  } 
 In the service layer to make calls to send mail method can: 

//Send activation mail 
  mailuitls.sendmail (user.getemail (), code); 
 

Specific accumulation of knowledge:

1, Properties of the main access to profile information, the main methods are GetProperty (string key), load (InputStream instream), SetProperty (string key, String val UE), Store (OutputStream out, String comments), clear ()

2, session is a pair of configuration information set, the main role is to receive a variety of configuration attribute information, initialization of javamail environment

3. Message is an abstract class that implements the interface part and defines some attributes

4, transport refers to the transmission of mail, and corresponding to the store storage function.

Although the functionality is implemented, some of the classes used in it require in-depth study and research. For now, just have a primer, know that in the JavaMail, there are properties, sessions, message these important classes.

The above is the entire content of this article, I hope to help you learn.

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.