Send Email with ASP.Net MVC3 using GoDaddy Hosting

 Send Email with ASP.Net MVC3 using GoDaddy HostingI use GoDaddy for my Windows Hosting. It’s affordable, supports ASP.Net 4.0 with MVC3, and supports multiple domains and SQL servers. The first issue I ran into was sending mail in code which is a little different than other Windows hosting providers. Here is what I did to get it to work perfectly without needing an actual email account with GoDaddy.






First I created a folder in my MVC3 project named Helpers and added a class called Email.cs with the following code.

public class Email
    {
        internal static void SendEmail(string fromAddress, string subject, string body)
        {
            var fAddress = new MailAddress("anything@yourdomain.com");
            var tAddress = new MailAddress("anything@yourdomain.com");
            var message = new MailMessage(fAddress, tAddress)
            {
                Subject = subject + " From " + fromAddress,
                Body = body
            };

            var client = new SmtpClient("relay-hosting.secureserver.net");
            client.Send(message);
        }
    }

Then I created a Model in my Model folder called ContactModel.cs with the following code.

public class ContactModel
    {
        [Required]
        [RegularExpression(@"^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$")]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email Address")]
        public string EmailAddress { get; set; }

        [Required]
        [Display(Name = "Message")]
        public string UserMessage { get; set; }
    }

Then I created a ActionResult in my Home controller although you can create a whole separate controller if you wish works either way. Here is the ActionResult code. ContactSuccess is just a page that says it works.

public ActionResult Contact()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Contact(ContactModel model)
        {
            if (ModelState.IsValid)
            {
                Email.SendEmail(model.EmailAddress, "Website Contact", model.UserMessage);
                return Redirect("ContactSuccess");
            }
            else
            {
                ModelState.AddModelError("", "All fields are required.");
            }
            return View(model);
        }

        public ActionResult ContactSuccess()
        {
            return View();
        }

Here is the code in my View file for the actual contact form Contact.chtml.

@model ProjectName.Models.ContactModel
           
@{
    ViewBag.Title = "Contact Us";
    }

<h2>Contact Us</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate-vsdoc.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <p><strong>@Html.LabelFor(m => m.EmailAddress)</strong><br/>
    @Html.TextBoxFor(m => m.EmailAddress)
    @Html.ValidationMessageFor(m => m.EmailAddress, "*")</p>
        
    <p><strong>@Html.LabelFor(m => m.UserMessage)</strong><br/>
    @Html.TextAreaFor(m => m.UserMessage)
    @Html.ValidationMessageFor(m => m.UserMessage, "*")</p>
    
    <p><input type="submit" value="Send" /></p>
}

Not to sell out because I actually like GoDaddy’s Windows Hosting you can get a discounted price and help me pay my bills by clicking Here. icon smile Send Email with ASP.Net MVC3 using GoDaddy Hosting

10 Must Have WordPress Plugins

wordpress logo notext rgb 300x300 10 Must Have WordPress PluginsI run multiple WordPress blogs and websites. Over time I have come to rely on a handful of plugins that I use on every site. Here is the list.

#1 All in one Favicon

One thing you want to avoid with WordPress sites is manually editing your theme files. Previously I would manually insert favicon code in my header.php but switching themes would cause me to re-edit the new header file. This takes care of it for you no matter how often you switch themes.

#2 All in One SEO Pack

Not only can you put your Google Analytics ID in the settings for this plugin but it also generates keywords based on your meta tags which is a great for keeping each post SEO friendly and searchable on the major search engines.

#3 FD Feedburner Plugin

If you plan to monetize your website using Google AdSense then Feedburner is a must. AdSense allows you to display adds in your RSS feed through feedburner. This plugin redirects your site’s RSS feeds to FeedBurner.

#4 Google XML Sitemaps

This plugin automatically creates and updates your sitemap.xml file whenever you create a new post or page allowing search engines to index your site properly. Must have for SEO compliance.

#5 Jetpack by WordPress.com

This plugin has about a dozen mini plugins within it that handle everything from video shortcodes, WordPress stats, sharing widgets for every post, shortlinks and more. This plugin keeps my plugin count down with all the great stuff it takes care of for you.

#6 reCAPTCHA Form

I used to use Contact Form 7 but would constantly get spam sent through it. After unsuccessfully trying to integrate re-captcha into contact form 7 I decided to use this plugin instead. It is just as great as cf7 but includes a recaptcha field at the end to thwart spam bots.

#7 WP-reCAPTCHA

Similar to the plugin above, this adds a re-captcha form to new user registration as well as comment forms for un-registered users. Your registered users will never see it keeping it from getting in the way of regular use.

#8 WPtouch

If you plan on users accessing your site from a mobile device make sure you use this plugin. Not only does it convert your site to a mobile version you can include you AdSense ID to display mobile friendly ads. You can also control what you want to be displayed excluding certain categories, posts or pages.

#9 W3 Total Cache

Caching is important for speed and server costs. Not only does this plugin handle caching of everything it also minimizes and combines javascript and css. There is also the ability to use a content delivery network. Tons of options to play with producing real time results. A must have.

#10 WP Security Scan

This plugin alerts you to any security vulnerabilities within your WordPress site and within the web server itself. Additionally you can sign up for a free monitoring service that alerts you by email if something goes wrong. The plugin itself is a one time use type of thing but the monitoring is invaluable. If my site goes down or something changes without my knowledge I get an email. Priceless.

Install Ruby on Rails with Passenger on MediaTemple DV 3.5

railsmt Install Ruby on Rails with Passenger on MediaTemple DV 3.5

Updated 10/22/2009 to reflect new stable versions of the software used.

The following article is a summary of the 3 days I spent trying to get this to work. The installation is not the same installation explained by Passenger’s website and a ton of other websites. This article pertains to Ruby on Rails, Media Temple and Passenger for Apache a simple to use replacement for Mongrel.

First of all you have to have a fresh (or re-imaged) DV 3.5 account with MediaTemple otherwise this article does not apply to you.

  1. Go to your MediaTemple account center and click the domain you are using. You will then see a link labeled Root Access & Developer Tools. Go there and activate both.
  2. Open up you favorite ssh program to access to the great unix command line of your dedicated server. I use plain old terminal on Mac OSX.
  3. Login to your server with the command ssh root@yourdomain.com
  4. Run the following command to update your CentOS rpm -Uvh http://mirror.centos.org/centos/5/os/i386/CentOS/python-iniparse-0.2.3-4.el5.noarch.rpm
  5. Do the same with this command rpm -Uvh http://mirror.centos.org/centos/5/os/i386/CentOS/yum-3.2.8-9.el5.centos.1.noarch.rpm
  6. Use YUM to remove the current Ruby install so we can update to the latest version by running yum remove ruby
  7. Change to your opt directory cd /opt
  8. Get the current Ruby build, I am providing a link for 1.8.7 which is the current version as of 2-24-2009 wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p243.tar.gz
  9. Now run the tar command to extract the archive we just downloaded tar -zxvf ruby-1.9.1-p243.tar.gz
  10. Now we are going to create a link so you can access ruby without added the version numbers ln -s ruby-1.9.1-p243 ruby
  11. Now lets head to our ruby directory cd ruby
  12. Run the command to install ruby ./configure && make && make install
  13. Lets pop out of this directory now cd ../
  14. We should now grab the latest RubyGems version which is 1.3.1 as of 2-24-2009 wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
  15. Uncompress the archive tar -zxvf rubygems-1.3.5.tgz
  16. Goto the new directory cd rubygems-1.3.5
  17. Run the setup to install RubyGems ruby setup.rb
  18. Now that we have gems installed we can start installing things in a much more simple manner. Start with Rails. gem install rails
  19. Now we will install Passenger the great Apache RoR app that we will be using instead of mongrel. gem install passenger
  20. We need to run one additional command to get Passenger setup passenger-install-apache2-module

That was the basic installation process. Now we will move on to the account specific modifications that are required to get this to work with MediaTemple. Pay attention to the text don’t just copy and paste there will be lines you need to modify. Pay attention to anything that say yourdomain.com. You need to replace this with your actual domain.

  1. First things first backup your apache config file. cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.backup
  2. You may need to edit these paths depending on what the current version of passenger is. We will now use the unix built in vi editor to edit our config file. Do this by running vi /etc/httpd/conf/httpd.conf then we scroll down to the section that has all the LoadModules commands. Go to the last space and press i to enter insert mode. Add the following to your file.
    LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.1.3/ext/apache2/mod_passenger.so
    PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.1.3
    PassengerRuby /usr/local/bin/ruby

    now just press escape to exit insert mode. Type :w and press enter then type :q and enter. You should be back at the console prompt.
  3. Apache is now setup correctly. Next we will create a nifty little file that MT uses for VirtualHosts. This is where things get different since normally you would put this info in the apache config. Create a new file by typing the following vi /var/www/vhosts/yourdomain.com/conf/vhost.conf then press i to enter insert mode. Copy and paste the following text. ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/vhosts/yourdomain.com/rails/yourappname/public
    <Directory “/var/www/vhosts/yourdomain.com/rails/yourappname/public”>
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
    RailsBaseURI /
    then press escape to exit insert mode. Then type :w and enter then :q and enter. Your back at the command line.
  4. Let create our rails directory now so we can setup our app. cd /var/www/vhosts/yourdomain.com then mkdir rails the cd rails
  5. Now we can create our rails application by typing rails yourappname
  6. Your rails app will be created now we have to give your user account that you use on MT permission by typing chown -R yourusername:psaserv yourappname
  7. Almost done, let’s move our httpdocs directory before we create a link. mv /var/www/vhosts/yourdomain.com/httpdocs /var/www/vhosts/yourdomain.com/httpdocs.old
  8. Now we can create a link redirecting visitors to our rails app instead of httpdocs. ln -s /var/www/vhosts/yourdomain.com/rails/yourappname/public /var/www/vhosts/yourdomain.com/httpdocs
  9. Run this command to update the server to let it know about our VHost file we created earlier /usr/local/psa/admin/sbin/websrvmng -a
  10. Restart apache service httpd restart
  11. Your done! You can upload changed files to your website at that rails/yourappname directory. Hope that helps anyone stuck.

Thanks for reading hope it was helpful.