SSH Framework Online Mall Project 25th War on the use of Java email to users to send mail _java

Source: Internet
Author: User
Tags sessions ssh

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

1. How to send email in Java

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

 public class Sendemaildemo {public static void main (string[] args) throws Exception
    {//1. Login mail Client (create session sessions) Properties prop = new properties ();  
    Prop.setproperty ("Mail.transport.protocol", "SMTP");
    Session sessions are created: Session.getdefaultinstance (prop);
    Set debug mode to debug the Send Message Session.setdebug (TRUE);
    Create a Message object messages = new MimeMessage (session); Write a letter Message.setsubject ("Welcome to my CSDN Blog homepage!")
    "); 
    Body Content message.setcontent ("Welcome to visit my csdn Blog homepage: http://blog.csdn.net/eson_15" + ", Mody ~", "Text/html;charset=utf-8");
    Annex Person Address Message.setfrom (new internetaddress ("nishengwus@163.com"));
    Transport transport = Session.gettransport ();
    Link Mail server Authentication information transport.connect ("smtp.163.com", "Nishengwus", "xxxxx password");
    Set the recipient address and send mail transport.sendmessage (message, New Internetaddress[]{new internetaddress ("694076359@qq.com")});
  Transport.close (); }
}

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

2. Package Send mail function

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:

The extracted Emailutil interface is public interface Emailutil {public abstract void SendEmail (string emailaddress, string id); //emailutilimpl Implementation Class @Component ("Emailutil") public class Emailutilimpl implements Emailutil {//Parameters receive customer's mailbox address and order number @Ov Erride public void SendEmail (string emailaddress, string id) {//1. Login to mail client (create session sessions) Properties prop = new
    Properties ();
    Session session = NULL;
    Message message = NULL;
    Transport transport = NULL;
      try {prop.setproperty ("Mail.transport.protocol", "SMTP");
      Sessions session = Session.getdefaultinstance (prop) was created;
      Set debug mode to debug the Send Message Session.setdebug (TRUE);
      Create a Mail object message = new MimeMessage (session);
      Letter Message.setsubject ("Online Mall order Feedback"); Text content message.setcontent ("Hello Customers, welcome you to patronize the online mall, order" + ID + "has paid success!")
      "," Text/html;charset=utf-8 ");      
      Annex Person Address Message.setfrom (new internetaddress ("soft03_test@sina.com")); Transport = Session.gettransport();

      Link Mail server Authentication information transport.connect ("smtp.sina.com", "Soft03_test", "soft03_test");
    Set the recipient address and send mail 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. Perfect Payaction

After completing the encapsulation of the tool class, we then put the tool class into the baseaction and inject it through the @resource annotation for action, and we refine the Backbank () method in the previous payaction, as follows:

@Controller ("Payaction")
@Scope ("prototype") Public
class Payaction extends baseaction<object> Implements Parameteraware {

  //omit unrelated code

  ... public void Backbank () {
    backdata backdata = (backdata) model;
    SYSTEM.OUT.PRINTLN (model);
    Boolean IsOK = Payservice.checkbackdata (backdata);
    if (IsOK) {
      //1. Update order status, parameters are passed in according to the situation in the database to test the
      Forderservice.updatestatusbyid (integer.valueof ( 201605006), 2);
      2. According to the user's mailbox address, send message
      String EmailAddress = BACKDATA.GETR8_MP (). Split (",") [0];
      Emailutil.sendemail (EmailAddress, Backdata.getr6_order ());
      3. Send SMS, the next blog to introduce the function of sending SMS
      System.out.println ("----success!! ----");
    } else {
      System.out.println ("----false!!! ----");
    }
  }
}

From the information returned from the PO r6_order parameter is saved in the order number, R8_MP parameter is the user's mailbox and telephone, the first is the mailbox the second is the phone, separated by commas, so we first to get the user's e-mail address, and then to send mail. Well, the function of sending a message to the user after the payment is completed is complete.

Original link: http://blog.csdn.net/eson_15/article/details/51475046

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.