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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39521872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:17:36  来源:igfitidea点击:

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

excelvbaexcel-vbathunderbird

提问by user2951249

What I want to do is send an email from a Thunderbird account automatically. The user shouldn't even have to hit the Send button of the email.

我想要做的是自动从 Thunderbird 帐户发送电子邮件。用户甚至不必点击电子邮件的发送按钮。

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. 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).

我试过使用 CDO,但它的问题是你必须输入你发送的帐户的用户名和密码。这个宏将被多个不同的帐户使用,因此输入每个用户名和密码是不可行的。如果可以通过某种方式从 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 protected]"
cc = "[email protected]"
bcc = "[email protected]"
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 bodyare all recognized correctly. The problem is they all get added to the end of the first field. For instance, with the way the code is right now, ccwill get put in the cc field, but bcc, subj, and bodyall get appended to ccin the cc field of Thunderbird.

截至目前,该领域ccbccsubj,和body都是正确识别。问题是它们都被添加到第一个字段的末尾。例如,按照现在的代码方式,cc将放在 cc 字段中,但是bccsubj, 和body所有附加到ccThunderbird 的 cc 字段中。

If I comment ccout, then bccis put in the correct field, but subjand bodyget appended to bccin the bcc field of Thunderbird.

如果我评论cc了,然后bcc被放在正确的领域,但subjbody获得追加到bcc雷鸟的BCC领域。

If I comment ccand bccout, then subjgets put in the correct field, but bodygets appended to subjin the subject field of Thunderbird.

如果我评论ccbcc退出,则会subj被放入正确的字段,但body会附加到subjThunderbird 的主题字段中。

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}", Trueisn't working at all. 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. Email from Thunderbird displays, but this code isn't sending the email like it's supposed to.

最后,SendKeys "^+{ENTER}", True根本不工作。这可能是因为所有参数都没有放在 Thunderbird 的正确字段中,但不确定,因为我无法让它工作。显示来自 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 protected]"
cc = "[email protected]"
bcc = "[email protected]"
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

回答by zedfoxus

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 = "[email protected]"
    cc = "[email protected]"
    bcc = "[email protected]"
    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='[email protected],[email protected]',cc='[email protected]',subject='dinner',body='How about dinner tonight?',attachment='C:\temp\info.doc,C:\temp\food.doc'"

雷鸟 -compose "to='[email protected],[email protected]',cc='[email protected]',subject='dinner',body='今晚晚餐怎么样?',attachment='C :\temp\info.doc,C:\temp\food.doc'"

The example indicates that after -composewe should use type our information in double-quotes. Each parameter is separated by comma. Nomenclature is parameter='value[,value]' [,parameter='value[,value]]....

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