Blog

ASP.net: Sending Application Mail through Google Apps (GMail)

I ran into a little gotcha when setting up a new web application to send email through the Google Apps SMTP servers. When you read the Google support documentation on setting up a client to send email through their SMTP server you will see there are 2 options SSL, TSL with a different port for each.
When writing the C# code to send the email there is an option to on the .Net SmtpClient object called ‘EnableSsl’. You of course need to set this to true but the gotcha comes in when you setup the SMTP server and port in your web.config file. You would assume since the option ‘EnableSsl’ refers to ‘SSL’ you would need to use port 465 for SSL as outlined by Google in their documentation. But. you would be wrong to assume that and on every call to send an email within your application you will run into a ‘Time Out’.
I don’t know everything about .Net mail but apparently ‘EnableSsl’ doesn’t really mean SSL it means TSL. I say this because as soon as I changed the SMTP server’s port in my web.config from the SSL port of 465 to the TSL port of 587 as outlined by Google my mail sent without issue.
Hopefully this will save someone a lot of time.
Web.config Setup:

  <system.net>
    <mailSettings>
      <smtp from="noreply@yourdomain.com">
        <network host="smtp.gmail.com" password="******" userName="noreply@yourdomain.com" port="587" />
      </smtp>
    </mailSettings>
  </system.net>

C# Send Mail Code:

            try
            {
                MailMessage message = new MailMessage();

                message.From = new MailAddress(tbxEmail.Text);
                message.To.Add(new MailAddress(_supportEmail));

                message.Subject = tbxSubject.Text;
                message.IsBodyHtml = false;
                message.Priority = MailPriority.High;
                message.Body = tbxMessage.Text;


                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Send(message);

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler handler = new ExceptionHandler();
                handler.LogException(ex,
                    SeverityLevel.Critical,
                    "Contact Form Message could not be sent to support email",
                    GetType(),
                    "SendMessageEmail");
            }

XSLT: Missing Closing / Self Closing Tags (img, br,…)

If you are using XSLT to transform XML documents into HTML for display within a website and you have found that your ,
and other regularly self-closing tags are not being closed in the transform output its a good chance you need to take a look at the output method being used by the XSLT.
The output method of the XSLT determines just how strictly the output will follow the XML standard. Add the following to your XSLT:

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

Telerik: RadChart working in DotNetNuke

In order to get the RadChart charting controls to work correctly in DotNetNuke you must first take into account that DNN rewrites all URLs to what are called ‘Pretty URLs’. Because of this the RadChart has a hard time locating the URL of the HttpHandler which creates the chart images.
To fix this you need to help the RadChart instance out by telling it where the HttpHandler URL is. This can be done by setting the HttpHandler URL through the RadChart’s HttpHandlerUrl property in the Page Load event of your DNN module control as shown below.

myRadChartInstance.HttpHandlerUrl = ResolveUrl("ChartImage.axd") ;

	

Telerik: Execute JavaScript on RadAjaxPanel PostBack

If you need to execute a JavaScript script upon the return of your RadAjaxPanel for example to slowly fade or hide a notification message to a user such as in the image below.

To accomplish this all you need to do is add your script to the RadAjaxPanel through the ResponseScript property.
Here is the code for hiding the #Message div after the Ajax call has returned it uses the JQuery JavaScript library.

private void AddHideScript()
        {
            rapCities.ResponseScripts.Add("$('#Message').animate({ opacity: 'hide' }, 5000);");
        }

Telerik: JavaScript Confirm in RadAjaxPanel

To properly warn users before they delete an item from a RadGrid within a RadAjaxPanel you need to add a OnClientClick client side event to the delete button as follows.

<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="DeleteItem" CausesValidation="false" OnClientClick="if(!confirm('Warning: This City will be permanently deleted. Would you still like to continue?'))return false;" />

JavaScript: Confirm User Action Script

Code:

<script language="javascript" type="text/javascript">
    function ConfirmDeleteSomething() {
        return confirm("Warning: This Something will be permanently deleted. Would you still like to continue? ");
    }



<asp:LinkButton ID="lbtnDelete" runat="server" 
    OnClick="lbtnDelete_Click"
    CausesValidation="false"
    OnClientClick="return ConfirmDeleteSomething()">Delete Something

Summary:

To make sure that a user is actually sure they want to perform an unrecoverable action, such as deleting something, you may want to add a confirmation dialog warning the user of what their potential actions may do.

To do this using JavaScript simply add a function which calls the ‘confirm’ command and call that function through the ‘OnClientClick’ event of your button control in ASP.net.