zoukankan      html  css  js  c++  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;
            }
        }
    }
    

     

  • 相关阅读:
    快速排序
    Java 循环队列
    Java 用链表实现栈和队列
    Java 遍历集合时产生ConcurrentModificationException异常
    Java 使用Scanner时的NoSuchElementException异常
    Mac中安装git后,终端运行git出错,提示安装Xcode
    Java的设计模式之开篇(1)
    粒子群算法(PSO)
    mysql时间与字符串相互转换
    Jsp中使用EL表达式对字符串进行操作
  • 原文地址:https://www.cnblogs.com/snow-wolf-1/p/13387533.html
Copyright © 2011-2022 走看看