0

first thing i'm not really a programmer and barely mange to finish this

i wanted to send a recurring email using outlook so i google it and found this script that i write in notepad then save it in .vbs and the body of the message is using HTML and after that i make schedule task to be automatic. and it is really working fine with me but now i want to add an attachment to this email i searched hard but did not find anything that can work with this script and the big problem that most of the time i do not understand anything so i hope if someone can help me this is the script that i use

Dim olkApp
Dim olkSes
Dim olkMsg
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkMsg = olkApp.CreateItem(0)
With olkMsg
    'On the next line enter the email address of the person you want to send to'
    .Recipients.Add "to email"
    .Recipients.ResolveAll
    'On the next line enter the email subject'
    .Subject = "Reminder Email"
    'On the next line enter your message.  You can use HTML formatting.'
    .HTMLBody = "here i put my message using HTML"
        .Send
End With
Set olkMsg = Nothing
olkSes.Logoff
Set olkSes = Nothing
Set olkApp = Nothing

i want to add attachment to this email can someone help me

mmfes120
  • 1
  • 2

1 Answers1

0

Just add the following code below subject:

.AddAttachment "C:\****\*****\testfile.txt"

Try the following

Dim oAttch As MailAttachment = New MailAttachment("C:\myattachment.zip") 

And then add the following under subject:

.Attachments.Add(oAttch) 

Sorry the above did not work for you...

I do not use office...

Below is the code that works for me:

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "from@email.co.za"
objEmail.To = "to@email.co.za"
objEmail.Subject = "Attachment"
objEmail.Textbody = "Email with attachment"
objEmail.AddAttachment "C:\*******\*****\testfile.txt"
objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"mail.your_server.co.za"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")     = 1
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objEmail.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Update
objEmail.Send

I don't know if this will work for you

Danie
  • 13
  • 5
  • i tried your solution but it gives error, Object doesn't support this property or method: 'AddAttachment' , my attachment is jpg is this what makes the error or it can work with any file type – mmfes120 Feb 22 '16 at 14:29
  • i'm sorry to report still not working i tried to send txt and jpg and put the file directly in c: so do you have any other solution i'll be thankful – mmfes120 Feb 23 '16 at 11:50
  • does this script can work with outlook, and if you can tell me what is every line for, and can i use HTML with the message body – mmfes120 Feb 24 '16 at 11:25
  • Yes, this works without outlook, and you can use html within the body. You must change the From, To email addresses. You need to change the smtpserver to the outgoing mail server and insert the correct username and password. If you do not need authentication on your mail server, you need to make the smtpauthenticate = 2 – Danie Feb 24 '16 at 12:20
  • You can also have a look at http://stackoverflow.com/questions/19555640/email-a-text-files-content-in-html-format – Danie Feb 24 '16 at 12:27
  • I forgot to mention if you do not need the authentication, you will not add the sendusername and sendpassword lines. – Danie Feb 24 '16 at 12:40