如何使用 Excel VBA 从 Thunderbird 自动发送电子邮件?
How can I automatically send email from Thunderbird with Excel VBA?
我想做的是自动从 Thunderbird 帐户发送电子邮件。用户甚至不必点击电子邮件的发送按钮。
我尝试过使用 CDO,但问题是您必须输入发送帐户的用户名和密码。该宏将用于多个不同的帐户,因此输入每个用户名和密码是不可行的。如果有某种方法可以从 Thunderbird 检索用户名、密码和 smtp 服务器,我可以使用 CDO,但我觉得我已经拥有的代码应该能够在没有 CDO 的情况下完成此操作(希望如此)。
这是我真正看到的唯一(到处都是)实现此目的的代码。
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email@test.com"
cc = "cc@test.com"
bcc = "bcc@test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"
thund = thund & " -compose " & Chr$(34) & "mailto:" & email & "?"
thund = thund & "cc=" & Chr$(34) & cc & "?"
thund = thund & "bcc=" & Chr$(34) & bcc & "?"
thund = thund & "subject=" & Chr$(34) & subj & Chr$(34)
thund = thund & "body=" & Chr$(34) & body
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
截至目前,字段 cc
、bcc
、subj
和 body
均已正确识别。问题是它们都被添加到第一个字段的末尾。例如,按照现在的代码方式,cc
将被放入 cc 字段,但 bcc
、subj
和 body
都会附加到 cc
在 Thunderbird 的 cc 字段中。
如果我将 cc
注释掉,那么 bcc
会被放入正确的字段中,但是 subj
和 body
会附加到 bcc
中Thunderbird 的 bcc 字段。
如果我注释掉 cc
和 bcc
,那么 subj
会被放入正确的字段,但 body
会附加到 subj
Thunderbird 的主题字段。
所以基本上我需要在每一行的末尾添加正确的代码。我试过 "?"
和 Chr$(34)
都没有用。
最后,SendKeys "^+{ENTER}", True
根本不起作用。这可能是因为所有参数都没有放在 Thunderbird 的正确字段中,但不确定,因为我无法让它工作。来自 Thunderbird 的电子邮件显示,但此代码并未按预期发送电子邮件。
解决方案(由@zedfoxus 提供)
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email@test.com"
cc = "cc@test.com"
bcc = "bcc@test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" & _
" -compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "^{ENTER}", True
End Sub
你非常接近。试试这个:
Public Sub SendEmail()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "test@test.com"
cc = "test@test.com"
bcc = "test@test.com"
subj = "Testing"
body = "Testing"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe " & _
"-compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
请注意 http://kb.mozillazine.org/Command_line_arguments_(Thunderbird) 中的示例。
thunderbird -compose "to='john@example.com,kathy@example.com',cc='britney@example.com',subject='dinner',body='How about dinner tonight?',attachment='C:\temp\info.doc,C:\temp\food.doc'"
该示例表明在 -compose
之后我们应该使用双引号将我们的信息键入。每个参数以逗号分隔。命名法是 parameter='value[,value]' [,parameter='value[,value]]...
.
我想做的是自动从 Thunderbird 帐户发送电子邮件。用户甚至不必点击电子邮件的发送按钮。
我尝试过使用 CDO,但问题是您必须输入发送帐户的用户名和密码。该宏将用于多个不同的帐户,因此输入每个用户名和密码是不可行的。如果有某种方法可以从 Thunderbird 检索用户名、密码和 smtp 服务器,我可以使用 CDO,但我觉得我已经拥有的代码应该能够在没有 CDO 的情况下完成此操作(希望如此)。
这是我真正看到的唯一(到处都是)实现此目的的代码。
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email@test.com"
cc = "cc@test.com"
bcc = "bcc@test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"
thund = thund & " -compose " & Chr$(34) & "mailto:" & email & "?"
thund = thund & "cc=" & Chr$(34) & cc & "?"
thund = thund & "bcc=" & Chr$(34) & bcc & "?"
thund = thund & "subject=" & Chr$(34) & subj & Chr$(34)
thund = thund & "body=" & Chr$(34) & body
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
截至目前,字段 cc
、bcc
、subj
和 body
均已正确识别。问题是它们都被添加到第一个字段的末尾。例如,按照现在的代码方式,cc
将被放入 cc 字段,但 bcc
、subj
和 body
都会附加到 cc
在 Thunderbird 的 cc 字段中。
如果我将 cc
注释掉,那么 bcc
会被放入正确的字段中,但是 subj
和 body
会附加到 bcc
中Thunderbird 的 bcc 字段。
如果我注释掉 cc
和 bcc
,那么 subj
会被放入正确的字段,但 body
会附加到 subj
Thunderbird 的主题字段。
所以基本上我需要在每一行的末尾添加正确的代码。我试过 "?"
和 Chr$(34)
都没有用。
最后,SendKeys "^+{ENTER}", True
根本不起作用。这可能是因为所有参数都没有放在 Thunderbird 的正确字段中,但不确定,因为我无法让它工作。来自 Thunderbird 的电子邮件显示,但此代码并未按预期发送电子邮件。
解决方案(由@zedfoxus 提供)
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email@test.com"
cc = "cc@test.com"
bcc = "bcc@test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" & _
" -compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "^{ENTER}", True
End Sub
你非常接近。试试这个:
Public Sub SendEmail()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "test@test.com"
cc = "test@test.com"
bcc = "test@test.com"
subj = "Testing"
body = "Testing"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe " & _
"-compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
请注意 http://kb.mozillazine.org/Command_line_arguments_(Thunderbird) 中的示例。
thunderbird -compose "to='john@example.com,kathy@example.com',cc='britney@example.com',subject='dinner',body='How about dinner tonight?',attachment='C:\temp\info.doc,C:\temp\food.doc'"
该示例表明在 -compose
之后我们应该使用双引号将我们的信息键入。每个参数以逗号分隔。命名法是 parameter='value[,value]' [,parameter='value[,value]]...
.