"SSH Online Mall project Combat 25" Use Java email to send users mail

Source: Internet
Author: User
Tags sessions how to send mail

When the user buys the product, we should send a message to the user, tell him the order has generated such information, the email address is from the user's basic information to obtain, OK, first of all, we look at the method of sending mail in Java.

1. How to send email in Java

Before perfecting this project, first to review how to send mail in Java, first of all, must send the message jar package: mail.jar , import into the Lib directory, OK, let's write a common Java program to review the Java email knowledge points:

 Public classSendemaildemo { Public Static voidMain (string[] args)throwsException {//1. Log in to the mail client (create session sessions)Properties prop =NewProperties (); Prop.setproperty ("Mail.transport.protocol", "SMTP"); //session sessions are createdSession session =session.getdefaultinstance (prop); //set debug mode to debug Send MessageSession.setdebug (true); //Create a Message objectMessage message =NewMimeMessage (session); //WriteMessage.setsubject ("Welcome to my CSDN Blog homepage! "); //Body ContentMessage.setcontent ("Welcome to my CSDN Blog home page: http://www.baidu.com" + "," Da ~ "," Text/html;charset=utf-8 "); //Attachment person AddressMessage.setfrom (NewInternetAddress ("[Email protected]")); Transport Transport=Session.gettransport (); //authentication information for linked mail serversTransport.connect ("smtp.163.com", "Shanhe", "This password is you turn on the mailbox stamp and IMAP after NetEase gives you the password (not the original login password)"); //set the recipient address and send the messageTransport.sendmessage (Message,Newinternetaddress[]{NewInternetAddress ("[Email protected]")});    Transport.close (); }}

This is the process of sending mail in Java: Create a session –> encapsulate message information –> set the sender address –> set the recipient address –> send.

2. Encapsulating the Send mail feature

After reviewing the Java method of sending mail, we encapsulate this process into a tool class, create a new Emailutilimpl implementation class, and then extract the Emailutil interface as follows:

//Emailutil interface after extraction Public InterfaceEmailutil { Public Abstract voidSendEmail (string emailaddress, String id);}//Emailutilimpl Implementation Class@Component ("Emailutil") Public classEmailutilimplImplementsEmailutil {//parameters receive customer's email address and order number@Override Public voidSendEmail (string emailaddress, string id) {//1. Log in to the mail client (create session sessions)Properties prop =NewProperties (); Session Session=NULL; Message Message=NULL; Transport Transport=NULL; Try{Prop.setproperty ("Mail.transport.protocol", "SMTP"); //session sessions are createdSession =session.getdefaultinstance (prop); //set debug mode to debug Send MessageSession.setdebug (true); //Create a Message objectMessage =NewMimeMessage (session); //WriteMessage.setsubject ("Online Shop order Feedback"); //Body ContentMessage.setcontent ("Customer Hello, welcome you to visit online shopping mall, order" + ID + "has been paid success! "," Text/html;charset=utf-8 "); //Attachment person AddressMessage.setfrom (NewInternetAddress ("[Email protected]")); Transport=Session.gettransport (); //authentication information for linked mail serversTransport.connect ("smtp.sina.com", "Soft03_test", "Soft03_test"); //set the recipient address and send the messageTransport.sendmessage (Message,NewInternetaddress[] {Newinternetaddress (EmailAddress)}); } Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); } finally {                     Try{transport.close (); } Catch(messagingexception e) {e.printstacktrace (); Throw NewRuntimeException (e); }        }    }
3. Perfecting Payaction
@Controller ("Payaction") @Scope ("Prototype") Public classPayactionextendsBaseaction<object>ImplementsParameteraware {//omit irrelevant code ...     Public voidBackbank () {backdata backdata=(backdata) model;        SYSTEM.OUT.PRINTLN (model); BooleanIsOK =Payservice.checkbackdata (backdata); if(IsOK) {//1. Update the order status, the parameters are in accordance with the situation in the database, to testForderservice.updatestatusbyid (Integer.valueof (201605006), 2); //2. According to the user email address, send mailString EmailAddress = BACKDATA.GETR8_MP (). Split (",") [0];//user's mailbox and phone Emailutil.sendemail (EmailAddress, Backdata.getr6_order ());//ePRO Returns the number of the order//3. Send SMS, next blog to introduce the function of sending SMSSYSTEM.OUT.PRINTLN ("----success!! ----"); } Else{System.out.println ("----FALSE!!! ----"); }    }}

From the information returned by the R6_order parameter is stored in the order number, the R8_MP parameter is the user's mailbox and phone, the first mailbox is the second phone, separated by commas, so we first want to get the user's email address, and then send the message. Well, the ability to send a message to the user after payment is complete.

"SSH Online Mall project Combat 25" Use Java email to send users mail

Related Article

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.