Automated Welcome mail to New Users/Enable mailbox is not displaying images in the HTML file

I have a working PowerShell script that sends automated mail when a new user mailbox is created or enabled on exchange server 2013, this works fine except that the images and logos in the html file does not display.

I have read somewhere that you have to add the images as a linked resource.

Anyone know the solution to this?

Thanks. 

June 9th, 2015 5:32am

I did something like this a while back. Unfortunately, I've forgotten most of it. But ISTR that you have to attach the images, and invent a unique (within the message) content-id property for them, then use that in your <img src="cid:whatever"> .

Or, you might just leave the images on your IIS server, and source them that way.

Free Windows Admin Tool Kit Click here and download it now
June 9th, 2015 5:52am

I used the code below on the script and also edited the HTML document code by changing the imagedata src = cid:xxxx.jpg.

but its still not working. I get an error that "you cannot call a method on a null-valued expression......

$image = New-Object System.Net.Mail.LinkedResource("C:\Welcomemsg\image001.jpg")
$image.ContentId = "image001.jpg"
$image.ContentType = "image/jpg"
$view.LinkedResources.Add($image)

                       
June 9th, 2015 7:26am

I've never tried this in powershell, so I don't know exactly where to look.  Do you get this error only once you'd added the $image code?  Does it say exactly which line causes the error?  You may need to share the entire script, so that I can try it here.

Edit:

BTW, the reference after <img src="cid: is supposed to match the value for $image.ContentId = .  Apologies if that seems obvious, but in the above code they don't

Free Windows Admin Tool Kit Click here and download it now
June 9th, 2015 7:33am

this is the script i used.....also in the html file the config was imagedata src = cid:image001.jpg for each instance of image.

this is the error i receive

The cmdlet extension agent with the index 5 has thrown an exception in OnComplete(). The exception is: Microsoft.Exchange.Provisioning.ProvisioningException: ScriptingAgent: Exception thrown while invoking scriptlet for OnComplete API: You cannot call a method on a null-valued expression.. ---> System.Management.Automation.RuntimeException: You cannot call a method on a null-valued expression. at CallSite.Target(Closure , CallSite , Object , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) --- End of inner exception stack trace --- at Microsoft.Exchange.ProvisioningAgent.ScriptingAgentHandler.OnComplete(Boolean succeeded, Exception e) at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnCompleteImpl(Task task, Boolean succeeded, Exception exception)

<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">

<Feature Name="Welcome Email" Cmdlets="New-Mailbox,Enable-Mailbox">

  <ApiCall Name="OnComplete">

   if($succeeded)    {


if ($provisioningHandler.TaskName -eq "New-Mailbox") {



                # Replace place holder.



                $USRdname=$provisioningHandler.UserSpecifiedParameters["Name"]

                $tempmsg= [string] (get-content ("c:\Welcomemsg\WelcomeMSG.htm"))

                $tempmsg = $tempmsg -replace "NewUser00",$USRdname



                # Picking Primary SMTP address,saving to $pSMTP to send email.



                $Ualias=$provisioningHandler.UserSpecifiedParameters["Alias"]

                $pSMTP = (get-mailbox $Ualias | select-object PrimarySMTPAddress | Format-Wide | Out-String ).trim()

}              



elseif ($provisioningHandler.TaskName -eq "Enable-Mailbox") {                

# Picking Primary SMTP address,saving to $epSMTP to send email.



                $eUalias=$provisioningHandler.UserSpecifiedParameters["Alias"]

                $dName = (get-mailbox $eUalias | select-object Name | Format-Wide | Out-String ).trim()         

                $pSMTP = (get-mailbox $eUalias | select-object PrimarySMTPAddress | Format-Wide | Out-String ).trim()



                # Replace place holder.





                $tempmsg= [string] (get-content ("c:\welcomemsg\welcomemsg.htm"))

                $tempmsg = $tempmsg -replace "NewUser00",$dName

}



                # Please give the correct HUB serve IP address in the following line.



                $HUBServer="10.180.1.10"



                $EMail =new-object net.mail.mailmessage

                $HUBTask = new-object net.mail.smtpclient($HUBServer)


                # Add any linked resources (e.g. images)


                $image = New-Object System.Net.Mail.LinkedResource("C:\Welcomemsg\image001.jpg")
$image.ContentId = "image001.jpg"
$image.ContentType = "image/jpg"
$view.LinkedResources.Add($image)


                $image = New-Object System.Net.Mail.LinkedResource("C:\Welcomemsg\image002.jpg")
$image.ContentId = "image002.jpg"
$image.ContentType = "image/jpg"
$view.LinkedResources.Add($image)


                # Email with link will be sent from the address given in the following line.



                $EMail.From="Administrator@lab.com"



                # Email with link will be sent TO the address given in the following line.



                $EMail.To.add($pSMTP)



                # Email Subject and Body details are speficied in following lines

                $EMail.Subject="Welcome"

                $EMail.Body= $tempmsg





                $Email.IsBodyHtml = $true

                $HUBTask.send($EMail)



   }

  </ApiCall>

</Feature>


</Configuration>

                                                                                        

  • Edited by OgeJessy 16 hours 9 minutes ago
June 9th, 2015 10:49am

Okay, thanks.  I'm at home now, so I'm not really in a position to try it out.  But I don't see anywhere where the $view in the two occurrences of

$view.LinkedResources.Add($image)

gets assigned to anything.  Should it be

$EMail.LinkedResources.Add($image)

?

Free Windows Admin Tool Kit Click here and download it now
June 9th, 2015 11:00am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics