Try to send email but get error

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Try to send email but get error

Post by B Daring »

I am trying to send an email using the below function. If I send it and use the display() at the end it works just fine, email window pops up and I can click send. But if I have send() it complains the is something wrong with the names when it tries to resolve the names. I get an error message of "System.__ComObject". But if I comment those out then it gives the catch error of "Outlook does not recognize one or more names." So I thought maybe it is happening to fast so I gave it a sleep command. But, I don't know if I am going down the right road.

Why doesn't it resolve?

Code: Select all

function SendEmail
{
	
	
	Param (
		[Parameter(Mandatory)][string]$To,
		[Parameter(Mandatory)][string]$URL,
		[Parameter(Mandatory)][string]$Reason,
		[Parameter(Mandatory)][string]$pic
		
	)
	
	#$winscript.popup("`r`r          Trying to Send Email          `r`r ", 4, "Windows Check", 4160)
	
		try
		{
		#$Check = Get-Process -Name OUTLOOK
		$Outlook = New-Object -comObject Outlook.Application
		}
		catch
		{
		write-host "outlookWasAlreadyRunning= "$Check
			# check for Outlook to be running
			if (($Outlook -eq $false) -or ($Outlook -eq $null))
			{
				$winscript.popup("`r`r          Outlook is unavailable.`r`r", 4, "Access Request", 4160)
				[environment]::exit(0)
			}
			
		}
	
	
	write-host "Approvers= "$To
	write-host "Pic= "$pic
	$html = @"
    <html>
        <body> 
            $DisplayName ($UserID) is requesting access.
<br /><br />
URL: $URL
<br /><br />
Reason:<br /> 
$Reason <br /><br />
This is the message:<br /><br />

            <img src="data:image/png;base64,{0}">
            
        </body>
    </html>
"@
	#$To = $To.replace(',', ';')
	# Convert image to Base64 string
	$imageBase64String = [Convert]::ToBase64String((Get-Content $pic -Encoding Byte))
	
	# Build mail message
	$html = [System.String]::Format($html, $imageBase64String)
	
	$Mail = $Outlook.CreateItem(0)
	[array]$Members = $To.split(",")
	$ToCount = $Members.Count
	write-host "ToCount= "$ToCount
	if ($ToCount -gt 1)
	{
		for ($i = 0; $i -lt $ToCount; $i++)
		{
			$Recipient += $Members[$i] + "@domain.org;"
			#$Mail.Recipients.Add($Recipient)
			write-host "Recipient[$i]= "$Recipient
		}
	}
	else
	{
		$Recipient = $To+"@domain.org;"
		#$Mail.Recipients.Add($To)
		write-host "Recipient= "$To
	}
	$Recipient = $Recipient + 'user@domain.org;', 'user2@domain.org'
	write-host "All Recipient= "$Recipient
	$Recipients = $Recipient| ForEach{ $Mail.Recipients.Add($_) }
	
	start-sleep 2
	
	
	If (!($Mail.ResolveAll))
	{		
		ForEach ($myRecipient In $Mail)
		{			
			If (!($myRecipient.Resolved))
			{
				$winscript.popup("`r`r        Invalid Recipient Name:.`r`n`r`" $myRecipient ", 8, "Access Request - Error", 4160)
				write-host "Bad Recipient= "$myRecipient
			}			
		}
	}
	
	$Mail.Subject = "Access Request  - $curDept  - $DisplayName"
	$Mail.HTMLBody = $html

	try
	{
		If ($ShiftKeyOveride -eq 1)
		{
			$Mail.Display()
		}
		else
		{
			$mail.Send()
		}
	}
	catch
	{
		write-host "Outlook Send Error"
		$winscript.popup("`r`r          Access Request:`r`r`r          Error Sending Email: " + $_.Exception.Message, 4, "Access Request", 4160)

	}
	
	[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Try to send email but get error

Post by jvierra »

You can't just comment out an error line. You also haven't posted the error. What is the error and on which line?
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Try to send email but get error

Post by B Daring »

Hi jvierra,

It never gives me a line, but in the "if then" for the Resolver it is this line

$winscript.popup("`r`r Invalid Recipient Name:.`r`n`r`" $myRecipient ", 8, "Access Request - Error", 4160) - line 90

That gives the "System.__ComObject" error, but I have since found that if I get the To of the recipient ($myRecipient.To) I get the email address which totally fine. So not sure why it can't resolve. With that said this line is false

If (!($Mail.ResolveAll))

which is my problem, the address is valid and correct.
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Try to send email but get error

Post by B Daring »

Sorry, I haven't made myself clear. The line doesn't give me the error, it is the variable in that line that has the "System.__ComObject" error. but as I said doing "$myRecipient.To" it gives me the "Exception.Message" I was looking for. Now, I need to know why I can' t get the script to resolve any addresses. Is there a trick?
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Try to send email but get error

Post by B Daring »

I found my problem. It is this line

If (!($Mail.ResolveAll))

Should be

If (!($Mail.Recipients.ResolveAll))

Found a bad script that had the wrong parameters in it
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked