June 2007 Entries

Its very easy to send an email using .NET 2.0 and you can choose to send HTML and/or plain text using the AlternateView collection on the MailMessage class.

However until reading about it in my MCTS exam book i didn't realise that you could embed images (well anything i guess) into the email, i assumed you were limited to having to have them on a public website and then having a link to them... resulting in most email clients (gmail/outlook that i use) not downloading the images by default for security reasons. I'm sure you'll agree this is annoying, especially if you've spent time and money creating a fancy looking e-newsletter!

 


public static void SendEmail(MailAddress To, MailAddress From, string Subject, string ImageFilePath)
{
  //Create HTML body
  string htmlbody = "<html><body><h1>Hello!</h1><br /><img src=\"cid:Pic1\" /></body></html>";
  //Create HTML AlternativeView
  AlternateView avHTML = AlternateView.CreateAlternateViewFromString(htmlbody, null,   MediaTypeNames.Text.Html);
  //Create LinkedResource to hold the image
  LinkedResource pic1 = new LinkedResource(ImageFilePath, MediaTypeNames.Image.Gif);
  pic1.ContentId =
"Pic1";
  avHTML.LinkedResources.Add(pic1);
  //Add alternative view to message instead of using .Body
  MailMessage mail = new MailMessage();
  mail.AlternateViews.Add(avHTML);
  //Add addresses and subject
  mail.To.Add(To);
  mail.From = From;
  mail.Subject = Subject;
  //Send email
  SmtpClient smtp = new SmtpClient("smtpout.secureserver.net");
  smtp.Credentials = EmailCred;
  smtp.Send(mail);
}

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
It's easy to set the tooltip on a RadioButtonList control, but what if you want to set a different tooltip for each of the individual radio buttons? (actually ListItems as far as .NET is concerned).

Well its fairly simple, but undocumented, take this example which will set the text of each item to be it's tooltip:

  List<string> ds = new List<string>();
 ds.Add("Radio 1");
 ds.Add("Radio 2");
 ds.Add("Radio 3");

 RadioButtonList rbList = new RadioButtonList();

 rbList.DataSource = ds;
 rbList.DataBind();

 foreach (ListItem item in rbList.Items)
 {
     item.Attributes.Add("Title", item.Text);
 }


And this approach will work for anything, for example you might want to add an OnClick or mouse over events in the same way.

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist