Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to send e-mail 2

Status
Not open for further replies.

KimVong

IS-IT--Management
Sep 13, 2002
90
US
does anyone know how to send out mail using vbscript. I run exchange 5.5 with a close relay. Any help is greatly appreciated.

kv
 
Look up CDO on the MS web site everthing you'll need is there.
 
Here's a piece of code I use to send an e-mail with a file attachment. It requires Outlook. Hope this helps!

mapman04

========= Code ===========
Dim sTo,strLoc,strFname,strSubject,strText,oSession,oMessage,oAttachment,oRecipient

'Sending to recipients

sTo="SMTP:user@somedomain.com"
strLoc = "C:\"
strFname = "A_File.txt"
strSubject = "Here is the file: " & strFname
strText = "The automated processing was successful!" & vbCr & vbCr & _
"Please review the attached file!" & vbCr & vbCr & _
"This messages was generated by a vbScript at: " & Now()
set oSession=CreateObject("MAPI.Session")
oSession.Logon "UserProfile"
set oMessage=oSession.Outbox.Messages.Add(strSubject,strText)
set oAttachment=oMessage.Attachments.Add(strFname,,,strLoc)
set oRecipient=oMessage.Recipients.Add(,sTo)
oRecipient.Resolve
oMessage.Send
oSession.Logoff
set oSession=nothing
 
OK, is there any script to use if you DON'T have Outlook installed??

/Daniel
 
save this as a .vbs file:

strHTML=&quot;<html><head></head><body>Testing...One...Two...Three</body></html>&quot;
Set objMail = CreateObject(&quot;CDO.Message&quot;)
With objMail
.To = &quot;them@abc.com&quot;
.CC = &quot;him@def.com&quot;
.From = &quot;you@ghi.com&quot;
.Subject = &quot;CDO Mail Test&quot;
'.CreateMHTMLBody &quot; **Un-comment this to send a web page**
.HTMLBody=strHTML
.Send
End With
Set objMail = Nothing
msgbox &quot;Mail Sent&quot;
 
Veep
I really like the coding you have above, and I tried and it work. I would like to ask you something,
I have a message body of about 10 lines. Do I have to put all of them on the top like you did for testing one .. two...three?
and how do I do that?
thanks

kv
 
I'm not really sure what you're asking. You could replace the html with a text body like this:
With objMail
.To = &quot;them@abc.com&quot;
.CC = &quot;him@def.com&quot;
.From = &quot;you@ghi.com&quot;
.Subject = &quot;CDO Mail Test&quot;
.TextBody=&quot;Hello There&quot; & vbcrlf & &quot;How is the weather today?&quot; _
& vbcrlf & &quot;Got to go now&quot;
.Send
End With

Or just assign all of your text to a variable.
 
Veep,
thanks for the reply,
what if I want to send an attachment?
how do I code that?
I didn't know that vbs is this powerful.
 
ObjMail.AddAttachment &quot;c:\YourFile.txt&quot;
 
I have a very similar requirement - I need VBScript to create a new email (with some formatted body text already in place) and present it on screen as a new Outlook message - ready for further editing.

Can anyone assist?

-Andrew
 
Here is a script I generate in a cobol program.
The script body is client variable and it attaches an excel
Spread sheet. The script runs outlook via MAPI.
Enjoy Bob

Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim Body1
Dim MessageAttachment
Dim ol, ns, newMail
Dim Body2
Dim Body3
Dim Body4
Dim Body5
Dim Body6
Dim Body7
Dim Body8
Dim Body9
Dim Body10
ToAddress = "admin@somewhere.com"
MessageSubject = "Magazine Sales Leads For you"
BODY1 = "TO: ME "
BODY2 = "COMAPNY NAME "
BODY3 = "11114 MAIN ST "
BODY4 = "GULFPORT, MS 39503 "
BODY5 = "From: MY Magazine Phone: 999-555-5601"
BODY6 = "24 MAIN STREET fax: 999-555-5640"
BODY7 = "Suite 99"
BODY8 = "Lambert, NJ 08530"
BODY10 = "This file can be opened with Microsoft Excel"
MessageBody = "Here are your leads for 06-16-2004"
MessageAttachment = "Y:\FOLDER\FOLDER\00110.XLS"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = BodY1 & vbCrLf & BODY2 & VBCRLF & BODY3 & VBCRLF & BODY4 & VBCRLF & vbcrlf & BODY5 & VBCRLF & BODY6 & vbcrlf & body7 & vbcrlf & body8 & vbcrlf & vbcrlf & Messagebody & vbcrlf & body10 & vbcrlf & vbcrlf
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send

SET OL = NOTHING
SET NS = NOTHING
SET NEWMAIL = NOTHING
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top