Amadiere.com

Fourteen and a half crazy frog burpers

6th April 2010

Development SMTP Servers for IIS7.5 on Windows 7

Filed under: ASP.NET,C# — Tags: , , , , , — Alex Holt @ 3:43 pm

I’ve had one of them days. You know? That “simple task” that spiralled out of control and resulted in me losing half a day to it’s tricks! That task? It was sending an email from an ASP.NET (MVC2) application. Previously, I’ve always done this via setting up IIS and the SMTP server in there, but for some reason, Microsoft decided they didn’t want to include the SMTP Server in Windows 7 anymore (even ‘Ultimate’ – it might also be the case for Windows Vista). So, I had to find an alternative.

There were a few options available to me:

  • SMTP Server on Localhost: This was the obvious choice, but after trying Mercury Mail and it’s quarter of a million settings as it installed (I’m no Email Admin, so didn’t know the answer to all of them). It didn’t work and I’m not sure why. To rub salt into the would, there was no uninstall either – it proper irritated me and I gave up using it out of principal.
  • SMTP Server on Localhost that is really just a Relay. Well, sounded good – but again, it was designed by people with bigger brains than me and it failed to send to what I thought was a correctly configured IIS7.5 config pointing to my GMail account.
  • Fake Server: Something that doesn’t actually send emails, but pretends to.

The last one is the one I eventually choose and boy am I glad I did! I downloaded the excellent SMTP 4 DEV from Codeplex

  1. I don’t have 100′s of emails cluttering up my email box for starters. Win!
  2. It was so easy to set-up and it worked perfectly without a change to my code. Win!
  3. It’s free. Win!

Here is some fake code that should send an email to the localhost.

MailMessage emailMessage = new MailMessage();
string messageBody = "This is the content of the email will be awesome!";
 
emailMessage.Body = messageBody;
emailMessage.Priority = MailPriority.Normal;
emailMessage.From = new MailAddress("no-reply@amadiere.com"); // obviously, this email address doesn't exist :)
emailMessage.Subject = "The answer is 42";
emailMessage.IsBodyHtml = false;
 
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Host = "127.0.0.1";  // localhost
mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 
mSmtpClient.Send(emailMessage);

15th March 2010

Good Quality Image Resizing in C#

Filed under: C# — Tags: , , , — Alex Holt @ 8:40 pm

I encountered a little bit of a problem the other day with some image resizing code from within an ASP.NET MVC application that was misbehaving. The issue was just a general C# and ASP.NET one, not related to MVC or Webforms, but it was that for some reason the images were losing a significant amount of quality when resizing. I’m talking a pixel sharp 2000 x 2000 picture that when resized to 300 x 300, was woefully blurry. Initially, the code was simply using the GetThumbnailImage() method to produce it’s resizes, this turned out to be the mistake!

While GetThumbnailImage() is fine for small thumbnail images (the clue I guess, was in the name), it somewhat struggled on the larger versions. To fix the issue, I had to convert the image to a bitmap, faff about with it like that, then export it back to a Jpeg once I was done.

For future me (and anyone else this might help), here is the code I eventually settled on:

EncoderParameters encodingParameters = new EncoderParameters(1);
encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 90L); // Set the JPG Quality percentage to 90%.
 
ImageCodecInfo jpgEncoder = GetEncoderInfo("image/jpeg");
 
// Incoming! This is the original image. This line can effectively be anything, but in this example it's coming from a stream.
var image = Image.FromStream(new System.IO.MemoryStream(Picture));
 
// Creating two blank canvas. One that the original image is placed into, the other for the resized version.
Bitmap originalImage = new Bitmap(image);
Bitmap newImage = new Bitmap(originalImage, 300, (image.Height * 300 / image.Width));  // Width of 300 & maintain aspect ratio (let it be as high as it needs to be).
 
// We then do some funky voodoo with the newImage. Changing it to a graphic to allow us to set the HighQualityBilinear property and resize nicely.
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
 
var streamLarge = new System.IO.MemoryStream();
newImage.Save(streamLarge, jpgEncoder, encodingParameters);
 
// This is the line that returns the picture to the relevant part of the model.
_event.Picture = streamLarge.ToArray();
 
// No need for all that drama for the thumbnail, the loss of quality isn't noticable.
var thumbnail = image.GetThumbnailImage(80, (image.Height*80/image.Width), null, new IntPtr(0));
var streamThumbnail = new System.IO.MemoryStream();
 
thumbnail.Save(streamThumbnail, jpgEncoder, encodingParameters);
 _event.ThumbnailPicture = streamThumbnail.ToArray();
 
// Good boy's tidy-up after themselves! :O
originalImage.Dispose();
newImage.Dispose();
thumbnail.Dispose();
streamLarge.Dispose();
streamThumbnail.Dispose();

1st March 2010

4 Top Tips for portable ASP.NET MVC Apps

Filed under: ASP.NET,CSS,HTML,MVC — Tags: , , , — Alex Holt @ 10:22 pm

Loooong cat! :O

ASP.NET MVC is awesome (find out how awesome it is over dinner) and allows for some great applications to be made, quickly, while at the same time offering a high degree of maintainability over the code that is written. The danger with being able to do things too fast is that simple mistakes are sometimes made. These hopefully are nothing major, but can become an irritation at various points down the line. One of the things that occasionally gets left behind is the portability of code – and this can be a bit of a ‘damn I wish I’d done it like that to start with’ moment.

Below are a few hints that may (or may not) help you as you develop the next great slice of awesomeness.

Disclaimer: These tips are displayed as ASP.NET MVC tips, but in reality, some of them progress to general ASP.NET Websites and Applications – or just websites in general.

  1. Don’t Assume You Know Your Root
    Before I get started, let me give some background on this point. I have recently been doing some final tweaks to an otherwise great MVC application. However, one of the tweaks I did was to make sure that part of the system was securely done via HTTPS. When looking around the net, this appeared to be a lot trickier than I thought. After all, could all these people be wrong?:

    Basically, yes they can, but only because their articles are dated, not because they are fools (it should also be noted that although their articles may be dated, there are some good techniques and ideas in them, so worth a nosey). :) Bart Wullems does a good job of explaining the amazing simplicity of this new attribute that was added in MVC 2 Preview 2. I’m a little surprised as to how this maybe wasn’t given a little bit more publicity – its a useful tool that was sorely missing before. Behold, ye [RequiresHttps] attribute!

     [RequireHttps]
     public ActionResult Login()
     {
     return View();
     }

    This addition of SSL feather to my MVC Application’s bow was to prove too much for Visual Studio’s Cassini and I was forced onto my local IIS. This in itself wasn’t a great issue, but it highlighted some issues with the way the application had been developed. It had been assumed from the start that the application would live at the root of its own domain. This is true for the production version of the system and was for the large part true of the development system. When moving to IIS however, the project was set to run as a Virtual Directory – meaning the website root was no longer the same as the application root. Which lead me to trawl through the entire application tweaking things here and there, just to make it work no matter where it lived. Don’t assume you know were your application will live! It might be any number of little requirement changes that could cause you to have to rethink how you are building your application.

  2. Use ActionLinks for linking within your application
    Doing this will save you a bunch of time and is one of the core supported features of ASP.NET MVC, so why not use it when it’s so simple? There are so many good articles and posts on this, a great starting point is (as always) ScottGu’s, which part way down talks about Constructing Outgoing URLs from the Routing System

    <%=Html.ActionLink("View more details", new { Controller = "Products", Action = "Details", Id=42 })%>

  3. Url.Content for content that is URLs.
    Url.Content is to static content, what ActionLink is to your dynamic pages. If you have any Images, CSS, Script files or basically anything else that isn’t an MVC page, then this little beaut’ is for you! This allows you to negate any issues with website roots and application roots changing – without having to monitor and check any links!

    Before, you may simply have done:

    <img src="/lolcats/longcat.png" alt="To scale" />

    This would have worked until your application was moved from the webroot (Cassini) to a virtual directory (e.g. “/MVCApp” in IIS). If you do the following however, all is solved as it works out the URL and writes that out accordingly:

    <img src="<%=Url.Content("~/lolcats/longcat.png")%>" alt="To scale" />

    Would appear as the below, automagically:

    <img src="/MVCApp/lolcats/longcat.png" alt="To scale" />

  4. Relative links within your CSS
    This is something that isn’t specifically unique to MVC applications or ASP.NET in general. It’s more a good practice / make sure you are aware of guideline.
    When coding stylesheets, you’re often faced with wanted to add background-image’s to them. Without the server-side jiggery pokery that ASP.NET (and the like) allows, you are left with limited choices how to do this. But in reality, the solution is fairly simple. You need to fix two things: The location of your Images and the location of your CSS. It doesn’t really matter where they are, but just that they are fixed (or you have the patience to correct your links should you wish to restructure).

    Images from a CSS can be HTTP, absolute to the web-root or, as is awesome, relative to the CSS itself. This means that as long as all your styles are entirely located within your stylesheet’s and not intermingled with your code, you’re on to a winner. It doesn’t matter which page calls the CSS, whether it be your homepage or one that is 42 levels deep – the links are only ever relative to the CSS page (which you included via Url.Content, right, eh? yeah!?).

    In the following example, the CSS is located within a ‘Styles’ directory directly at the project root.

    .longCat
    {
     background-image: url('../lolcats/longcat.png');
    }

Hopefully these pointers will help someone else, if not, they will hopefully protect against my own stupidity and making similar mistakes again. Feel free to comment below if there are any more thoughts and portability ideas you think I could do with including.

Older Posts »

Theme designed & built for Amadiere.com by Alex Holt. Powered by WordPress