可能重复:
Send e-mail through VBA
Send email from Excel in Exchange environment
到目前为止我有这个
Dim objOutl
Set objOutl = CreateObject("Outlook.Application")
Set objMailItem = objOutl.CreateItem(olMailItem)
objMailItem.Display
strEmailAddr = "me.me@you.com"
objMailItem.Recipients.Add strEmailAddr
objMailItem.Body = "Hi"
objMailItem.Attachments.Add "access.xml"
Set objMailItem = nothing
Set objOutl = nothing
有效!但仅限于拥有Outlook的计算机。如何使用具有Windows Live的计算机?
答案 0 :(得分:1)
Windows Live Mail(WLM)不支持通过VBA进行自动化,因此它不像Outlook那样简单。
对于其他选项,请尝试在搜索字段中输入[vba] e-mail
。你会得到很多点击;以下是相关示例:Hit,hit,hit。其中一些为您提供了使用CDO发送邮件的工作代码。如果我是你,我就会这样做。
如果必须使用WLM,请查看支持WLM的this mail add-ins for Excel。
否则你就会陷入使用VBA的SendMail
方法,这种方法非常有限:
示例代码:
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SendMail "me.me@you.com", _
"Insert subject here"
有关更多示例,请点击此处:http://www.rondebruin.nl/sendmail.htm
答案 1 :(得分:0)
以下假设处理访问(vba)(代码不是我的):
Public Function send_email()
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mygmail@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Update
End With
' build email parts With cdomsg
.To = "somebody@somedomain.com"
.From = "mygmail@gmail.com"
.Subject = "the email subject"
.TextBody = "the full message body goes here. you may want to create a variable to hold the text"
.Send
End With
Set cdomsg = Nothing
End Function
请注意,如果您想使用其他电子邮件服务,则应稍微更改一下代码。
其他一些选项 - msdn reference
希望它有所帮助。