An example of sending a message------the application of construction mode

Source: Internet
Author: User

Suppose there is a Java-built electronic magazine system that periodically sends e-magazines to users ' e-mail boxes. Users can subscribe to e-Magazines via web pages, or they can stop subscribing via the Web. When a user starts a subscription, the system sends an email greeting, and when the user ends the subscription, the system sends an e-mail message saying farewell. This example is the system responsible for sending "welcome" and "Farewell" Mail module.

In this case, the product class is the "Farewell" and "welcome" messages sent to the customer. Because of the particularity of this example, two products can abstract a common interface, that is, the message information abstract class. The following is the source code for the abstract class

Import Java.util.date;import java.util.properties;import javax.mail.message;import javax.mail.MessagingException; Import Javax.mail.session;import Javax.mail.transport;import Javax.mail.internet.internetaddress;import Javax.mail.internet.mimemessage;import javax.mail.internet.mimeutility;/* * Abstract e-mail message class * */public abstract class Automessage {protected string smtphost;protected string subject= "";p rotected string body= "";p rotected string from= ""; Protected string frompassword= "";p rotected string to= "";p rotected Date sendate=null;public automessage () {}public void        Send () {Properties Properties = new Properties ();p roperties.put ("Mail.smtp.host", "smtp.qq.com");        Properties.put ("Mail.sender.username", from);        Properties.put ("Mail.sender.password", Frompassword);        Session session = Session.getinstance (properties);        MimeMessage message = new MimeMessage (session);        Transport Transport=null; try {//Sender//Below this is the setting of the sender's nick name IntErnetaddress from = new InternetAddress (Mimeutility.encodeword ("maltose") + "<" +this.from+ ">");                        Message.setfrom (from);            Recipient InternetAddress to = new InternetAddress (this.to);                        Message.setrecipient (Message.RecipientType.TO, to);                                    Mail subject Message.setsubject (this.subject);            The contents of the message can also be made plain text "Text/plain" Message.setcontent (This.body, "text/html;charset=utf-8");            The date the message was sent Message.setsentdate (this.sendate);                        Save Message Message.savechanges ();            Transport = Session.gettransport ("SMTP");            SMTP authentication is the email user name password Transport.connect (this.smtphost, This.from, This.frompassword) you use to send the email;            Send transport.sendmessage (message, message.getallrecipients ());        System.out.println ("Send success!");        } catch (Exception e) {e.printstacktrace ();}finally {if (transport!=null) {try {transport.close ();                } catch (Messagingexception e) {e.printstacktrace (); }}}}public String Getsmtphost () {return smtphost;} public void Setsmtphost (String smtphost) {this.smtphost = SMTPHost;} Public String Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public String GetBody () {return body;} public void Setbody (String body) {this.body = body;} Public String Getfrom () {return from;} public void Setfrom (String from) {this.from = from;} Public String Getfrompassword () {return frompassword;} public void Setfrompassword (String frompassword) {This.frompassword = Frompassword;} Public String Getto () {return to;} public void Setto (String to) {this.to = to;} Public Date Getsendate () {return sendate;} public void Setsendate (Date sendate) {this.sendate = Sendate;}}

Specific two message information, source code

public class Goodbymessage extends Automessage{public goodbymessage () {System.out.println ("Enter goodby Message");} public void Saygoodby () {System.out.println ("goodby.");}} public class Welcomemessage extends Automessage{public welcomemessage () {System.out.println ("Enter welcome Message");} public void Saywelcome () {System.out.println ("welcome.");}}

Because e-mail can be classified as the sender's address, recipient address, subject, content, and so on (or part). So the e-mail construction process can be divided into several parts of the construction process, including buildto (), Buildfrom (), Buildsenddate (), Buildsubject (), Buildbody () and so on. We should be able to see that the mail is the final product, while the To,from,date,subject and the mail content are part of the product, adding a director role, is a standard builder mode.

This system contains the client, the Director role (director), the abstract Builder (builder), the Concrete Builder (Welcomebuilder,goodbyebuilder), the product (Welcomemessage, Goodbyemessage) and other roles.

/* * Director role Source code * */public class Director {private Builder builder=null;public director (Builder builder) {This.builder=builde R;} public void construct (String smtphost,string from,string frompassword,string to) {builder.buildsmtphost (smtphost); Builder.buildfrom (from); Builder.buildfrompassword (Frompassword); Builder.buildbody (); Builder.buildobject (); Builder.buildto (to); Builder.builddate (); Builder.sendmail ();}}

/* * Abstract Builder Source code * */import Java.util.date;public abstract class Builder {protected Automessage msg;public Builder () {}/* * send Host part building method for mail * */public void Buildsmtphost (String smtphost) {msg.setsmtphost (smtphost);} /* * The part building method for the sender address * */public void Buildfrom (String from) {Msg.setfrom (from);} /* * Sender Password Part building method * */public void Buildfrompassword (String frompassword) {Msg.setfrompassword (Frompassword);} /* * Recipient Address Part construction method * */public void Buildto (String to) {Msg.setto (to);} /* * Send Time Part construction method * */public void Builddate () {msg.setsendate (New Date ());} /* * Message Subject Part construction method * */public abstract void buildobject ();/* * Message Content Part Construction method * */public abstract void buildbody ();/* * After the product is finished, Send mail with this method * */public void SendMail () {msg.send ();}}

/* Concrete Builder Source code * */public class Welcomebuilder extends Builder{public Welcomebuilder () {msg=new welcomemessage ();} @Overridepublic void Buildobject () {//TODO auto-generated method stubstring object= "Welcome"; Msg.setsubject (object);} @Overridepublic void Buildbody () {//TODO auto-generated method stubstring body= "Welcome"; Msg.setbody (body);}} public class Goodbyebuilder extends Builder {public goodbyebuilder () {msg=new goodbymessage ();} @Overridepublic void Buildobject () {//TODO auto-generated method stubstring object= "Byebye"; Msg.setsubject (object);} @Overridepublic void Buildbody () {//TODO auto-generated method stubstring body= "Byebye"; msg.setbody (body);}}

public class Client {public static void main (string[] args) {Builder welcomebuilder=new welcomebuilder ();D irector Directo R=new Director (Welcomebuilder);
The parameters are, mail server address, send mail address, send mail password, receive mail address director.construct ("smtp.qq.com", "[email protected]", "************", "[ Email protected] ");}

An example of sending a message------the application of construction mode

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.