I use powershell to send emails from Outlook (complete automatic process). This work perfectly for the default mailbox. I have a lot of restrictions in the development network and can't use any software other than Outlook and powershell.
My question is: Is there a method to send email from a different account in outlook, using powershell
(in my Outlook I have three accounts A, B and C), even if i have one of the as predetermined.
The code that I use is this.
$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = "SUBJECT"
$mail.body = "BODY"
$mail.To = "email001#xx.com;"
$mail.Send()
Is there a property I can set to make the email origin to be B or C, and not the predetermined A.
You need to set the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent. For example:
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
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
Related
I'm looking into a script that can access an Outlook folder and read the content of the email. I hope (don't know how yet) to parse the body to extract a URL from it. However, the body of my email and other messages I've tried it on keeps returning empty.
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GETNAMESPACE("MAPI")
$inbox = $ns.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$inbox.Items($x).Body
# returns nothing
Same result for .HTMLBody and .RTFBody.
Possibly related is that SenderName is blank too. Is it possible that have a corporate IT policy that prevents me from seeing my own message metadata? I'm exploring Powershell because they've disabled VBA in Outlook already.
Currently, each day my business needs to send and encrypted email using MS Outlook. The process works, but obviously the whole 'doing it every day thing' sucks. I've tried looking up ways to automate the process which has helped me solve the surrounding processes, but i still can't find anything, or whip up anything myself which will handle the email side. The flow of events are;
Receive
Encrypted email is received
Email is opened and attachment is dragged to UNC share 1 (repeated for each email recieved)
Attachment is picked up and processed by surrounding script
Send
Browse UNC share 2
Copy 1 of x files to a new email
Set subject as full file name
Select email to be signed/encrypted in top ribbon
Send email and repeat for each file in UNC share 2
I can get emails to send with attachment via powerhsell and by using S/MIME, but once I combine the two I get stuck and the script turns to rubble :(
Any help at all would be greatly appreciated
If I'm not mistaken, Powershell can load and use .NET assemblies, correct? (I'm not a powershell user, so my knowledge is limited).
Given that, I would recommend taking a look at using the MimeKit and MailKit libraries.
Check out the MimeKit README for some examples on how to use the S/MIME API's.
MimeKit comes with a WindowsSecureMimeContext which you might be able to use out of the box, but that depends on where your private keys and certificates are stored. If they aren't in the X509Store, then you could use the TemporarySecureMimeContext and simply load the certificate(s) and private key(s) into that and use it.
Got a similar problem and here's my solution so far, still testing:
$Outlook = New-object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "me#company.tld"
$Mail.Subject ="Test"
$Mail.Body ="Test Body"
# 0 = nothing, 1 = encrypt, 2 = sign, 3 = both!
$PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003"
$Mail.PropertyAccessor.SetProperty($PR_SECURITY_FLAGS, 3)
$Mail.Send()
If Outlook is running, everything works fine. But when I start Outlook afterwards, I will find the created Mail in the Outbox without encryption and not signed.
Without Outlook running, the Code would produce an "InvokeMethodOnNull"-Error. Putting a $Mail.Display() before the SetProperty()-Part would take care of that and the buttons seem to be checked in the opening E-Mail, but the result will be the same: Start Outlook and your mail is in Outbox without encryption and not signed.
I'm writing a script that can send mails to a certain e-mail address but I'm having some trouble with the sending itself.
The problem is that when I send a mail with the script it takes approximately 1min to send. I can see this because I can see it in my outbox.
But when I create this exact same mail manually in outlook it only takes a matter of seconds to process and send the mail.
My code for creating and sending the mail:
(I use Redemption)
$mItem = $ol.CreateItem(0)
$mail = $routlook.GetRDOObjectFromOutlookObject($mItem)
$mail.To = "<Recipient's Address>"
$mail.Subject = "Some Subject"
$mail.Body = "Some Body"
$mail.Attachments.Add("<Path to Attachment>")
$mail.DeleteAfterSubmit = $True
$mail.Send()
I don't know what the problem is. Could this be due to my code? or perhaps the exchange-server or the Outlook client?
How can I make the mails from the script send faster?
Please help,
Thanks!
You can use send-mailmessage directly in your code without the long way through COM and Outlook.
Assuming an Exchange version of at least 2007, you'd be much better off using the EWS Managed API than Outlook/COM.
I'm trying to read some data from a .pst file using powershell and I can't seem to extract some attributes for mail items, specifically:
recipients, sender, cc, bcc, body
for some reason when I try to access these properties from code it shows up as blank. Most of the other properties show up file (subject, attachments etc...)
I'm using the following code:
$objOutlook = new-object -ComObject Outlook.Application
$ns = $objOutlook.GetNamespace("MAPI")
$ns.AddStore($pst.FullName)
$folders = $ns.Folders
$archiveStore = $ns.Folders[$pst.Name.Replace(".pst","")]
from there I just use Folders and Items recursively to get my mail items.
Any ideas?
This returns the sender for me.
$archiveStore.Items | %{$_.SenderName}
Which property are you using to fetch the recipients,sender,cc,bcc,body?
Where and how do you run a PowerShell script? And what code exactly do you use to fetch the property values?
The fact is that Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. See Considerations for server-side Automation of Office for more information.
I have the following code where i could send mail through my configured outlook.
I can run this vbs using a rule in my outlook which in turn send a mail to the email specified in the script
But i am getting a confirmation box asking a virus or not while running this script to send a mail.
How to get rid of this confirmation box to make always allow to send mails.
Dim ToAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
ToAddress = "John.Smith#place.com" ' change this...
MessageSubject = "My Subject"
MessageBody = "DATA"
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
ns.logon "","",true,false
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
' validate the recipient, just in case...
Set myRecipient = ns.CreateRecipient(ToAddress)
myRecipient.Resolve
If Not myRecipient.Resolved Then
MsgBox "unknown recipient"
Else
newMail.Recipients.Add(myRecipient)
newMail.Send
End If
Set ol = Nothing
I believe your being hit by the now built-in security feature(s) that Microsoft put in place a couple years back with a security patch. The only way I know around it to is to digitally sign the code and then import the certicate that was used to sign that code into the certificate store, or better yet use the Redemption DLL. From the Redemption DLL site:
Outlook Redemption works around
limitations imposed by the Outlook
Security Patch and Service Pack 2 of
MS Office 98/2000 and Office
2002/2003/2007 (which include Security
Patch) plus provides a number of
objects and functions to work with
properties and functionality not
exposed through the Outlook object
model.
The DLL can be downloaded from here: http://www.dimastr.com/redemption/download.htm, and if you look around you can find several examples of how to use it. Here is one to get you started: http://www.utteraccess.com/forums/printthread.php?Cat=&Board=80&main=409393&type=thread
Also please note the peviously posted and answered questions:
How to avoid Outlook security alert when reading outlook message from C# program
Outlook nagging dialog about macro