Email Newsletters/Campaigns

In the IT world a person tends to learn something new every day, if not hourly. It's a fast paced world that we all live in, especially once we start talking about computers. Today I learned a new feature of e-mail that I had never known about.

When you visit a web site and you subscribe to their newsletter you are often asked if you would prefer an html or a text version of their newsletter. Seeing this over the years off an on, I always assumed the only way to send html and text campaigns/newsletters out correctly was to keep two separate lists. Well, it turns out I was wrong.

There's a way to send out both the html and the text at the same time. The client will then decide if it wants to, or can, display the html or the text version. The technology for this is called Multipart/Alternative MIME.

This little bit of information crossed my path when I was trying to create a simplistic Mass Mailing application. I have an html version that the program creates, and a text version that the program creates. I then have a huge list of e-mails that need to be sent out to people. So I created some C# code.

 
        private void btnSendEmails_Click(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToString();
            SmtpClient client = new SmtpClient("smtpserver");
            lblTotalEmails.Text = "100";
            progressBarEmails.Maximum = 100;
            progressBarEmails.Minimum = 0;
            progressBarEmails.Value = 0;
            for (int i = 0; i < 5; i++)
            {
                client.Send(createMail("AdressToSendTo"));
                lblSent.Text = "" + i;
                progressBarEmails.Value++;
                this.Refresh();
            }
 
            MessageBox.Show(time + " : " + DateTime.Now.ToString());
        }
 
        private MailMessage createMail(string to)
        {
            MailMessage mail = new MailMessage();
            AlternateView viewTxt = new AlternateView(txtLoc, System.Net.Mime.MediaTypeNames.Text.Plain);
            AlternateView viewHtml = new AlternateView(htmlLoc, System.Net.Mime.MediaTypeNames.Text.Html);
            mail.AlternateViews.Add(viewHtml);
            mail.AlternateViews.Add(viewTxt);
            mail.To.Add(to);
            mail.From = new MailAddress("FromAddress");
            mail.Subject = txtSubject.Text;
            mail.IsBodyHtml = true;
            return mail;
        }

Basically all that needs to be done, is to add the alternative views. Once the e-mail is sent out, the client then decides which version to display.

Time to go learn something new.

Print | posted on Wednesday, January 30, 2008 3:51 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)