vba 使用 windows 中的 Visual Basic 发送带有附件的雷鸟电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28664088/
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 06:19:13  来源:igfitidea点击:

Sending thunderbird email with attachment using visual basic from windows

vbaemailthunderbird

提问by Mike D

I'm trying to send email from a visual basic script using Visual basic WITHOUT any user action. The idea came for the first reference below.

我正在尝试使用 Visual Basic 从 Visual Basic 脚本发送电子邮件,无需任何用户操作。这个想法来自下面的第一个参考。

http://forums.mozillazine.org/viewtopic.php?f=39&t=540783

http://forums.mozillazine.org/viewtopic.php?f=39&t=540783

http://forums.mozillazine.org/viewtopic.php?t=203591

http://forums.mozillazine.org/viewtopic.php?t=203591

https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#-options

https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#-options

The script does work BUT I have been unable to figure out how to include attachments. I know little about VB but in the first reference the mixture of " and chr(34) (" I assume) seems very weird. Also the discrepancy between "mailto" and "to" between the first and third reference is troubling.

该脚本确实有效,但我一直无法弄清楚如何包含附件。我对 VB 知之甚少,但在第一个参考文献中," 和 chr(34) (" 我认为) 的混合似乎很奇怪。第一个和第三个引用之间的“mailto”和“to”之间的差异也令人不安。

Here's what I have tried.

这是我尝试过的。

dim s
Set s = CreateObject("WScript.Shell")
s.run """thunderbird.exe""" & " -compose mailto:[email protected]? &subject=""send mail 3""&body=""nice body text""&attachment='file:///c:/Documents and Settings/Mike/My Documents/sendmail/vb/msg.txt'" 
WScript.Sleep 1000
s.SendKeys "^{ENTER}"
WScript.Sleep 1000 

This composes and sends an email without user action BUT without the attachment. I have tried both single and double quotes around the file name.

这会在没有用户操作但没有附件的情况下撰写并发送电子邮件。我在文件名周围尝试了单引号和双引号。

If I change the file name to a non-existent file, an email is send without attachment BUT user action is required to actually send the message.

如果我将文件名更改为不存在的文件,则会发送不带附件的电子邮件,但需要用户操作才能实际发送消息。

回答by Edi

edited my answer since I've been learning a thing or two these last couple of weeks ;)

编辑我的答案,因为我最近几周一直在学习一两件事;)

you need an apostrophe around to: and cc: lists with more than one recipient you also need an apostrophe around any main body text or attachments you're going to send

您需要在 to: 和 cc: 列表中添加一个撇号,其中包含多个收件人 您还需要在您要发送的任何正文或附件周围添加一个撇号

if you put an apostrophe into your VBA editor, it just comments it out, so you can get away with that by using Chr(39)

如果您将撇号放入 VBA 编辑器中,它只会将其注释掉,因此您可以使用 Chr(39) 摆脱它

also, as the documentation suggests, you need attachment='file:///c:/test.txt' which includes the file:///

此外,正如文档所建议的,您需要attachment='file:///c:/test.txt',其中包括 file:///

I've included an example from something I've been working on below

我在下面包含了一个我一直在做的事情的例子

Dim reportTB As Object
Set reportTB = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1


reportTB.Run "thunderbird.exe  -compose to=bfx_" & LCase(show) & "[email protected],subject=[" & show & "] EOD Report " _
     & prjDate & ",body=" & Chr(39) & "Hi " & UCase(show) & "ers,<br><br>Today's end of day report is attached.<br>" & _
      "Any questions let me know.<br><br>Edi " & Chr(39) & ",attachment=" & Chr(39) & reportPath & Chr(39), windowStyle, waitOnReturn

Hope that helps :)

希望有帮助:)

回答by Serenity

Write proper programs. You can't send attachments with mailto. The standard only allows one parameter (although everyone accepts many).

编写适当的程序。您无法使用 mailto 发送附件。该标准只允许一个参数(尽管每个人都接受多个)。

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "[email protected]"

emailObj.To       = "[email protected]"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

emailObj.AddAttachment "C:/Users/User/Desktop/err.fff"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "dc"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password1"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then 
    Msgbox "Done"
Else
    Msgbox err.number & " " & err.description
    err.clear
End If