Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

VBS Email with attachment

I want to modify this VBS script so that it sends an attachment:

The attachment can be found at C:\Attachment\attachment.pdf


MailTo = "email@email.com"
From = "email@email.com"
Subject = "This is a test"
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password
Sub Email (MailTo, From, Subject, Body, Username, Password)
 Set objMessage = CreateObject("CDO.Message")
 With objMessage
  .Subject = Subject
  .From = From
  .To = MailTo
  .TextBody = Body
 End With
 With objMessage.Configuration.Fields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
 End With
 objMessage.Send
End Sub
0
100questions
Asked:
100questions
  • 4
  • 2
1 Solution
 
oBdACommented:
Use the AddAttachment method (see line 27):
MailTo = "email@email.com"
From = "email@email.com"
Subject = "This is a test"
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password
Sub Email (MailTo, From, Subject, Body, Username, Password)
 Set objMessage = CreateObject("CDO.Message")
 With objMessage
  .Subject = Subject
  .From = From
  .To = MailTo
  .TextBody = Body
 End With
 With objMessage.Configuration.Fields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      .Update
 End With
 objMessage.AddAttachment "C:\Attachment\attachment.pdf"
 objMessage.Send
End Sub

Open in new window

0
 
100questionsAuthor Commented:
Thanks.  How do I modify this script to send to multiple email addresses, say email1@email.com, email2@email.com, email3@email.com?
Also, if I want to send any file which is found at C:\Attachment\, whether it's a pdf or txt etc.. how can I modify the script to send any file which is found in the Attachment folder?
0
 
100questionsAuthor Commented:
Lastly, I would like to see a revised script which also asks the user running the script to add a date to the subject line.  
For instance if I specify the subject to be 'Latest Report', when I run the script I want a prompt to appear so that the user type in a date, say 'November 13' and then the script will concatenate the Subject as following:  Latest Report November 13.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
oBdACommented:
SubjectDate = InputBox("Date to be added to the subject:", "Enter date")
MailTo = "email@email.com,email2@email.com,email3@email.com"
From = "email@email.com"
Subject = "This is a test " & SubjectDate
Body = "VBSCript email"
Username = "user"
Password = "password"
Email MailTo, From, Subject, Body, Username, Password

Sub Email (MailTo, From, Subject, Body, Username, Password)
	Set objMessage = CreateObject("CDO.Message")
	With objMessage
		.Subject = Subject
		.From = From
		.To = MailTo
		.TextBody = Body
	End With
	With objMessage.Configuration.Fields
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
		.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user"
		.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
		.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
		.Update
	End With
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder("C:\Attachment")
	Set colFiles = objFolder.Files
	For Each objFile in colFiles
		objMessage.AddAttachment objFile.Path
	Next
	objMessage.Send
End Sub

Open in new window

0
 
100questionsAuthor Commented:
I would like to modify the script so that in the Subject Line it shows yesterday's date:
It should concatenate with yesterday's date .. ie   "This is the subject line: should automatically enter yesterday's date here"
0
 
Bill PrewCommented:
Try this, change this line:

.Subject = Subject

to

.Subject = Subject & " " & FormatDateTime(DateAdd("d", -1, Date), vbShortDate)

~bp
0
 
100questionsAuthor Commented:
Thanks, this worked.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now