soapUI 读取邮件内容
2015-11-27 14:08
363 查看
tip: 不会用groovy, 所以先用java写好,spring 下载个groovy插件,可以直接convert to groovy
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.String;
import java.util.regex.Pattern;
import java.util.regex.*;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Part;
// 连接pop3服务器的主机名、协议、用户名、密码
String host = "owa.morningstar.com";
String protocol = "pop3";
String user = "msdomain1\\******";
String pwd = "*********";
String contentSubject = "Regarding area"; // 邮件标题包含关键字
int flag=1;
// 创建一个有具体连接信息的Properties对象
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
props.setProperty("mail.pop3.host", host);
// 使用Properties对象获得Session对象
Session session = Session.getInstance(props);
session.setDebug(true);
// 利用Session对象获得Store对象,并连接pop3服务器
Store store = session.getStore(protocol);
store.connect(host, user, pwd);
// 获得邮箱内的邮件夹Folder对象,以"只读"打开
Folder folder = store.getFolder("inbox"); // for POP3 just can open the inbox folder
folder.open(Folder.READ_ONLY);
// 获得邮件夹Folder内的所有邮件Message对象
Message [] messages = folder.getMessages();
int mailCounts = messages.length;
//search form the newest
for(int i = mailCounts-1; i > mailCounts-10; i--) {
StringBuffer bodytext = new StringBuffer(); //存放邮件内容的StringBuffer对象
String subject = messages[i].getSubject();
String contentType = messages[i].getContentType();
// check the contenttype , here is the text, if the type is other ,maybe not work
if (subject.contains(contentSubject)){
bodytext.append((String)messages[i].getContent());
// get the quick base number form the email content
String body = bodytext;
String[] QBsplit = body.split("<br>Quickbase");
String QBNumber =QBsplit[1].substring(1,13);
// assert the quick base number is as expected Quickbase Ticket:[0-9]{5}
Pattern p = Pattern.compile("Ticket:[0-9]{5}");
Matcher m = p.matcher(QBNumber);
boolean b =m.matches();
assert (b==true),"Failed, The Quicktake number is created Failed" //用 assert 可以监控到运行失败还是成功
if(b){
log.info "The Quicktake number is created successfully";
}
// if there are more than one same email ,just get the newest one
flag=flag+1;
if(flag==2){
break;
}
else{
log.info "The Quicktake number is created Failed" ;
}
}
}
folder.close(false);
store.close();
如果需要在下一个step用到, 可以save到project 的属性里
testRunner.testCase.testSuite.project.setPropertyValue("urlPWD",urlPWD );
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.String;
import java.util.regex.Pattern;
import java.util.regex.*;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Part;
// 连接pop3服务器的主机名、协议、用户名、密码
String host = "owa.morningstar.com";
String protocol = "pop3";
String user = "msdomain1\\******";
String pwd = "*********";
String contentSubject = "Regarding area"; // 邮件标题包含关键字
int flag=1;
// 创建一个有具体连接信息的Properties对象
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
props.setProperty("mail.pop3.host", host);
// 使用Properties对象获得Session对象
Session session = Session.getInstance(props);
session.setDebug(true);
// 利用Session对象获得Store对象,并连接pop3服务器
Store store = session.getStore(protocol);
store.connect(host, user, pwd);
// 获得邮箱内的邮件夹Folder对象,以"只读"打开
Folder folder = store.getFolder("inbox"); // for POP3 just can open the inbox folder
folder.open(Folder.READ_ONLY);
// 获得邮件夹Folder内的所有邮件Message对象
Message [] messages = folder.getMessages();
int mailCounts = messages.length;
//search form the newest
for(int i = mailCounts-1; i > mailCounts-10; i--) {
StringBuffer bodytext = new StringBuffer(); //存放邮件内容的StringBuffer对象
String subject = messages[i].getSubject();
String contentType = messages[i].getContentType();
// check the contenttype , here is the text, if the type is other ,maybe not work
if (subject.contains(contentSubject)){
bodytext.append((String)messages[i].getContent());
// get the quick base number form the email content
String body = bodytext;
String[] QBsplit = body.split("<br>Quickbase");
String QBNumber =QBsplit[1].substring(1,13);
// assert the quick base number is as expected Quickbase Ticket:[0-9]{5}
Pattern p = Pattern.compile("Ticket:[0-9]{5}");
Matcher m = p.matcher(QBNumber);
boolean b =m.matches();
assert (b==true),"Failed, The Quicktake number is created Failed" //用 assert 可以监控到运行失败还是成功
if(b){
log.info "The Quicktake number is created successfully";
}
// if there are more than one same email ,just get the newest one
flag=flag+1;
if(flag==2){
break;
}
else{
log.info "The Quicktake number is created Failed" ;
}
}
}
folder.close(false);
store.close();
如果需要在下一个step用到, 可以save到project 的属性里
testRunner.testCase.testSuite.project.setPropertyValue("urlPWD",urlPWD );
相关文章推荐
- ng-value中格式化日期
- 1041. Be Unique (20)
- AMD与CDM RequireJS与SeaJS CommonJS 的认识
- [转]Java集合类: Set、List、Map、Queue使用场景梳理
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- UIView生命周期详解
- What is an LSN: Log Sequence Number
- iOS 点击UIButton切换数组元素
- 232. Implement Queue using Stacks
- POJ 3080 Blue jeans
- POJ 3080 Blue jeans
- ios9控件新宠:UIStackView
- string 和 stringbuilder 的区别
- IO获取UITableView的cell内容
- easyui-progressbar进度条
- GUI界面资源
- 支持向量机SVM(五)SMO算法
- [Built-in Functions] - P
- UIMenuItem 文字选择菜单
- Machine Learning week 7 quiz: programming assignment-Support Vector Machines