I've followed different tutorials about prepopulating an email using outlook, and I've been successful on filling the "To:", "CC:", "Subject:" and the body.
outlook.exe /c ipm.note /m test#gmail.com?v=1
&cc=otherEmail#gmail.com
&subject=hello
&body=hi%20there
The point is, I would want to change the "From:" field, since I have 3 different accounts on my Outlook. I was wondering if there are some command to do that.
I didnt find anything about how to do it, I tried this but didnt work:
outlook.exe /c ipm.note /m test#gmail.com?v=1
&from=secondaryAccount#gmail.com
&cc=otherEmail#gmail.com
&subject=hello
&body=hi%20there
This also didnt work:
outlook.exe /c ipm.note /profile=secondaryAccount#gmail.com
/m test#gmail.com?v=1
&cc=otherEmail#gmail.com
&subject=hello
&body=hi%20there
Im using Outlook installed on my local PC, Office365.
You need to automate Outlook to set the MailItem.SendUsingAccount property available in the Outlook object model. Not all properties are available in the command line unfortunately. Here is a VBA macro which shows hot to set up the property:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
Also you may find the Automating Outlook from Other Office Applications article helpful.
Related
I have the following VB Script:
Set MSOutlook = CreateObject("Outlook.Application")
Set MailMsg = MSOutlook.CreateItem(0)
With MailMsg
.To = "xyz#gmail.com"
.Subject = "my subject"
.HTMLBody = "my text"
.Display
SendKeys "{TAB}{DOWN}{DOWN}{ENTER}", True
.Send
End With
Set MSOutlook = Nothing
Set MailMsg = Nothing
This script runs fine when executed within a VB compiler, such as Microsoft Visual Basic for Applications.
The SendKeys is required because of this damn Titus classification system we have in place at work to classify everything, such as emails and documents.
The email sends fine and it is classified correctly.
Now I am trying to call this script from the following Powershell script:
& cscript 'C:\Documents\sendEmail.vbs' //nologo
But the following error is received either in the Powershell ISE or when calling the vb script directly from a PowerShell console:
cscript.exe : C:\Documents\sendEmail.vbs Microsoft VBScript runtime error: Type mismatch: 'SendKeys'
Anyone know much about this and why the SendKeys function apparently cannot be used this way?
To pre-empt comments, yes, I would really like to move this vb functionality into the PowerShell script but the TITUS plugin is causing all sorts of headaches around that right now.
Thanks
In VBA, the SendKeys command is a member of the global Interaction module and, thus, always in scope.
In VBScript, you need to use the SendKeys method of the WScript.Shell object instead:
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.SendKeys "{TAB}{DOWN}{DOWN}{ENTER}", True
I use SQL Server Agent Jobs/DTS packages, coded in ActiveX Script/VBScript.
It works fine.
But there are some issues I would need help at the moment:
First: Is there a possibility to send an html email out of the ActiveX Script code in the DTS Step?
My company doesn't want to buy a separate commercial DLL for sending smtp email .. like JMail for example.
I know there are many such DLLs I could buy which can be used to send an email using VB or other languages.
But we don't have the money for such external components.
Could I use SQL Server Database Mail?
... but it is necessary to send the mails in html ...
Is there a possibility to create a new *.txt, *.csv or *.xlsx file from the ActiveX Script/VBScript code in the DTS Step?
I would like to copy the html email body code (a string which I built in the ActiveX Script code) into these files and attach them to the email I send out of the script code. So the user/recipient gets the html content embedded in the email body and separate in files too.
Thanks in advance for your help.
Tommy
This code works. PLUS it displays any errors which tell you why it didn't work.
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dc#gmail.com"
emailObj.To = "dc#gmail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "C:/Users/User/Desktop/err.fff"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "dc"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Ss"
emailConfig.Fields.Update
On Error Resume Next
emailObj.Send
If err.number = 0 then
Msgbox "Done"
Else
Msgbox err.number & " " & err.description
err.clear
End If
Also your account at www.gmail.com needs to be set to allow SMTP access (if using gmail as many are who do this).
The configuration info comes from Outlook Express (last in WinXP, renamed to Windows Mail in Vista, and dropped from Win7 and later). This shows default configuration on your computer. You may need to install Windows Live Mail. You also have to turn on the SMTP service in Windows Features.
Set emailConfig = emailObj.Configuration
On Error Resume Next
For Each fld in emailConfig.Fields
Text = Text & vbcrlf & fld.name & " = " & fld
If err.number <> 0 then
Text = Text & vbcrlf & fld.name & " = Error - probably trying to read password - not allowed"
err.clear
End If
Next
Msgbox Replace(Text, "http://schemas.microsoft.com", "")
Also CDO for Windows 2000 is not always included in all editions/versions of windows. See http://support.microsoft.com/en-au/kb/171440.
.
I'm new to Applescript and working on an app that runs in the background, and I want it to send email updates every so often WITHOUT mail.app.
I've tried googling this numerous times, and while this is the closest I've found, I don't know how to use a Python script. This app needs to work no matter what Mac OSX it is installed in, including Computers without Python. Is that possible?
I have often wanted to do the same thing and struggled myself. I ultimately landed on a python option. Python is supported back to OS X 10.5 and comes preinstalled on many of the newer versions of OS X. Is there some reason you don't feel you can use it? Are you supporting OS 10.4.x or older machines?
If they python option is still a possible option, I've included an example of how to use it here.
property PyMail : "/Users/TBD/Documents/Sample_Python_Email/PyMail.py" -- PATH TO PYTHON MAIL SCRIPT
on run
set emailTo to "youraddress#somedomain.com" -- MULTIPLE ADDRESS SHOULD BE A COMMA DELIMITED STRING LIKE THIS -- "address1#gmail.com, address2#hotmail.com"
set emailFrom to "Your Name <your.name#somedomain.com>"
set subject to "Demo Email"
set message to "Hi user,
I hope things are going well"
set pathToAttchment to "/Users/TBD/Desktop/prof.jpg" -- POSIX PATH TO FILE, LEAVE AS EMPTY STRING FOR NO ATTACHMENT
set username to "smtpusername" -- MAY OR MAY NOT BE REQUIRED IN YOUR CASE
sendPyMail(emailTo, emailFrom, subject, message, pathToAttchment, username)
end run
on sendPyMail(emailTo, emailFrom, subject, message, attachment, username)
try
do shell script "python " & quoted form of PyMail & " " & quoted form of emailTo & " " & quoted form of emailFrom & " " & quoted form of subject & " " & quoted form of message & " " & quoted form of attachment & " " & quoted form of username
return true
on error
return false
end try
end sendPyMail
Here is the python script (just copy and past it into a text editor and save as PyMail.py. You'll need to change the smtp server and possibly add the password that goes with the username you're supplying...
import sys
SMTP_TO = sys.argv[1]
SMTP_TO = SMTP_TO.split(',')
SMTP_FROM = sys.argv[2]
SUBJECT = sys.argv[3]
MESSAGE = sys.argv[4]
TEXT_FILENAME = sys.argv[5]
SMTP_USERNAME = sys.argv[6]
SMTP_SERVER = 'smtp.domainx.com'
SMTP_PORT = 25
SMTP_PASSWORD = ''
# now construct the message
import smtplib, email
from email import encoders
import os
msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
if TEXT_FILENAME != "":
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(TEXT_FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)
msg.attach(body)
if TEXT_FILENAME != "":
msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', ';'.join(SMTP_TO))
msg.add_header('Subject', SUBJECT)
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, SMTP_TO, msg.as_string())
mailer.close()
Run this in Terminal
echo "this is the body" | mail -s "this is the subject" "to#address"
Note: This assumes you have a locally installed MTA
Your AppleScript would be:
do shell script "echo 'this is the body' | mail -s 'this is the subject' 'to#address'"
Knowing nothing about python, I used TextWrangler and cut and paste the two scripts above.
The second one crashes at line 1, returning a syntax error, as follows:
line 1
property PyMail : "/Users/.../PyMail.py" -- PATH TO PYTHON MAIL SCRIPT
^
SyntaxError: invalid syntax
The carrot pointer is under the 'l' in 'PyMail'.
I commented out line 1, and it returned the same message for line 3.
I want a batch file program to get email.
For example I have a text file main.txt with some data
I want this to my mail id. Can you please help me in this programming.
Thanks in advance.
If you've got an email server you can send the emails to, I'd first recommend Blat as mentioned by PA's comment.
If you're running Microsoft Outlook email client, you can drive that with a VBScript script - not strictly a batch file but VBScript is generally part of Windows. Of course you can use a batch file to call the vbscript file with the right parameters.
(I've used this technique to schedule things in Outlook - schedule sending an email with a particular subject at a particular time.)
'SendMail.vbs
option explicit
' Script for sending mails to myself, with given subject and optionally file contents for body
' Note this only works with particular Schedule service settings, i.e.,
' it has to log on as me and have access to the Desktop
dim fso, f, oMailItem, oOlApp
' Create the mail
Set oOlApp = CreateObject("Outlook.Application")
Set oMailItem = oOlApp.CreateItem(0) '0 = olMailItem
oMailItem.Subject = WScript.Arguments(0)
oMailItem.Recipients.Add ("receiver.name#somemailserver.com")
if WScript.Arguments.Count > 1 then
Set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(WScript.Arguments(1), 1 )
oMailItem.Body = f.ReadAll
f.Close
end if
oMailItem.Send
set f = nothing
set oMailItem = nothing
set oOlApp = nothing
Call it with a command like
sendmail.vbs My_Subject_Line contents_file.txt
I am facing problems with Unicode named folders. When I drag the folder to the script, it doesn't show the path of the folder properly.
Simple VBScript (this is just a portion of it):
Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 1 Then
If FSO.FileExists(Wscript.Arguments.Item(0)) = true and FSO.FolderExists(Wscript.Arguments.Item(0)) = false Then
Alert "You dragged a file, not a folder! My god." & vbcrlf & "Script will terminate immediately", 0, "Alert: User is stupid", 48
WScript.Quit
Else
targetDir = WScript.Arguments.Item(0)
Wshso.Popup targetDir
End If
Else
targetDir = Wshso.SpecialFolders("Desktop")
Alert "Note: No folder to traverse detected, default set to:" & vbcrlf & Wshso.SpecialFolders("Desktop"), 0, "Alert", 48
End If
If it is a normal path without Unicode characters, it's fine. But in this case:
Directory: 4minute (포미닛) - Hit Your Heart
Then it will show something like 4minute (?) - Hit Your Heart
And if I do a FolderExists it can't find the dragged folder.
Is there any workaround to support Unicode named Folders?
Thanks!
I'll edit if this is not clear enough
This does seem to be a problem peculiar to the Windows Script Host's DropHandler shell extension. Whereas:
test.vbs "C:\포미닛.txt"
C:\WINDOWS\System32\WScript.exe "test.vbs" "C:\포미닛.txt"
both work when typed from the console (even if the console can't render the Hangul so it looks like ?), a drag and drop operation that should result in the same command goes through a Unicode->ANSI->Unicode translation that loses all characters that aren't in the current ANSI code page. (So 포미닛 will work on a default Korean Windows install but not Western.)
I'm not aware of a proper way to fix the problem. You could perhaps work around it by changing the DropHandler for .vbs files in the registry:
HKEY_CLASSES_ROOT\VBSFile\ShellEx\DropHandler\(Default)
from the WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D}, the one used for .exe, .bat and similar, which doesn't suffer from this issue. You would also probably have to change the file association for .vbs to put quotes around the filename argument too, since the EXE DropHandler doesn't, to avoid problems with spaces in filenames.
Since this affects argument-passing for all VBS files it would be a perilous fix to deploy on any machine but your own. If you needed to do that, maybe you could try creating a new file extension with the appropriate DropTarget rather than changing VBSFile itself? Or maybe forgo drop-onto-script behaviour and provide a file Open dialog or manual drop field instead.
For anyone landing here from Google...
Bobince's tip lead me to work around this problem by wrapping my vbscript file (myscript.vbs) in a dos batch file (mybatch.bat).
The tip was:
"Seem to be a problem peculiar to the Windows Script Host's
DropHandler shell extension whereas.... the one used for .exe, .bat and
similar... doesn't suffer from this issue."
mybatch.bat contains:
:Loop
IF "%1"=="" GOTO Continue
set allfiles=%allfiles% "%1"
SHIFT
GOTO Loop
:Continue
"myscript.vbs" %allfiles%
You may also find this code from my myscript.vbs to be helpful
For Each strFullFileName In Wscript.Arguments
' do stuff
Next
Based on DG's answer, if you just want to accept one file as drop target then you can write a batch file (if you have it named as "x.bat" place VBScript with filename "x.bat.vbs" at same folder) that just contains:
#"%0.vbs" %1
the # means to not output the row on the display (I found it to show garbage text even if you use chcp 1250 as first command)
don't use double-quotes around %1, it won't work if your VBScript uses logic like the following (code I was using below was from http://jeffkinzer.blogspot.com/2012/06/vbscript-to-convert-excel-to-csv.html). Tested it and it works fine with spaces in the file and folder names:
Dim strExcelFileName
strExcelFileName = WScript.Arguments.Item(0) 'file name to parse
' get path where script is running
strScript = WScript.ScriptFullName
Dim fso
Set fso = CreateObject ("Scripting.FileSystemObject")
strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
Set fso = Nothing
' If the Input file is NOT qualified with a path, default the current path
LPosition = InStrRev(strExcelFileName, "\")
if LPosition = 0 Then 'no folder path
strExcelFileName = strScriptPath & "\" & strExcelFileName
strScriptPath = strScriptPath & "\"
else 'there is a folder path, use it for the output folder path also
strScriptPath = Mid(strExcelFileName, 1, LPosition)
End If
' msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath
Modify WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D} and add this function to convert short path to long:
Function Short2Long(shortFullPath)
dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(shortFullPath)
Set app = CreateObject("Shell.Application")
Short2Long = app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
end function