2013-11-25 47 views
0

我想通过Groovy发送邮件。我试图在mac中使用mail命令,它的工作正常。将Gstring传递给shell命令

这是我使用的命令:

echo $message | mail -s "subject" [email protected] 

message是GString的:

def message = """ Hi 

Test mail """ 

我现在面临的问题是;当邮件发送时,message将以单行而不是其存储的方式出现。

我在哪里犯错?

+0

你是如何运行该shell脚本行? –

+0

类似于你的答案http://stackoverflow.com/questions/8049854/execute-a-command-in-linux-using-java-and-fetch-the-output – sriram

回答

1

你可以这样做:

[ '/bin/bash', '-c', "echo '$msg' | mail -s 'subject' [email protected]" ].execute().waitFor() 

即:在报价'

显然包裹msg,在msg任何引号需要进行转义,或者这将失败。

一个更具弹性的选项将使用java邮件

相关问题