如何使用Excel VBA自动从Thunderbird发送电子邮件?

时间:2016-09-15 23:40:40

标签: excel vba excel-vba thunderbird

我想要做的是自动从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

截至目前,字段ccbccsubjbody都已正确识别。问题是它们都被添加到第一个字段的末尾。例如,通过代码现在的方式,cc将被放入cc字段,但bccsubjbody都会附加到{{ 1}}在Thunderbird的cc字段中。

如果我对cc发表评论,那么cc会被放入正确的字段,但{c}字段会bccsubj附加到body雷鸟

如果我对bcccc发表评论,那么bcc会被放入正确的字段中,但subj会被附加到主题字段中的body雷鸟

所以基本上我需要在每一行的末尾添加正确的代码。我已经尝试过subj"?"都无济于事。

最后,Chr$(34)根本不工作。这可能是因为所有参数都没有放在Thunderbird的正确字段中,但不确定,因为我无法正常工作。来自Thunderbird的电子邮件显示,但此代码并未发送它应该发送的电子邮件。

解决方案(由@zedfoxus提供)

SendKeys "^+{ENTER}", True

1 个答案:

答案 0 :(得分:4)

你非常接近。试试这个:

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 =&# 39;晚餐',身体='今晚晚餐怎么样?',附件=' C:\ temp \ info.doc,C:\ temp \ food.doc'& #34;

该示例表明在-compose后我们应该使用双引号键入我们的信息。每个参数用逗号分隔。命名法是parameter='value[,value]' [,parameter='value[,value]]...