Winmail开发模拟登录及获取邮件接口-java(二)
进入今天获取winmail邮件接口,winmail官方提供的接口不少,举个有代表性的栗子:
在项目中取得winmail用户的新邮件数/新邮件列表
参照安装目录中:D:Magic Winmail\server\webmail\doc\get_newmsg_num\readme.txt 说明
//本例为测试demo 本人用java和js两总方法试过,都ok
<!DOCTYPE html>
<html>
<head>
<title>test.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<a type="button" onclick="testNum()" href="">num</a><br>
<a type="button" onclick="testList()" href="">list</a>
</body>
</html>
<!-- 该文件来源为:winmail安装目录-> D:Magic Winmail\server\webmail\doc\encrypt -->
<script type="text/javascript" src="../js/common/encrypt.js"></script>
<script>
function testNum(){
//Crypt_Encode为加码函数
var b = Crypt_Encode("Aa123456");
// 邮件夹 &folder=xxx 有四个值参照readme
window.open("http://localhost:6080/getmsgnum.php?user=admin&onlynew=1&folder=INBOX&pass=" + encodeURIComponent(b),"_blank");
}
function testList(){
//&folder=privatefolder
var b = Crypt_Encode("Aa123456");
window.open("http://localhost:6080/getmsglist.php?user=admin&onlynew=1&folder=INBOX&pass=" + encodeURIComponent(b),"_blank");
}
</script>
以上是测试代码,如果是后台调用未读邮件和列表接口,拿到密码后必须匹配winmail的Crypt_Encode加密算法
,如果你牛掰,自己可以研究js中的加密,然后自创。那么如何在java 中,获取到Crypt_Encode函数加密后的值呢:
//后期方法升级 str 要执行的字段
/**
* str 传入字段值
* request 获取项目的发布路径用 如果是本地调试可以为null
*/
public String getWinmailPwdJavascript(String str,HttpServletRequest request) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
// 读js文件
String path = "js/common/encrypt.js";
String webroot_server_path="";
//服务器地址
//if(request!=null){
webroot_server_path = request.getSession().getServletContext().getRealPath("/") + path;
}
//本地地址
String webroot_local_path = GlobalNames.CODE_SRC_PATH + "/WebRoot/" + path ;
File file1 = new File(webroot_server_path);
File file2 = new File(webroot_local_path);
File file = null;
if (file1.exists()||file2.exists()) {
if(file2.exists()){
file = file2;
}else{
file = file1;
}
}
FileInputStream fileInputStream = new FileInputStream(file);
Reader scriptReader = new InputStreamReader(fileInputStream, "utf-8");
String result = "";
try{
engine.eval(scriptReader);
if (engine instanceof Invocable)
{
// 调用JS方法
Invocable invocable = (Invocable)engine;
result = (String)invocable.invokeFunction("Crypt_Encode", new Object[]{str});
}
}catch (Exception e){
e.printStackTrace();
}finally{
scriptReader.close();
}
return result;
}
接下来,见证结果的时候到了:
num 接口:
1
查看邮件,确实只有一条未读邮件
list接口:
[welcome 2018/3/30 10:50(Fri)]
他是个连接 点击后可以跳转到该未读邮件详情里去。