System.Net.Mail.SmtpFailedRecipientException and Exchange 2007

Posted by Ryan Baxter Fri, 30 Jan 2009 17:45:00 GMT

I’d recently been struggling with a .NET application that sends email via SMTP through Exchange 2007 outside of my domain at work. That is, until I found a workaround that uses the Exchange 2007 Pickup folder. This eliminated my authentication hassles and resolved the dreaded Mailbox unavailable. The server response was: 5.7.1 Unable to relay error. I’ve posted the solution here, but I also suggest reading the original post.

SmtpClient smtpClient = new SmtpClient("EXCHANGESRV", 25) {
    DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory,
    PickupDirectoryLocation = "\\EXCHANGESRV\PickupFolder"
}

Keep in mind that you’ll either need sufficient write permission on the Pickup folder or be able to impersonate somebody that does. I happen to have written about identity impersonation a few months ago. You’re in luck.

Kudos to stackoverflow.com. I only wish I had enough rep to upvote the submitter. :(

And finally, kill Excel...

Posted by Ryan Baxter Thu, 15 Jan 2009 04:07:00 GMT

Everyone who’s worked with Excel on the Microsoft .NET framework has dealt with the problem of hanging Excel processes. The C# solution below is not very elegant, but it stops those pesky Excel processes from hanging around.

using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
private static extern
    int GetWindowThreadProcessId(int hWnd, out int processId);

private static void CreateSpreadsheet()
{
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

    try
    {
      // Create Excel spreadsheet.
    }
    catch (Exception ex)
    {
      // Do something with the exception.
    }
    finally
    {
      KillExcel(excel.hWnd);
    }
}

private static void KillExcel(int hWnd)
{
    int processId;
    int threadProcessId = GetWindowThreadProcessId(hWnd, out processId);

    Process.GetProcessById(processId).Kill();
}

Yet Another Join Method

Posted by Ryan Baxter Fri, 12 Sep 2008 21:27:00 GMT

The .NET String type has a Join method, but in my latest ASP.NET project I had the need for joining String array elements with additional prefix and suffix values. The method below delimits array elements with the provided separator and concatenates the elements with the prefix and suffix string parameters.

I’ve found this particularly handy when creating SQL statements that require the IN keyword.

string[] names = { "Bobby", "Suzy" };

sql += "WHERE People.FirstName IN (" + Utility.Join(",", names, "'", "'") + ")";

Add this method to your utility class or extended String type.

public static string Join(string separator, string[] value, string
prefix, string suffix)
{
    string toReturn = String.Empty;

    int i;
    for (i = 0; i < value.Length; i++)
    {
        if (i != value.Length - 1)
            toReturn += prefix + value[i] + suffix + separator;
        else
            toReturn += prefix + value[i] + suffix;
    }

    return toReturn;
}

Older posts: 1 2 3 4