Solved

VBS Email with attachment

Posted on 2013-11-13
7
953 Views
Last Modified: 2013-11-18
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
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 39644802
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
 

Author Comment

by:100questions
ID: 39645803
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
 

Author Comment

by:100questions
ID: 39645890
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
Windows running painfully slow? Try these tips..

Stay away from Speed Up Computer Programs that do more harm than good.
Try these tips instead.
Step by step instructions in trouble shooting Windows Performance issues.

 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39646189
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
 

Author Comment

by:100questions
ID: 39652051
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
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39652088
Try this, change this line:

.Subject = Subject

to

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

~bp
0
 

Author Comment

by:100questions
ID: 39657502
Thanks, this worked.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question