Sending mail with ASP.NET and Gmail

After a server upgrade I had to setup my mail again. It seems that IIS7 has lost it’s core mail functionality, so I tried to use the SMTP service from Gmail for sending my e-mail. First thing I did is to setup a simple test page, that I would like to share with you.

Front

The page contains a simple web form for inserting some test data and is looking like this:

 

Knipsel

 
 
 
 

The markup code

 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gmail_mailtest.aspx.cs" Inherits="SendMail" %>
<html>
<head runat="server"><title>Email Test Page</title></head>
<body>
    <form id="form1" runat="server">
        Message to: <asp:TextBox ID="txtTo" runat="server" Text="[your_mail]" /><br>
        Message from: <asp:TextBox ID="txtFrom" runat="server" Text="[yourname]@gmail.com" /><br>
        Subject: <asp:TextBox ID="txtSubject" runat="server" Text="test from mailtest.aspx" /><br>
        Message Body:<br>
        <asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px" Text="test123" /><br>
        <asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email via Gmail" /><br>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Code behind

The code behind is handling the connection to Gmail:

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
    protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
            txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;
        smtpClient.Credentials = new System.Net.NetworkCredential("[yourname, without the @gmail.com]","[PaSSWoRD]");
        smtpClient.EnableSsl = true;

        try
        {
           smtpClient.Send(mailObj);
           Label1.Text = "Mail is sent!";
        }
            catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }
}
 
Tadaa, it works like a charm!

Using .NET methods in your XSLT

Not many people know that it is possible to use a .NET object as an extension object when transforming XML with XSLT. Imagine that you want to recalculate the price of a book and round the result number. Impossible to calculate within XSLT, since it’s a transformation language and not a programming language.

Solution is to create a class with a method that performs the calculation. Then when transforming add this opject as an extension object to the XsltArgumentList.

The C# code:

 

    public class BookPrice
    {
        public decimal NewPrice(decimal price, decimal conv)
        {
            return decimal.Round(price * conv, 2);
        }
    }
 
     private void btnTransExten_Click(object sender, EventArgs e)
        {
            XslCompiledTransform trans = new XslCompiledTransform();
            trans.Load(@"..\..\prices.xslt");
 
            XsltArgumentList args = new XsltArgumentList();
 
            BookPrice price = new BookPrice();
            args.AddExtensionObject("urn:price-conv", price);
 
            trans.Transform(@"..\..\books.xml", args, XmlWriter.Create(@"c:\outputext.xml"));
           
            MessageBox.Show("File transformed.");
 
        }

 

The XSLT will look something like this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:price-conv">
  <xsl:param name="conv" select="1.15"/>
  <xsl:template match="booklist">
    <booklist>
      <xsl:for-each select="book">
        <book>
          <xsl:copy-of select="node()"/>
          <new-price>
            <xsl:value-of select="myObj:NewPrice(./price, $conv)"/>
          </new-price>
        </book>
      </xsl:for-each>
    </booklist>
  </xsl:template>
</xsl:stylesheet>

 

Hope this helps,

Two useful jQuery scripts for forms

Here are two useful jQuery scripts for online forms.

The first is to prevent double submission of a form a.k.a. ‘anti-double-click’.

$("form").submit(function() {
$(":submit",this).attr("disabled", "disabled");
});

The second is a script to disable auto complete for all fields in a form:

$('input[type=text]').attr('autocomplete', 'off');

 

Hope this helps,