Share This!

Thursday, August 29, 2013

MailMessage to Text

This simple class exposes a static method to take a MailMessage object and represent it as a block of text.  I ran into this because when an emailer program I wrote started getting error messages, the only thing I got in the error log was

Failure sending mail. 

 So - to track the problem, I wanted to quickly see what the mailmessage looked like and include it in my error notifications.

Here is the class

using System.Collections.Generic;
using System.Net.Mail;
using System.Text;

namespace mailmessage_to_text
{
    class MailtoText
    {
        private static string MailAddresstoString(MailAddress addy)
        {
            //"Russ Jansen" <RussJ@prosourceinc.com>
            string answer = "";
            if (addy != null)
            {
                if (string.IsNullOrEmpty(addy.DisplayName))
                {
                    answer = string.Format("<{0}>", addy.Address);
                }
                else
                {
                    answer = string.Format("\"{1}\" <{0}>", addy.Address, addy.DisplayName);
                }
            }
            return answer;
        }

        private static string MailAddressCollectionToString(MailAddressCollection mc)
        {
            List<string> addies = new List<string>();
            foreach (MailAddress addy in mc)
            {
                addies.Add(MailAddresstoString(addy));
            }
            return string.Join(", ", addies.ToArray());
        }

        /// <summary>
        /// Convert a MailMesage to a string
        /// </summary>
        /// <param name="msg">the MailMessage to convert</param>
        /// <returns>the MailMessage as a string</returns>
        public static string MessageToString(MailMessage msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("FROM: {0}", MailAddresstoString(msg.From)).AppendLine();
            sb.AppendFormat("TO: {0}", MailAddressCollectionToString(msg.To)).AppendLine();
            sb.AppendFormat("CC: {0}", MailAddressCollectionToString(msg.CC)).AppendLine();
            sb.AppendFormat("BCC: {0}", MailAddressCollectionToString(msg.Bcc)).AppendLine();
            sb.AppendFormat("SUBJECT: {0}", msg.Subject).AppendLine();
            sb.AppendFormat("BODY: {0}", msg.Body).AppendLine();

            foreach (Attachment att in msg.Attachments)
            {
                sb.AppendFormat("Attachment: {0}", att.Name);
            }

            return sb.ToString();
        }

    }
}
 This class exposes a single static function MessageToString() that will convert a MailMessage object to a readable string.  Perfect for logging or sending in a notification email.

The output looks like this:
FROM: "Bryan" <bryan@gmail.com>
TO: "Bob" <bob@gmail.com>, "Bill" <bill@gmail.com>, <some@yahoo.com>
CC: <bossman@cap.com>
BCC:
SUBJECT: New WebSite Contact
BODY: Blah has submitted the following on the CONCACT.ASPX page.
   Name: Ruby
   Email: Ruby@rails.com

...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Friday, August 23, 2013

Visual Studio Grrr 1


video

This video illustrates the horrible gyrations you have to go through to create event handlers for components that are embedded into tables.  Clearly a fix is needed to the Visual Studio for Web interface, but they have added a feature that - while not a good solution - is at least a usable workaround.
...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Thursday, August 22, 2013

Dear Job Poster

Your recent job posting really gave me a good chuckle.  You want a "Programmer Analyst" - which is a junior programmer title - with the following:

Master's Degree or foreign equiv. in CS,Comp.Engng,Computer App.,or related field, plus 2 yrs of exp.in job offered,S/W consultant, engnr or related

an expert in...
HTML, DHTML, CSS, PHP, XML PERL, CORBA,  C, C++, and JAVA;

and then
Oracle, SQL, and MS Access

So, overall expert in DOS/Windows and Linux technologies.

I'm not sure why you think someone with a masters in CS will want a junior programmer job, or what you gained by shortening engineer to engnr.  I also don't know what you mean by SQL, there are MySQL, MSSQL, Sybase SQL Server - or do you just mean the general concept of the structured query language? 

This is what happens when HR people try to write job postings with no understanding of what any of it means.  In reality, this looks like a Java web programmer position.  I'm not sure why anyone with a masters degree would be interested in this for more than an afternoon, as this kind of work is very mundane and repetitive. 

I may be unemployed, but I do enjoy a good chuckle.

...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Contact Us

Name

Email *

Message *