1

I can create an email and display it with my script, but for some reason it doesn't send and I receive the following error. Am I missing something, maybe there's a permissions issue?

Exception calling "Send" with "0" argument(s): "Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))"At C:\TEMP\Scripts\PowerShell\Outlook EMail Creation\TestEMailSend.ps1:27 char:5
+     $mail.Send()
+     ~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

My code:

$global:UserReportsToEmail = "[email protected]"
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.To = "$global:UserReportsToEmail"
$mail.cc = "[email protected]"
$mail.Subject = "mySubject" 
$mail.HTMLBody = 
"<font color ='blue'><b>TESTING STUFFFF!</b></font><br>
Text on a new line $UserID"

$mail.Send()
$inspector = $mail.GetInspector
$inspector.Display()
2
  • I just tested your code and it works for me, which powershell version are you using? Commented Oct 26, 2016 at 14:59
  • I'm on version 4. Commented Oct 26, 2016 at 15:11

2 Answers 2

1

According to a few of the Microsoft sites (e.g. https://social.msdn.microsoft.com/Forums/en-US/80c66a08-66ee-4ab6-b629-6b1e70143eb0/operation-aborted-exception-from-hresult-0x80004004-eabort-outlook-appointment?forum=outlookdev ) this is due to 'object model guard'. A security feature to prevent automated programs from doing stuff like auto-emailing out viruses and stuff from the background.

You probably already worked this out. Posting this here so that others like myself can more quickly and easily understand why its not working.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for that redirection. However, do you happen to know how we can bypass it (specifically using PowerShell)?
No I don't. Most likely I was trying to do something similar, found the question, found my own answer and added it. For automated email I would just use a proper SMTP server and Send-MailMessage or similar.
Thank you. I found out that changing the required keys at the following location and restarting Outlook works: HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\16.0\Outlook\security Specifically, it was setting promptoomsend to 2 that worked for me.
0

you can use the Send-MailMessage CmdLets : https://technet.microsoft.com/en-us/library/hh849925.aspx

then when i need more controls and dispose functionnality i use System.Net.Mail.SmtpClient

    try
    {
        $emailCredentials = Import-Clixml "C:\testMail\credentials.clixml"
        $recipients = @("[email protected]", "[email protected]", "[email protected]")
        $attachments = @("C:\testMail\file.txt", ""C:\testMail\file2.txt", "C:\testMail\file3.txt")

        # create mail and server objects
        $message = New-Object -TypeName System.Net.Mail.MailMessage
        $smtp = New-Object -TypeName System.Net.Mail.SmtpClient($buildInfoData.BuildReports.Mail.Server)

        # build message
        $recipients | % { $message.To.Add($_) }
        $message.Subject = $subject
        $message.From = New-Object System.Net.Mail.MailAddress($emailCredentials.UserName)
        $message.Body = $mailHtml
        $message.IsBodyHtml = $true
        $attachments | % { $message.Attachments.Add($(New-Object System.Net.Mail.Attachment $_)) }

        # build SMTP server
        $smtp = New-Object -TypeName System.Net.Mail.SmtpClient(smtp.googlemail.com)
        $smtp.Port = 572
        $smtp.Credentials = [System.Net.ICredentialsByHost]$emailCredentials
        $smtp.EnableSsl = $true

        # send message
        $smtp.Send($message)

        Write-Host "Email message sent" 
    }
    catch
    {
        Write-Warning "$($_.Exception | Select Message, Source, ErrorCode, InnerException, StackTrace | Format-List | Out-String)" 
    }
    finally
    {
        Write-Verbose "Disposing Smtp Object"
        $message.Dispose()
        $smtp.Dispose()
    }

4 Comments

This is good to know, but unfortunately it's not an option for me.
sometimes you don't have stmp server permission, but have access to Outlook
you can't user either send-mailmessage or .net smtp object ? why ?
SMTP access is restricted, but I can use Outlook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.