简体   繁体   English

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

[英]How can I automatically send email from Thunderbird with Excel VBA?

What I want to do is send an email from a Thunderbird account automatically.我想要做的是自动从 Thunderbird 帐户发送电子邮件。 The user shouldn't even have to hit the Send button of the email.用户甚至不必点击电子邮件的发送按钮。

I've tried using CDO, but the problem with it is that you have to input the username and password of the account you are sending from.我试过使用 CDO,但它的问题是您必须输入您发送的帐户的用户名和密码。 This macro will be used from several different accounts, so inputting each username and password isn't feasible.这个宏将被多个不同的帐户使用,因此输入每个用户名和密码是不可行的。 I could use CDO if there was someway of retrieving the username, password, and smtp server from Thunderbird, but I feel like the code I already have should be able to accomplish this without CDO (hopefully).如果有某种方式可以从 Thunderbird 检索用户名、密码和 smtp 服务器,我可以使用 CDO,但我觉得我已经拥有的代码应该能够在没有 CDO 的情况下完成此操作(希望如此)。

Here is really the only code I see (and it's everywhere) in regards to accomplishing this.这确实是我看到的唯一代码(而且到处都是)关于实现这一点。

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

As of right now, the fields cc , bcc , subj , and body are all recognized correctly.截至目前,字段ccbccsubjbody都被正确识别。 The problem is they all get added to the end of the first field.问题是它们都被添加到第一个字段的末尾。 For instance, with the way the code is right now, cc will get put in the cc field, but bcc , subj , and body all get appended to cc in the cc field of Thunderbird.例如,同程的代码是现在, cc将获得把抄送域中,但bccsubjbody都得到追加到cc在雷鸟的CC域。

If I comment cc out, then bcc is put in the correct field, but subj and body get appended to bcc in the bcc field of Thunderbird.如果我评论cc出来,然后bcc被放在正确的领域,但subjbody得到追加到bcc在雷鸟的BCC领域。

If I comment cc and bcc out, then subj gets put in the correct field, but body gets appended to subj in the subject field of Thunderbird.如果我将ccbcc注释掉,那么subj会被放入正确的字段,但body会附加到 Thunderbird 主题字段中的subj

So basically I need to add the correct code at the end of each of these lines.所以基本上我需要在每一行的末尾添加正确的代码。 I've tried both "?"我都试过"?" and Chr$(34) to no avail.Chr$(34)无济于事。

Lastly, SendKeys "^+{ENTER}", True isn't working at all.最后, SendKeys "^+{ENTER}", True根本不起作用。 This might be because of all the parameters not being put in the correct field of Thunderbird, but not sure since I can't get that working.这可能是因为所有参数都没有放在 Thunderbird 的正确字段中,但不确定,因为我无法让它工作。 Email from Thunderbird displays, but this code isn't sending the email like it's supposed to.显示来自 Thunderbird 的电子邮件,但此代码并未像预期那样发送电子邮件。

SOLUTION (as provided by @zedfoxus)解决方案(由@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

You were pretty close.你非常接近。 Try this:尝试这个:

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

Notice the example from http://kb.mozillazine.org/Command_line_arguments_(Thunderbird) .请注意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 "to='john@example.com,kathy@example.com',cc='britney@example.com',subject='dinner',body='今晚晚餐怎么样?',attachment='C :\\temp\\info.doc,C:\\temp\\food.doc'"

The example indicates that after -compose we should use type our information in double-quotes.该示例表明,在-compose之后,我们应该使用双引号键入我们的信息。 Each parameter is separated by comma.每个参数用逗号分隔。 Nomenclature is parameter='value[,value]' [,parameter='value[,value]]... .命名法是parameter='value[,value]' [,parameter='value[,value]]...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM