How to send a email message using Telnet

Occasionally I’ve had to troubleshoot sending email messages to a particular SMTP server by simulating a mail client (or .NET mail code) by using the telnet client. Here’s how I do it on Windows:

  1. open the command prompt
  2. type: telnet smtpserver.domain.com 25 <press enter>
  3. type: helo server.com <press enter>
  4. type: mail from: you@domain.com  <press enter>
  5. type: rcpt to: someone@domain.com <press enter>
  6. type: data <press enter>
  7. write a test message. Typically I type something like: hello world! <press enter>
  8. to send the message type a single period (.) on a line by itself and of course <press enter>

How to check if a file exists on an FTP server

This is a method that I’ve used in the past to check for file existence on an FTP server.

Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
Dim request As FtpWebRequest = WebRequest.Create(fileUri)
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.GetFileSize
Try
Dim response As FtpWebResponse = request.GetResponse()
' THE FILE EXISTS
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
' THE FILE DOES NOT EXIST
Return False
End If
End Try
Return True
End Function

Get’s called like this:

If CheckIfFtpFileExists("ftp://ftp.domain.com/filename.txt") Then
...
End If

Using Web Slices with Nerd Dinner

The web slices feature was introduced in IE8 and allows users to subscribe to content directly within a webpage. Web slices allow web site developers to deliver content to the user’s browser no matter what the user is doing. When the content inside the web slice is modified, the user is notified through their Favorites bar and they can respond however they deem appropriate.

To define it, the element that encloses the region of the web slice needs to have the class ‘hslice’ and an unique id. Inside the web slice, items with the class ’entry-title’ will represent the title in the web slice window. Also, items with the class ‘entry-content’ represent the actual content of the web slice.

For those that are interested, the checkin of this code is on the NerdDinner CodePlex site. Here is the markup we created for NerdDinner.com to make the Popular Dinners section of the site available as a web slice.

<div id="2" class="hslice">
<h2 class="entry-title">Popular Dinners</h2>
<div></div>
<a rel='feedurl' href='/Dinners/WebSlicePopular' style='display:none;'></a> </div>

which looks like this when it’s rendered on the page: and looks like this in the Favorites bar: The code added to the controller class to handle this behavior looks like this:

public ActionResult WebSlicePopular()
{
    ViewData[""Title""] = ""Popular Nerd Dinners"";
    var model = from dinner in dinnerRepository.FindUpcomingDinners()
                                orderby dinner.RSVPs.Count descending
                                select dinner;
    return View(""WebSlice"",model.Take(5));
}

And the View is:

&lt;%@ Page Language=&quot;&quot;C#&quot;&quot; Inherits=&quot;&quot;System.Web.Mvc.ViewPage"" ContentType=""text/html"" %&gt;





    &lt;


    <div>
        <div>
            <h2></h2>
            <div>
                <ul>
                    
                    <li>
                        
                        on <strong>
                            
                            </strong> at
                        
                    </li>
                    
                </ul>
            </div>
        </div>
        <a></a>
    </div>


The MSDN site has some great Web Slice resources, including tutorials to help you get started.

Seo Improvements with ASP.NET 4

ASP.NET 4 had been released for a few months now so I thought that I’d take a look at two new properties added to the System.Web.Page class – MetaDescription and MetaKeywords. These two attributes represent corresponding meta tags in your page and get rendered on the page like so:


    your DESCRIPTIVE KEYWORDS title goes here
    
    

Both the description and keywords meta tags can be important in search engine optimization or (SEO). Specifically, Google (and others) use the description meta tag for improving search listings. (For more details, see Improve snippets with a meta description makeover on the Google Webmaster Central blog.) Also, according to Meta Keywords Advice on the Search Engine Guide Web site, Google no longer uses the contents of the keywords meta tag in their search indexes. It’s my understanding that Windows Live Search doesn’t use the keywords meta tag in determining it’s page rankings, but others may, so it doesn’t hurt to build dynamic content in the keywords meta tag.

These new properties may seem a bit trivial, but prior to ASP.NET 4 if you wanted to provide dynamic values for either of these properties you would have to inject the values into the header. There are two ways of doing this. The first is by adding Literal controls into the element and setting their text in the CodeBehind (Method 1). The other was by creating a HtmlMeta control in the code behind and add them to the control collection for the header element (Method 2).

Here’s an example of Method 1:

MetaOld.aspx







    
    
    


    
    

MetaOld.aspx.vb


Partial Class MetaOld
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Title = ""your DESCRIPTIVE KEYWORDS title goes here""
        metaDescription.Text = """"
        metaKeywords.Text = """"
    End Sub
End Class

Alternatively, here’s an example of Method 2. Notice that there is no markup required in the aspx page to support this technique:

MetaOld2.aspx







    


    
    

MetaOld2.aspx.vb


Partial Class MetaOld2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Title = ""your DESCRIPTIVE KEYWORDS title goes here""

        Dim metaDescription As New HtmlMeta
        Dim metaKeywords As New HtmlMeta

        metaDescription.Name = ""DESCRIPTION""
        metaDescription.Content = ""Your keyword rich marketing sales-pitch meta description goes here""

        metaKeywords.Name = ""KEYWORDS""
        metaKeywords.Content = ""your keywords,go here,separated by a comma,but not a space""

        Page.Header.Controls.Add(metaDescription)
        Page.Header.Controls.Add(metaKeywords)
    End Sub
End Class

Since ASP.NET 4, Microsoft introduced the MetaDescription and MetaKeywords attributes in the System.Web.UI.Page class achieving the same behavior is much simpler. You can set these attributes at run time which allows you to generate the content dynamically and provide better descriptions for a particular page (Method 3). Alternatively, you can set the MetaDescription and MetaKeywords attributes declaratively in the @ Page directive at the top of your aspx page (Method 4).

Here’s Method 3, in this example using only the new attributes in the codebehind there isn’t anything needed in the aspx markup to support this.

MetaNew.aspx







    


    
    

MetaNew.aspx.vb


Partial Class MetaNew
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.Title = ""your DESCRIPTIVE KEYWORDS title goes here""
        Page.MetaDescription = ""Your keyword rich marketing sales-pitch meta description goes here""
        Page.MetaKeywords = ""your keywords,go here,separated by a comma,but not a space""
    End Sub
End Class

Method 4, since these attributes are a part of the Page class you can use them declaratively in the Markup as well:

MetaNew2.aspx







    your DESCRIPTIVE KEYWORDS title goes here


    
    

An important note: these properties share a couple of behaviors with the Title attribute of the Page class. First, if there are no description or keywords meta tags in the head element, setting either the MetaDescription or MetaKeywords attributes will cause the meta tags are added to the page when it is rendered. Second, if there are already description or keywords meta tags in the head element, the MetaDescription and MetaKeywords attributes will act as get and set methods for the contents of the existing tags.

What’s causing this ‘Hashtable insert failed. Load factor too high.’ error?

On a number of occasions I’ve seen this error on an ASP.NET Web Application that’s under a fairly heavy load.

Hashtable insert failed. Load factor too high.  At:
    at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
    at System.Runtime.Serialization.SerializationEventsCache.GetSerializationEventsForType(Type t)
    at System.Runtime.Serialization.SerializationObjectManager.RegisterObject(Object obj)
    
    at System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer)

The problem is that it just doesn’t happen once or twice and go away. It requires a restart of the W3SVC service which takes the entire web server down. After looking at the stack trace in the error my first thought was that there was a Hashtable being used in the application that wasn’t thread safe. Unfortunately, it wasn’t that easy. There were no Hashtables being used directly in the code. So I decided to use a Support Services call to try to get to the bottom of what was going on.

After a number of back-and-forths, Microsoft sent a patch (which is now available publically at http://support.microsoft.com/kb/968432 ). They didn’t explain what the problem was, so I fired up ildasm and looked at the changes. What I noticed were changes to a number of methods in the System.ComponentModel.ReflectTypeDescriptionProvider class. This class is a .NET internal class that is responsible for providing Type information about particular classes.

If you look at the Before and After snippets below, you’ll see the kind of changes that were made to all of the methods in this class. Basically, the code that is responsible for building a Hashtable wasn’t thread safe, hence the additional lock in the After snippet.

Before (System.dll version 2.0.50727.4016):

private static Attribute[] ReflectGetAttributes(Type type)
{
    if (_attributeCache == null)
    {
        lock (_internalSyncObject)
        {
            if (_attributeCache == null)
            {
                _attributeCache = new Hashtable();
            }
        }
    }

    Attribute[] array = (Attribute[]) _attributeCache[type];
    if (array == null)
    {
         object[] customAttributes = type.GetCustomAttributes(typeof(Attribute), false);
         array = new Attribute[customAttributes.Length];
         customAttributes.CopyTo(array, 0);
         _attributeCache[type] = array;
    }
    return array;
}

After (System.dll version 2.0.50727.4038):

private static Attribute[] ReflectGetAttributes(Type type)
{
    if (_attributeCache == null)
    {
        lock (_internalSyncObject)
        {
            if (_attributeCache == null)
            {
                _attributeCache = new Hashtable();
            }
        }
    } 

    Attribute[] array = (Attribute[]) _attributeCache[type];
    if (array == null)
    {
        lock (_internalSyncObject)
        {
            array = (Attribute[]) _attributeCache[type];
            if (array == null)
            {
                object[] customAttributes = type.GetCustomAttributes(typeof(Attribute), false);
                array = new Attribute[customAttributes.Length];
                customAttributes.CopyTo(array, 0);
                _attributeCache[type] = array;
            }
        }
    }
    return array;
}

I passed the MS 70-547 Exam

I just passed the Microsoft Exam 70-547 (TS: Microsoft® .NET Framework 2.0 – Web-based Client Development) at here at Tech-Ed Developers in Orlando. Again, I used the same series as a study guide “MCPD Self-Paced Training Kit (Exam 70-547): Designing and Developing Web-Based Applications Using the Microsoft® .NET Framework“, by Mike Snell, Bruce Johnson, Brian Lanham, Shawn Wildermuth, and Sara Morgan. This upgrades my certification to Microsoft Certified Professional Developer “Web Developer”.

So, I’ve been able to get my MCPD just in time for Microsoft to release a new series of certification exams for Microsoft .NET 3.5.

I passed the MS 70-547 Exam

I just passed the Microsoft Exam 70-547 (TS: Microsoft® .NET Framework 2.0 – Web-based Client Development) at here at Tech-Ed Developers in Orlando. Again, I used the same series as a study guide “MCPD Self-Paced Training Kit (Exam 70-547): Designing and Developing Web-Based Applications Using the Microsoft® .NET Framework“, by Mike Snell, Bruce Johnson, Brian Lanham, Shawn Wildermuth, and Sara Morgan. This upgrades my certification to Microsoft Certified Professional Developer “Web Developer”.

So, I’ve been able to get my MCPD just in time for Microsoft to release a new series of certification exams for Microsoft .NET 3.5.