Send file in body of email not as attachment vbscript - vbscript

im using vbscript CDO for mail sending but i want to attachment as a body of the email can you please suggest me.
strSMTPFrom = "kampati.vinay#testing.in"
strSMTPTo = "kampati.vinay#testing.in"
strSMTPRelay = "testing.in"
strTextBody = "MDaemon Q status"
strSubject = "MDaemon Q status"
strAttachment = "C:\MDaemon\output.txt"
Set oMessage = CreateObject("CDO.Message")
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update
oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.TextBody = strTextBody
oMessage.AddAttachment strAttachment
oMessage.Send

you can use create email body as HTML and
like
.............
body
.......
<div><img src="linkOfImage" height="50px" width="50px"/></div>
......
...............
for this you have to set one bit in your email header.
Example:
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("output.txt", True)
' The mailman object is used for sending and receiving email.
set mailman = CreateObject("Chilkat_9_5_0.MailMan")
' Any string argument automatically begins the 30-day trial.
success = mailman.UnlockComponent("30-day trial")
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
WScript.Quit
End If
' Set the SMTP server.
mailman.SmtpHost = "smtp.comcast.net"
' Create a new email object
set email = CreateObject("Chilkat_9_5_0.Email")
' Add an embedded image to the HTML email.
fileOnDisk = "images/dude2.gif"
filePathInHtml = "dudeAbc.gif"
' Embed the GIF image in the email.
success = email.AddRelatedFile2(fileOnDisk,filePathInHtml)
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
WScript.Quit
End If
' The src attribute for the image tag is set to the filePathInHtml:
htmlBody = "<html><body>Embedded Image:<br><img src=""dudeAbc.gif""></body></html>"
' Set the basic email stuff: HTML body, subject, "from", "to";
email.SetHtmlBody htmlBody
email.Subject = "VBScript HTML email with an embedded image."
success = email.AddTo("Admin","admin#chilkatsoft.com")
email.From = "Chilkat Support <support#chilkatsoft.com>"
success = mailman.SendEmail(email)
If (success <> 1) Then
outFile.WriteLine(mailman.LastErrorText)
Else
outFile.WriteLine("Mail Sent!")
End If
outFile.Close

Related

Outlook VBscript method .Send stops scripts

I am developing a script to send an email according to certain inputs, I am able to craft the email but not send it using the .Send method.
I am getting the following error: (please note that the line is matching the .Send use in the original case)
I have already successfully sent emails using the .SendKeys(^~) method, but I would like to be use Outlook object to do so and not simply send shortcuts.
This is my current code:
' Declare all variables that will be used later on
Dim outobj, mailobj, emailto, cc, subject, body, attachement
Dim strFileText
Dim objFileToRead
Dim splitEmailto
' Set the outlook application object
Set outobj = CreateObject("Outlook.Application")
' set the namespace
Set myNamespace = outobj.GetNameSpace("MAPI")
msgbox myNamespace.Folders(2)
' Set the mail item object
Set mailobj = outobj.CreateItem(olMailItem)
' Set a shell
Set WshShell = WScript.CreateObject("WScript.shell")
' Get all the argument and assign
emailto = "name#domain.eu"
cc = "name#domain.eu"
subject = "Simple Email"
body = "Some Text"
attachement = "C:\Users\name\Desktop\fileName.xls"
' Craft the email object
With mailobj
.Display
' assign the tos
.To = cstr(emailto)
' add CCs
.CC = cstr(cc)
' attach the relevant files
If attachement <> "" Then
If instr(attachement, ";") Then
splitAtt = split(attachement, ";")
For Each att In splitAtt
If att <> "" Then
.Attachments.add cstr(att)
End If
Next
Else
.Attachments.add cstr(attachement)
End If
End If
If Subject <> "" Then
.Subject = Subject ' sets the subject
End If
If body <> "" Then
.Body = body ' sets the body
End If
.Send
End With
' Clear the memory
Set outobj = Nothing
Set mailobj = Nothing
' check for no more events in the sending event
' Report out & Quits
WScript.StdOut.WriteLine("Email sent")
WScript.Quit
I would like to be able to send the email with the .Send. any idea?
The error is E_ABORT.
Why are you displaying the message and immediately calling Send? You either display the message (Display, but no Send), or just send it outwith displaying (Send, but no Display).

How to send a Confidential email using VBScript

I have been searching on-line on how to send an email with attachment as confidential. I was already able to create a script to be able to send an email with an attachment but I can't figure out how to send it as confidential.
I would appreciate if somebody can help me how to set email sensitivity in VBScript.
Here's my code:
Call Email
sub Email
Set objEmail = CreateObject("CDO.Message")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objEmail.From = "myemail"
objEmail.To = "SendToEmail"
ObjEmail.Subject = "Email Title"
ObjEmail.Textbody = "Email Body"
objEmail.AddAttachment "C:\Temp\ERSD\dchmar_" & sDate & ".txt"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="xx.xx.xx.xx"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/exchange/sensitivity") = 3
objEmail.Configuration.Fields.Update
objEmail.Send
End sub
Could you try this?
It's unknown if you have some custom headers. So check the headers in Outlook to see if those match with what I've posted below but I believe that should accomplish what you're asking.
Set objEmail = CreateObject("CDO.Message")
Set objEmailConf = CreateObject("CDO.Configuration")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objEmail.From = "myemail"
objEmail.To = "SendToEmail"
ObjEmail.Subject = "Email Title"
ObjEmail.Textbody = "Email Body"
objEmail.AddAttachment "C:\Temp\ERSD\dchmar_" & sDate & ".txt"
objEmailConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmailConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="xx.xx.xx.xx"
objEmailConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'objEmailConf.Fields.Item("http://schemas.microsoft.com/exchange/sensitivity") = 3
objEmailConf.Fields.Update
objEmail.Configuration.Fields.Item("urn:schemas:mailheader:Sensitivity") = "Company-Confidential"
objEmail.Configuration.Fields.Update
objEmail.Send

Giving an attachment a name when sending it with VBscript

set cdoConfig = CreateObject("CDO.Configuration")
with cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "localhost"
.Item(cdoSMTPAuthenticate) = 1
.Item(cdoSendUsername) = "contact#example.com"
.Item(cdoSendPassword) = "password"
.Update
end with
set cdoMessage = CreateObject("CDO.Message")
with cdomessage
set .Configuration = cdoConfig
.From = "contact#example.com"
.To = email
.Subject = subject
.HTMLBody = message
.AddAttachment "c:/i/report.pdf"
.Send
end with
set cdomessage = nothing
set cdoconfig = nothing
Everything sends find, but the recipient gets the message as "Untitled Attachment 000X.pdf"
How do I give the attachment a name?
I have something like this. But never tried.
'With cdomessage
.AddAttachment "c:/i/report.pdf"
.Attachments(1).Fields.Item("urn:schemas:mailheader:content-disposition") ="attachment;filename=" & NEWNAME
.Attachments(1).Fields.Update
'End With

VBScript SMTP Server

I have set this up to auto email through the Outlook client, is it possible to change this code to work directly through an SMTP server? And could anyone possibly help me do it?
Any help would be much appreciated, thanks!
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files
If LCase(fso.GetExtensionName(f)) = "xls" Then
Set wb = app.Workbooks.Open(f.Path)
set sh = wb.Sheets("Auto Email Script")
row = 2
name = "Customer"
email = sh.Range("A" & row)
subject = "Billing"
the = "the"
LastRow = sh.UsedRange.Rows.Count
For r = row to LastRow
If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then
SendMessage email, name, subject, TRUE, _
NULL, "Y:\Billing_Common\autoemail\Script\energia-logo.gif", 143,393
row = row + 1
email = sh.Range("A" & row)
End if
Next
wb.Close
End If
Next
Sub SendMessage(EmailAddress, DisplayName, Subject, DisplayMsg, AttachmentPath, ImagePath, ImageHeight, ImageWidth)
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
template = FindTemplate()
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(EmailAddress)
objOutlookRecip.resolve
objOutlookRecip.Type = 1
' Set the Subject, Body, and Importance of the message.
.Subject = Subject
.bodyformat = 3
.Importance = 2 'High importance
body = Replace(template, "{First}", name)
body = Replace(body, "{the}", the)
if not isNull(ImagePath) then
if not ImagePath = "" then
.Attachments.add ImagePath
image = split(ImagePath,"\")(ubound(split(ImagePath,"\")))
body = Replace(body, "{image}", "<img src='cid:" & image & _
"'" & " height=" & ImageHeight &" width=" & ImageWidth & ">")
end if
else
body = Replace(body, "{image}", "")
end if
if not isNull(AttachMentPath) then
.Attachments.add AttachmentPath
end if
.HTMLBody = body
.Save
.Send
End With
Set objOutlook = Nothing
End Sub
Function FindTemplate()
Set OL = GetObject("", "Outlook.Application")
set Drafts = OL.GetNamespace("MAPI").GetDefaultFolder(16)
Set oItems = Drafts.Items
For Each Draft In oItems
If Draft.subject = "Template" Then
FindTemplate = Draft.HTMLBody
Exit Function
End If
Next
End Function
If you want to send mail directly to an SMTP server, there's no need to go through Outlook in the first place. Just use CDO. Something like this:
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set msg = CreateObject("CDO.Message")
msg.Subject = "Test"
msg.From = "sender#example.com"
msg.To = "recipient#example.org"
msg.TextBody = "This is some sample message text."
With msg.Configuration.Fields
.Item(schema & "sendusing") = 2
.Item(schema & "smtpserver") = "smtp.intern.example.com"
.Item(schema & "smtpserverport") = 25
.Update
End With
msg.Send

Opening outlook 2010 through vbscript

I want to send email using outlook 2010, windows 7 & IE8 , what is code required to get the "Outlook.Application" object?.
I tried with
CreateObject("Outlook.Application") but getting error "Object Required"
Sample Code :-
' Create email object
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
email.Recipients.Add("abcaashn#gmail.com")
' Create the body of the email
MailBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
MailBody = MailBody & "<HTML>" & vbcrlf
MailBody = MailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
MailBody = MailBody & "<BODY>" & vbcrlf
MailBody = MailBody & "<B>For Your Information</B>,<BR><BR>"
MailBody = MailBody & "This is Sample Email.<BR><BR>"
MailBody = MailBody & "</BODY></HTML>"
' Send the Email
email.Subject = "No Invoices Issued"
email.HTMLBody = MailBody
email.Send
Try This simple code.
This will help you till opening the Outlook and navigate you to Inbox
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile", , False, True
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
objFolder.Display
End Sub
You can send an email using CDO which is the subsystem that Outlook uses. You can find more information in my article Sending Emails Using CDO in WSH on ASP Free.
Set objMessage = CreateObject("CDO.Message")
' Set Email Headers
objMessage.From = "sender#mymail.com"
objMessage.To = "abcaashn#gmail.com"
objMessage.Subject = "No Invoices Issued"
' Construct Email Body
objMessage.HTMLbody = "<b>For Your Information</b>, <br><br>" _
& "This is a Sample Email.<br><br>"
objMessage.AutoGenerateTextBody = True
' Set Server Settings
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mymail.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
This will work for me:-
Public Sub runOutlook
Set oolApp = CreateObject("Outlook.Application")
Set objNS = oolApp.GetNamespace("MAPI")
Set email = oolApp.CreateItem(0)
email.Display
email.To = "yash.tiwari#programmers.io"
email.Subject = "Test"
email.HTMLbody = "<b>For Your Information</b>, <br><br>" _
& "This is a Sample Email.<br><br>"
email.GetInspector.WindowState = 2
End Sub

Resources