jmail 发邮件

public void Send() throws Exception {
        Transport transport = null;
                try {

            if (StringUtil.isBlank(this.host)) {
                throw new NetException(6002);
            }
            if (StringUtil.isBlank(this.account)) {
                throw new NetException(6005);
            }
            if (StringUtil.isBlank(this.mailPattern)
                    && StringUtil.isBlank(this.from))
                throw new NetException(6003);
            if (StringUtil.isBlank(this.to) && Utility.isBlankList(this.toList)
                    && StringUtil.isBlank(this.cc)
                    && Utility.isBlankList(this.ccList)) {
                throw new NetException(6004);
            }
            if (StringUtil.isBlank(this.from)
                    && StringUtil.isNotBlank(this.account)
                    && StringUtil.isNotBlank(this.mailPattern))
                this.from = this.account + this.mailPattern;

                
            Session session = getSession();
            MimeMessage message = getMessage(session);
            transport = session.getTransport(PROTOCOL);
            transport.connect(this.host, this.account, this.password);
            transport.sendMessage(message, message.getAllRecipients());
            
        } catch (NetException e) {
            Log.logError(this, "send", e.getCode(), null, e);
            throw e;
        } catch (MessagingException e) {
            Log.logError(this, "send", 6006, null, e);
            throw new NetException(6006, e);
        } finally{
            if( transport != null && transport.isConnected() ){
                transport.close();
            }
        }
    }

posted @ 2017-09-11 10:44  developer_os  阅读(198)  评论(0编辑  收藏  举报