发送带有附件..VBA代码的电子邮件 [英] Sending Email with Attachment ..VBA code

查看:67
本文介绍了发送带有附件..VBA代码的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送带有附件的电子邮件:

I am trying to send email with attachment:

我的代码:

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

它从第一列中读取电子邮件地址,在第二列中,我共享了附件的地址.当向最后一个用户发送电子邮件时,它将附加第一行中的所有附件.例如:

It reads the email address from the first column and in the second column I have shared the address for attachments. When it emails the last user it attaches all the attachment from the top row. e.g:

spra@xyz.com    C:\Users\sprasad\Desktop\Test.docx          
sha@gwu     C:\Users\sprasad\Desktop\Test2.docx

因此,对于sha @ gwu,它会同时发送文档Test和Test2.

我只想为sha @ gwu附加test2文档.我的代码怎么了?

推荐答案

添加此行...

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 = ""
  .Attachments.DeleteAll       ' <--------
  .AddAttachment Text2
  Text2 = Null
  .Send
  Next i

End With

End Sub

这篇关于发送带有附件..VBA代码的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆