加了判断邮件是否收到的逻辑。 因为邮件发和收直接会有个时间差
//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 管发邮件 。