【发布时间】:2016-05-13 15:27:21
【问题描述】:
我正在尝试发送带有附件的电子邮件:
我的代码:
Sub SendEmailUsingGmail()
Dim Text As String
Dim Text2 As String
Dim i As Integer
Dim j As Integer
Dim NewMail As CDO.Message
Set NewMail = New CDO.Message
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Gmail Account
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Set your credentials of your Gmail Account
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "shank@gmail.com"
NewMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "********"
'Update the configuration fields
NewMail.Configuration.Fields.Update
'Set All Email Properties
With NewMail
.Subject = "Test Mail"
.From = "shank@gmail.com"
For i = 1 To 2
Text = Cells(i, 1).Value
Text2 = Cells(i, 2).Value
.To = Text
.BCC = ""
.TextBody = ""
.AddAttachment Text2
Text2 = Null
.Send
Next i
End With
End Sub
它从第一列读取电子邮件地址,在第二列中我共享了附件地址。 当它向最后一个用户发送电子邮件时,它会附上第一行的所有附件。 例如:
spra@xyz.com C:\Users\sprasad\Desktop\Test.docx
sha@gwu C:\Users\sprasad\Desktop\Test2.docx
因此,对于 sha@gwu,它会同时发送 doc Test 和 Test2。
我只想附上 sha@gwu 的 test2 文档。 我的代码有什么问题??
【问题讨论】:
-
你的
End With和Next i是错误的方式 -
@Macro...你能解释一下吗...我是 VBA 新手,这是我的第三个代码....
-
我不确定有什么要解释的 - 交换
End With和Next i... 因为代码甚至不应该编译,所以不确定你是如何运行它的。 -
您需要展示更多代码以便我们解释
-
For i = 1 To 2 With NewMail .Subject = "Test Mail" .From = "shank@gmail.com" Text = Cells(i, 1).Value Text2 = Cells(i, 2 ).Value .To = Text .BCC = "" .TextBody = "" .AddAttachment Text2 .Send Text2 = "" End With Next i Set NewMail = Nothing End Sub