Sending email on Rackspace Cloud using asp.net
January 20, 2010
I have hosted with the Rackspace Cloud now for about two years. I really couldn’t be happier with them and would recommend them to anyone. The one thing I have seemed to have issues with is sending emails from my web applications. After talking with support and doing some testing I have a solution that should last.
Here is an example of a basic contact form.
First set the smtp information in the web.config
<system.net> <mailSettings> <smtp> <network host="mail.emailsrvr.com" userName="info@mydomain.com" password="mypassword" port="25"/> </smtp> </mailSettings> </system.net>
I put this at the very bottom of the web.config just before the closing tag.
Note that the userName and password must be of a valid email address of the domain you are sending from. Make sure to set this up through their control panel.
I also add in the appSettings of the web.config the email addresses of whom to send it to and the subject line. Just best practices.
<appSettings> <add key="mailTo" value="joel@mydomain.com"/> <add key="mailSubject" value="Contact Form inquiry"/> </appSettings>
Now my example is of an asp.net web application in c#. I am going to assume everyone knows how to setup the page, lets go to the code-behind.
You will need to add these three lines to your using block.
using System.Web.Configuration; using System.Text; using System.Net.Mail;
The submit button handler:
/// <summary>
/// Handles the contact form submit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void imtbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
StringBuilder body = new StringBuilder();
body.AppendFormat("Company Name: {0}{1}", txtCompany.Text, Environment.NewLine);
body.AppendFormat("Contact Name: {0}{1}", txtName.Text, Environment.NewLine);
body.AppendFormat("Phone: {0}{1}", txtPhone.Text, Environment.NewLine);
body.AppendFormat("Email: {0}{1}", txtEmail.Text, Environment.NewLine);
body.AppendLine();
body.AppendLine("Message");
body.AppendLine(txtMessage.Text);
MailMessage message = new MailMessage(txtEmail.Text, WebConfigurationManager.AppSettings["mailTo"], WebConfigurationManager.AppSettings["mailSubject"], body.ToString());
SmtpClient client = new SmtpClient();
client.Send(message);
pnlForm.Visible = false;
lblMessage.Visible = true;
}
Pretty simple stuff, the import part is system.net in the web.config, making sure you have the correct host and port and a valid email.
You would think they would have this in their knowledge base, but as of this writing they don’t. I hope this helps someone.
Keep mulitple ListViews on the same page.
December 15, 2009
I just had a project that required me to have a list of articles both in the the left nav and main content area.
In the left nav is was just a list of the titles. The titles were links to anchors in the main page since the actual articles were a bit lengthy.
The client only wanted 5 articles to be displayed per page. So paging was needed. I used two separate ListViews to display the news items. Along with that each ListView had a DataPager to handle the paging.
The trick was if a user clicked on page 2 in the left nav how would we update the main content ListView to move to page 2 as well?
To accomplish this I accessed the PagePropertiesChanging handler and updated both DataPager PageProperties.
So if a user clicked in the left nav the code-behind is:
protected void lvLeftNav_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
dpNews.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
dpLeftNav.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindNews();
}
As you see I update both DataPagers.
Here is the code-behind for the main news content code-behind:
protected void lvNews_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
dpNews.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
dpLeftNav.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindNews();
}
I should mention that the BindNews() method handles binding both the Left Nav and Main News to the same News List<News>.
Search Results – Webpage has expired on back button
December 8, 2009
I’ve been asked this a few times now so I thought I would blog my solution to point others to this next time I am asked.
Often times people will create a simple search on their site. The most obvious way to do this is with a simple textbox and button. You then handle the search when the button is clicked in the code-behind.
It may look something like this:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
{
BindSearch(txtSearch.Text.Trim());
}
}
Pretty straight forward. The problem here is you’re doing a postback to the same page. You won’t have a history of the previous searches in your browser. After doing a search and you want to see what you had for results from the previous search and simply hit back in your browser you’ll see this
So what I have always done to avoid this is to simply do a Response.Redirect to the page instead of just calling the BindSearch() method and put the search term in the query string.
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
{
Response.Redirect("/Search.aspx?search=" + txtSearch.Text.Trim());
}
}
This is a pretty simple solution, does anyone else have a different way to handle this?
Controls not being registered in designer.cs
August 3, 2009
I’ve encountered this a few times now.
When dealing with a webform with an extraordinary amount of controls, ie. textboxes, dropdownlists, panels, etc. I’ve noticed that after x amount of controls have been added they are no longer registered in the designer.cs file.
I notice this by first the control not showing up in intellisense when trying to use it in code-behind. If I manually type it out I will still get a build error.
So, at this point I have to manually register any new controls in the designer.cs file. At the top of the file it says,
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
However it seems that they are never lost and now everything works fine. I am not sure if this is normal behavior and this is what you are suppose to do, but so far it has worked for me.
Has anyone else seen this, does anyone know the exact amount of controls that you can add to a page before they are no longer registered in the designer.cs?
asp.net dynamic meta and title tags
July 1, 2009
Using a Master page.
Problem for SEO is it really scews up the formatting and positioning of the tags.
First get the data there.
If you’re not overly concerned about formatting of the title tag, in the content page:
Page.Title = “The title of your page”;
// Add the keywords meta tag
HtmlMeta metaKeywords = new HtmlMeta();
metaKeywords.Name = "keywords";
metaKeywords.Content = nseCase.MetaKeywords;
Page.Header.Controls.AddAt(1, metaKeywords);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));
// Add the description meta tag
HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = "description";
metaDescription.Content = nseCase.Description;
Page.Header.Controls.AddAt(1,metaDescription);
Page.Header.Controls.AddAt(1, new LiteralControl("\n"));
What is happening is the meta tags are being added to the first line in the head tag (below the title tag). Think of it like a stack, that is why it is being done in reverse order in which you would think.
If you want to do a little more work to remove the spacing and line breaks from the title tag you need to take this approach.
In the Master page remove the runat=”server” from the head tag. Now you can create literal tags for each desired tag in the <head> tag.
<head>
<asp:Literal ID=”litTitle” runat=”server” />
<asp:Literal ID=”litMetaDescription” runat=”server” />
<asp:Literal ID=”litMetaKeywords” runat=”server” />
</head>
Now in your content page you can access these literal controls by:
Literal title = (Literal)Master.FindControl("litTitle");
title.Text = String.Format("<title>{0}</title>", “Your dynamic title”);
Literal metaDescription = (Literal)Master.FindControl(“litMetaDescription”);
metaDescription.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “description”, “here is your page description”);
Literal metaKeywords = (Literal)Master.FindControl(“litMetaKeywords”);
metaKeywords.Text = String.format(“<meta name=\”{0}\” content=\”{1}\” />”, “keywords”, “here, are, your, pages, keywords”);
The first approach is quicker to do and still allows you to put a default title in it. You may not want to set the title and meta tags for each page.
The second approach requires that you set the title and meta tags on every page if you want them. I suppose you could use a contentplaceholder for many of the pages which is quicker than doing it in the code-behind for pages that you don’t need the tags to be dynamic.
My SEO guy is finally happy that I came up with a solution for this. I hope this helps you get an SEO guy off your back.
-
Categories
- asp.net (11)
- Blog (1)
- Business (5)
- Conferences (1)
- Errors (3)
- Portfolio (2)
- Reviews (2)
- SQL Server (3)
- Tips (1)
- Uncategorized (1)
- Web Development (13)
-
Archives