我正在用Golang编写一个程序,该程序将使用Mozilla的Thunderbird电子邮件客户端发送电子邮件。应该执行的Windows命令是:
start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" -compose "to='CloudCoin@Protonmail.com',subject='Subject1',body='Hello'" -offline
我的Go代码如下所示(命令是上面列出的代码):
var command string
command = `start "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
command += ` -compose "to='` + toAddress + `',`
command += `subject='Subject1',`
command += `body='Hello'"`
command += ` -offline`
cmd := exec.Command("cmd.exe", "/C", command)
但是我得到一个错误:
Windows cannot find '\\'. Make sure you typed the name correctly, and then try again.
如果我将代码更改为此(移动单词start):
var command string
command = ` "" "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"`
command += ` -compose "to='` + toAddress + `',`
command += `subject='Subject1',`
command += `body='Hello'"`
command += ` -offline`
fmt.Println("Command: " + command)
cmd := exec.Command("cmd.exe", "/C", "start", command)
然后出现另一个错误:
Windows cannot find 'Files'. Make sure you typed the name correctly, and then try again.
似乎不是尝试启动“”,而是尝试启动\\。如何保留双引号?
答案 0 :(得分:1)
您的问题可能是传递给exec.Command
的每个单独的字符串作为cmd.exe
的单个参数传递(不解析),这也可能不会拆分给定的字符串,因此您必须自己做。
请参见this example,该参数也被拆分。您应该可以将“”排除在外,因为无论如何您还是要手动拆分,或者为其编写程序或使用进行拆分的解释器运行它。
func do() {
args := []string{
"/C",
"start",
"",
`C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe`,
"-compose",
"to=" + toAddress + ",subject=Subject1,body=Hello",
"-offline",
}
cmd := exec.Command("cmd.exe", args...)
}