工做流二次開發之郵箱提醒

爲了考慮之後二次開發,和未來的代碼增多。調用工做流的接口,大量代碼寫在本身新建項目中。spring

工做流接口:安全

public boolean sendMail(Map lhm){
         //設置HTTP鏈接的URL地址,就是第二個應用的URL。若是在同一個計算機上能夠將192.168.1.134變成127.0.0.1或者localhost  
        
        try {
            String http="http://localhost:8080/SDKFlow/email.action";
            URL url=new URL(http);  
            //生成HttpURLConnection鏈接  
            HttpURLConnection httpurlconnection=(HttpURLConnection) url.openConnection();  
            //設置有輸出流,默認爲false,就是不能傳遞參數。  
            httpurlconnection.setDoOutput(true);  
            //設置發送請求的方式。注意:必定要大寫  
            httpurlconnection.setRequestMethod("POST");
            //設置鏈接超時的時間。不過不設置,在網絡異常的狀況下,可能會形成程序僵死而沒法繼續向下執行,因此通常設置一個超時時間。單位爲毫秒  
            httpurlconnection.setConnectTimeout(30000);  
            //設置輸出流。  
            OutputStreamWriter writer=new OutputStreamWriter(httpurlconnection.getOutputStream(), "utf-8");  
            //傳遞的參數,中間使用&符號分割。  
            writer.write("authid="+lhm.get("authid")+"&title="+lhm.get("title"));
            //用於刷新緩衝流。由於默認她會寫入到內存的緩衝流中,到必定的數據量時,纔會寫入,使用這個命令可讓他當即寫入,否則下面就到關閉流了  
            writer.flush();  
            //用於關閉輸出流,關閉以後就不能夠輸出數據了,因此要使用flush刷新緩衝流  
            writer.close();  
            //得到返回的請求嗎。  
            int responseCode=httpurlconnection.getResponseCode();  
            //表示請求成功  
            if(responseCode==HttpURLConnection.HTTP_OK){  
                System.out.println("OK"+responseCode);  
                //得到服務端的輸出流。獲得返回的數據  
                InputStream urlstream=httpurlconnection.getInputStream();  
                BufferedReader reader=new BufferedReader(new InputStreamReader(urlstream));  
                String line;  
                String tline="";  
                while((line=reader.readLine())!=null){  
                    tline+=line;  
                }  
                //輸出全部的數據  
                System.out.println(tline);  
                return true;
            }else{  
                System.out.println("ERR"+responseCode);  
            }  
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;
    }服務器

本身新建的項目爲spring+mybatis構成。網絡

controller類:session

@RequestMapping(value="/email")
    public String Email(HttpServletRequest request, HttpServletResponse response)throws Exception{
        logger.info("進入郵件控制層");
        DataSourceContextHolder.setDbType("horizon");
        String authid=request.getParameter("authid");
        String title=request.getParameter("title");
//        String authid="HZ8080815fe7c0a7015febef32b10180";
//        String title="ss";
        String str=  sdkFlowService.GetUserEmailByUserID(authid,title);
        logger.info("返回值爲:"+str);
        if(str.trim().equals("ok")){
            return "welcome";
        }else if(str.trim().equals("fail")){
            
            return "";
        }else{
            return "";
        }
        
    
    }mybatis

接口實現類:app

/**
     * 經過用戶信息ID獲取用戶信息
     * @param str 用戶ID
     * @return 用戶相關信息——獲取email
     * @throws Exception
     */
    @Transactional
    @Override
    public String GetUserEmailByUserID(String id,String title) throws Exception {
        logger.info("進入Email接口");
        DBUserInfo user = new DBUserInfo();
        user.setID(id.trim());
        DBUserInfo userifno=sdkflowMapper.getUserEmail(user);
        if(userifno.getEMAIL()==null ||userifno.getEMAIL().equals("")){//若是郵箱地址爲空,則返回字符串「fail」
            return "fail";
        }
        logger.info("獲取下節點人信息:"+userifno);
        logger.info("開始發送郵件");
        ////////////////////////////////////////////////////////////// 郵箱發送郵件
        Properties prop = new Properties();
        //協議
        prop.setProperty("mail.transport.protocol", "smtp");
        //服務器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "465");
        //使用smtp身份驗證
        prop.setProperty("mail.smtp.auth", "true");
        //使用SSL,企業郵箱必需!
        //開啓安全協議
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //
        //獲取Session對象
        Session s = Session.getDefaultInstance(prop,new Authenticator() {
            //此訪求返回用戶和密碼的對象
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication("*************.com", "*******");
                return pa;
            }
        });
        //設置session的調試模式,發佈時取消
        s.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(s);
        try {
            try {
                mimeMessage.setFrom(new InternetAddress("*****.com","*****.com"));
                
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(userifno.getEMAIL()));//給指定的人發送郵件
            //設置主題
            mimeMessage.setSubject(title);
            mimeMessage.setSentDate(new Date());
            //設置內容
            mimeMessage.setText("您有信息須要處理,詳情請點擊連接http://******/workflow");
            mimeMessage.saveChanges();
            //發送
            Transport.send(mimeMessage);
            return "ok";//成功則返回「ok」
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "fail";
    }
    socket

相關文章
相關標籤/搜索