Solved

VBS Email with attachment

Posted on 2013-11-13
7
958 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 85

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 85

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 56

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…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

628 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