Computer Hope

Microsoft => Microsoft DOS => Topic started by: BM on February 18, 2010, 03:56:45 AM

Title: attach file to email
Post by: BM on February 18, 2010, 03:56:45 AM
Hi,

how can I attach file to email through cmd?

I want to create a batch file that when I click on it, it will open new email (from active outlook email), attach file in specified path to that email and fill "to" and "subject" fields. So if I want to email that file I should only run batch file then click send. Is this doable?
Title: Re: attach file to email
Post by: gpl on February 18, 2010, 06:06:59 AM
this cannot be done natively with the commandline, a little vbscript would be able to assist you.

Otherwise, if you really must use the commandline check this thread http://www.computerhope.com/forum/index.php?topic=64216.0
Title: Re: attach file to email
Post by: Sidewinder on February 18, 2010, 06:53:58 AM
This little snippet may help:

Code: [Select]
Set ol = CreateObject("Outlook.Application")
Set objNamespace =ol.GetNamespace("MAPI")
Set objMailItem = ol.CreateItem(olMailItem)

strRecipients  = "email of recipient(s)"
objMailItem.Recipients.Add strRecipients
objMailItem.Subject = "Attachment Enclosed"
objMailItem.Body = "Hi"
objMailItem.Attachments.Add "file to attach"
objMailItem.Send
ol.Quit

Fill in strRecipients, subject, body, and attachments (use full path) with relevant data and you should be good to go.

Save the script with a VBS extension and run from either the run box or the command line as wscript scriptname.vbs

Good luck.  8)