SoapUI 收邮件代码 (修改版)

该博客介绍了一段Java代码,用于通过POP3协议连接邮件服务器并检查收件箱中特定主题的邮件。代码包含一个循环机制,用于处理可能的时间延迟,直到找到匹配的邮件内容。一旦找到,它将提取内容中的特定字符串并将其保存到项目属性中,以便后续步骤使用。
摘要由CSDN通过智能技术生成

加了判断邮件是否收到的逻辑。 因为邮件发和收直接会有个时间差

//package com.morningstar.Feedback.tests;


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.lang.Thread;
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 = "****\****";
String pwd = "*****";
String contentSubject = "New ***** Password";
 def urlPWDOlder = testRunner.testCase.testSuite.project.getPropertyValue("urlPWD")  
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); 
          
//search form the newest one
while(flag){
// 获得邮箱内的邮件夹Folder对象,以"只读"打开 
Folder folder = store.getFolder("inbox"); 
folder.open(Folder.READ_ONLY);         
// 获得邮件夹Folder内的所有邮件Message对象 
Message [] messages = folder.getMessages(); 
    int mailCounts = messages.length; 
    int i = mailCounts-1;
    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(flag==6){   // just try five time
    break;
    }
    if (subject.contains(contentSubject)){
        bodytext.append((String)messages[i].getContent());
        String body = bodytext;
        // here is try to get the pwec from the email content
        String[] subBody = body.split("d\\.")
        String urlPWD =  subBody[1].substring(83,99);   
  log.info urlPWD; // check the pwec is got 
  if (urlPWDOlder == urlPWD){
  Thread.sleep(5000);
  flag=flag+1;
  continue;
  }else{
            testRunner.testCase.testSuite.project.setPropertyValue("urlPWD",urlPWD );// save the pwec to the property for the next step to use
             break;
  }
    }
    folder.close(false); 
   }  
   


store.close();   


关于邮件协议,  POP3 没有read 、unread 功能,管收邮件 。 SMTP 管发邮件 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值