我正在尝试使用特定的电子邮件帐户(不是默认帐户)从 Outlook 2010 发送电子邮件。
电子邮件基于一个静态模板,该模板从表 (senders_table) 中为收件人、主题和电子邮件正文中的一些变量字段提取数据。
代码没有循环遍历我表中的所有记录。电子邮件通过指定的帐户发出,并从表中提取适当的数据,但在第一条记录后停止。
Private Sub test_Click()
'You must add a reference to the Microsoft Outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim stremail As String
Dim strsubject As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Senders_Table")
With rs
If .EOF And .BOF Then
MsgBox "No emails will be sent becuase there are no records assigned from the list", vbInformation
Else
Do Until .EOF
stremail = ![email]
strsubject = ![address]
strbody = "Dear " & ![name] & "," & _
Chr(10) & Chr(10) & "Some kind of greeting" & ![address] & "!" & _
" email message body goes here"
.Edit
.Update
.MoveNext
Loop
End If
End With
On Error Resume Next
With OutMail
.To = stremail
.CC = ""
.BCC = ""
.Subject = strsubject
.Body = strbody
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
.Send
End With
On Error GoTo 0
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set OutMail = Nothing
Set OutApp = Nothing
End Sub