Cyprich.com

Occasionally Relevant Insights from a Software Builder

By

SendMail Function for PowerShell

The SendMail function for PowerShell will send an e-mail through an SMTP server. I’m currently using it for backing up a web server to a virtual machine and sending a confirmation by e-mail on the status of the backup.

Usage is very simple:

          SendMail Host From To Subject Message UserName Password

The Host is the mail server address, i.e. smtp.mail.com. The From and To are the sender and receiver of the e-mail. The Subject and Message are the title and contents of the e-mail. If authentication is required, then enter values for the UserName and Password, otherwise make them blank using single quotes, i.e. ‘’.

The function will return a boolean value: true if the mail was sent successfully, false if it couldn’t be sent. See example in this post.

 

SendMail Code

The following is the code for the SendMail function which can be pasted in your PowerShell application.

[== Code ==============================================]

function SendMail ([string] $sHost, [string] $sFrom, [string] $sTo, [string] $sSubject, [string] $sMessage, [string] $sUserName, [string] $sPassword)

####################################################################################################
# PURPOSE:
#   Sends an e-mail to an SMTP server. It will also authenticate to the server if a user name and
#   password are sent to the function. If authentication is not used, leave $sUserName and $sPassowrd
#   blank, i.e. ”.
#
# PARAMETERS:
#   $sHost (in) – host name of mail server
#   $sFrom (in) – e-mail address of mail sender
#   $sTo (in) – e-mail address of mail recipient
#   $sSubject (in) – subject title of message
#   $sMessage (in) – message sent to recipient
#   $sUserName (in) – user name for authentication
#   $sPassword (in) – password for authentication
#
# RETURN:
#   True – mail was sent succesfully
#   False – mail was not sent
####################################################################################################

{
  [bool] $bSuccess = 1                 # was mail sent without problems?

  $SmtpClient = new-object system.net.mail.smtpClient 
  $smtpclient.Host = $sHost

  # Authenticate only if a user name and password are set.
  if (($sUsername -ne ”) -band ($sPassword -ne ”))
  {
    $Credentials = new-object System.Net.networkCredential

    $Credentials.domain = ""
    $Credentials.UserName = $sUserName
    $Credentials.Password = $sPassword
    $SMTPClient.Credentials = $Credentials
  } # if (($sUsername -ne ”) -band ($sPassword -ne ”))

  # Send the mail.
  try
  {
    $smtpclient.Send($sFrom, $sTo, $sSubject, $sMessage)
  }
  catch
  {
    $bSuccess = 0
  }
  $bSuccess
} # SendMail

[== Code ==============================================]

 

Example of the SendMail Function

This is an example of how the SendMail function should be used.

[== Code ==============================================]

$bSent = SendMail ‘smtp.mail.com’ ‘sender@website.com’ ‘recipient@website.com’ ‘Test’ ‘This is a test.’ ‘sender@website.com’ ‘abc123’

if ($bSent)
{
  "Mail sent successfully."
}


else
{
  "Mail not sent."
}

[== Code ==============================================]

 

You can download the SendMail code and example here.

By

How to Redirect Web Pages in IIS

Redirecting web pages becomes necessary when a web site is updated and old pages are replaced with new ones. This is very common when a site is upgraded to a CMS which typically displays pages as folders (i.e. www.website.com/about) as opposed to www.website.com/about.htm.

Its important to redirect old pages to new ones because if the old page is bookmarked in a web browser, it will lead to a 404 error page and the user will probably not try to manually change the URL to go to the home page. Also, its better for SEO to have a consistent page structure in your site.

Redirection in IIS

The easiest way to redirect web pages is through the Management Console in IIS in Windows. Find the page you want to redirect, right click on it, and select Properties. This will give you the following dialog box.

Redirecting a Page in IIS

Select the A redirection to a URL radio button and enter the URL of the new page in the Redirect to textbox. Press the OK button to save the settings. When the page is viewed in a web browser, it will automatically redirect the browser to http://www.website.com/about.

Redirecting Using HTML

You can also give instructions to redirect in a web page. The page that needs to be directed will contain the code below. The meta tag should be enough to do the redirect, but on some web servers it may not work. The JavaScript below will redirect only if JavaScript is enabled on the user’s web browser. The meta redirect is done on the server side and doesn’t require any settings on the user’s part.

<html>
<head>
<meta http-equiv=”Refresh” content=”0; http://www.website.com/about”>
</head>

<body>
<script language=”javascript”>
window.location = “http://www.website.com/about”;
</script>
</body>
</html>

By

How to Remove Background Image from Windows Desktop Icons

I somehow got background images enabled on my Windows XP desktop. There is no obvious solution to fixing this problem. It took some time experimenting and researching, but I finally found how to fix it.

Icons with a background image look like this:

Windows Desktop Icons with a Background Image

They should look like this:

Windows Desktop Icons without a Background Image

While I found several solutions to fix this, only one of them worked for me. To remove the background image, right click in an empty area on the desktop. A menu will open. Move the mouse over Arrange Icons By and you’ll open the next menu. You will see Lock Web Items on Desktop checked. If you click on this menu item, it will uncheck the option and remove the background images on the icons.

Removing Background Image from Windows Desktop Icons