Unable to Send Outlook Email via Powershell

0

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 = "my.email@domain.com"
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.To = "$global:UserReportsToEmail"
$mail.cc = "EMAIL@domain.com"
$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()
powershell
asked on Stack Overflow Oct 26, 2016 by MrMr • edited Oct 26, 2016 by Mansuro

2 Answers

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 = @("user@mail.com", "user2@mail.com", "user3@mail.com")
        $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()
    }
answered on Stack Overflow Oct 26, 2016 by freakydinde • edited Oct 26, 2016 by freakydinde
0

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.

answered on Stack Overflow Feb 16, 2017 by BSAFH

User contributions licensed under CC BY-SA 3.0