soapUI 读取邮件内容

来源:互联网 发布:专业非编软件 编辑:程序博客网 时间:2024/05/02 07:26

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 );











0 0
原创粉丝点击