When the user has purchased the product, we should send an email to the user to tell him that the order has been generated and the email address is obtained from the user's basic information. Okay, let's first look at the method of sending emails in Java.
1. Methods for sending email in java
Before improving this project, let’s first review how emails are sent in Java. First of all, you must need to send emails: mail.jar, import them into the lib directory. Okay, let’s write an ordinary java program to review the knowledge points of java email:
public class SendEmailDemo { public static void main(String[] args) throws Exception { //1. Log in to the email client (create session session) Properties prop = new Properties(); prop.setProperty("mail.transport.protocol", "smtp"); //Create session Session session Session session = Session.getDefaultInstance(prop); //Set debug mode to debug send message session.setDebug(true); //Create an email object Message message = new MimeMessage(session); //Write a letter message.setSubject("Welcome to my CSDN blog homepage!"); //Text content message.setContent("Welcome to my CSDN blog homepage: http://blog.csdn.net/eson_15" + ", Momoda~", "text/html;charset=utf-8"); //Attacher's address message.setFrom(new InternetAddress("[email protected]")); Transport transport = session.getTransport(); //Certification information of the link mail server transport.connect("smtp.163.com", "nishengwus", "xxxxx Password"); // Set the recipient address and send the email transport.sendMessage(message, new InternetAddress[]{new InternetAddress("[email protected]")}); transport.close(); }}The above is the process of sending emails in Java: Create a session > Encapsulate email information > Set sender address > Set recipient address > Send.
2. Encapsulate sending mail function
After reviewing the method of sending emails by Java, we encapsulate this process into a tool class, create a new EmailUtilImpl implementation class, and extract it into the EmailUtil interface after completion, as follows:
//The extracted EmailUtil interface public interface EmailUtil { public abstract void sendEmail(String emailAddress, String id);}//EmailUtilImpl implementation class @Component("emailUtil")public class EmailUtilImpl implements EmailUtil { // Parameters receiving customer's email address and order number @Override public void sendEmail(String emailAddress, String id) { // 1. Log in to the email client (create session session) Properties prop = new Properties(); Session session = null; Message message = null; Transport transport = null; try { prop.setProperty("mail.transport.protocol", "smtp"); // Create session session = Session.getDefaultInstance(prop); // Set debug mode to debug send message session.setDebug(true); // Create an email object message = new MimeMessage(session); // Write a letter message.setSubject("Online mall order feedback"); // Text content message.setContent("Hello customer, welcome to visit the online mall, order" + id + "Paid successfully!", "text/html;charset=utf-8"); // Attachment address message.setFrom(new InternetAddress("[email protected]")); transport = session.getTransport(); // Authentication information of the link mail server transport.connect("smtp.sina.com", "soft03_test", "soft03_test"); // Set the recipient's address and send the email transport.sendMessage(message, new InternetAddress[] { new InternetAddress(emailAddress) }); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); throw new RuntimeException(e); } } }3. Improve payAction
Complete the encapsulation of the tool class. Next, we put the tool class into BaseAction and inject it through the @Resource annotation for use by Action. Below we will improve the backBank() method in the previous payAction, as follows:
@Controller("payAction")@Scope("prototype")public class PayAction extends BaseAction<Object> implements ParameterAware { //Omit irrelevant code... public void backBank() { BackData backData = (BackData)model; System.out.println(model); boolean isOK = payService.checkBackData(backData); if(isOK) { //1. Update the order status, the parameters are transmitted in by itself according to the situation in the database, and are used to test forderService.updateStatusById(Integer.valueOf(201605006), 2); //2. Send email according to the user email address String emailAddress = backData.getR8_MP().split(",")[0]; emailUtil.sendEmail(emailAddress, backData.getR6_Order()); //3. Send mobile phone text messages, the next blog introduces the function of sending text messages System.out.println("---success!!----"); } else { System.out.println("----false!!!-----"); } }} In the information returned from Yibao, the r6_Order parameter saves the order number, and the r8_MP parameter is the user's email and phone number. The first one is the email address and the second one is the phone number, separated by commas, so we first need to obtain the user's email address and then send the email. OK, the function of sending emails to the user after payment is completed is completed.
Original link: http://blog.csdn.net/eson_15/article/details/51475046
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.