1. 程式人生 > 實用技巧 >java設計模式應用

java設計模式應用

1、builder設計模式(郵件工具類)

public class EmailMessage {

    private Message message;

    private static final String FROM = "sender";
    private static final String PASSWORD = "****";
    private static Session session;

    static {
        Properties properties = new Properties();
        // 連線協議
        properties.setProperty("mail.transport.protocol", "smtp");
        // 主機名
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        // 埠號
        properties.put("mail.smtp.port",465);
        properties.setProperty("mail.smtp.auth", "true");
        // 設定是否使用ssl安全連線 ---一般都使用
        properties.setProperty("mail.smtp.ssl.enable", "true");
        // 設定是否顯示debug資訊 true 會在控制檯顯示相關資訊
        properties.setProperty("mail.debug", "true");

        session = Session.getInstance(properties);
    }


    public static void main(String[] args) throws MessagingException {

        new EmailMessage
                .MessageBuilder(EmailMessage.session)
                .sender(EmailMessage.FROM)
                .receiver("receiver")
                .subject("java傳送郵件")
                .content("<h3>你好,Jack</h3><p>很高興你給的我的回信,這給了我很多靈感。</p>","text/html;charset=utf-8")
                .builder().send();
    }



    public void send() throws MessagingException {
        Transport transport = session.getTransport();
        transport.connect(FROM,PASSWORD);
        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
    }

    public EmailMessage(MessageBuilder builder) {
        this.message = builder.getMessage();
    }


    public static class MessageBuilder{
        private Message message;

        public MessageBuilder(Session session) {
            this.message = new MimeMessage(session);
        }

        public MessageBuilder sender(String sender) {
            try {
                this.message.setFrom(new InternetAddress(sender));
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return this;
        }

        public MessageBuilder receiver(String receiver) {
            try {
                message.setRecipient(Message.RecipientType.TO,new InternetAddress(receiver));
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return this;
        }

        public MessageBuilder receivers(List<String> receivers) {
            try {
                InternetAddress[]  internetAddress = new InternetAddress[receivers.size()];
                int i = 0;
                for (String receiver : receivers) {
                    internetAddress[i] = new InternetAddress(receiver);
                    i = i + 1;
                }
                message.setRecipients(Message.RecipientType.TO,internetAddress);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return this;
        }

        public MessageBuilder subject(String subject) {
            try {
                this.message.setSubject(subject);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return this;
        }


        public MessageBuilder content(String content,String contentType) {
            try {
                this.message.setContent(content,contentType);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return this;
        }

        public EmailMessage builder() {
            return new EmailMessage(this);
        }

        public Message getMessage() {
            return message;
        }
    }
}