2013-04-12 65 views
0

我想使用Groovy腳本在SOAPUI中發送包含測試步驟結果的電子郵件。我首先在我的Groovy控制檯上編寫了代碼,它運行良好。 當我複製並將其粘貼到SOAPUI(Groovy腳本測試步驟)時,它顯示錯誤。 這裏是我的代碼:Groovy腳本在Groovy控制檯中工作,但在SoapUi中拋出錯誤Groovy腳本

我想發送一封電子郵件,其中包含使用Groovy腳本的SOAPUI中的測試步驟結果。我首先在我的Groovy控制檯上編寫了代碼,它運行良好。 當我複製並將其粘貼到SOAPUI(Groovy腳本測試步驟)時,它顯示錯誤。 這裏是我的代碼

import javax.mail.* 
import javax.mail.internet.* 

public class SMTPAuthenticator extends Authenticator 
{ 
    public PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication('[email protected]', '****'); 
    } 
} 

def d_email = "[email protected]", 
     d_uname = "test", 
     d_password = "*****", 
     d_host = "smtp.gmail.com", 
     d_port = "465", //465,587 
     m_to = "[email protected]", 
     m_subject = "Testing", 
     m_text = "Hi, this is the testing email." 

def props = new Properties() 
props.put("mail.smtp.user", d_email) 
props.put("mail.smtp.host", d_host) 
props.put("mail.smtp.port", d_port) 
props.put("mail.smtp.starttls.enable","true") 
props.put("mail.smtp.debug", "true"); 
props.put("mail.smtp.auth", "true") 
props.put("mail.smtp.socketFactory.port", d_port) 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory") 
props.put("mail.smtp.socketFactory.fallback", "false") 

def auth = new SMTPAuthenticator() 
def session = Session.getInstance(props, auth) 
session.setDebug(true); 

def msg = new MimeMessage(session) 
msg.setText(m_text) 
msg.setSubject(m_subject) 
msg.setFrom(new InternetAddress(d_email)) 
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)) 

Transport transport = session.getTransport("smtps"); 
transport.connect(d_host, 465, d_uname, d_password); 
transport.sendMessage(msg, msg.getAllRecipients()); 
transport.close(); 

問題是這一行: transport.sendMessage(味精,msg.getAllRecipients());

當我刪除這一行,沒有errpr但沒有發送的短信..

你知道爲什麼這個工程完全常規控制檯上,但會拋出錯誤在SOAPUI

謝謝您的幫助提前!

+1

有什麼錯誤? –

回答

0

我測試你的代碼和它的作品,除了兩件事情:

1. Run the script with 'groovy.bat -classpath mail-1.4.1.jar sendemail.groovy' 
2. Fix the 2 misspellings of gmail in the script.