Decrease Font Size
Increase Font Size

ASP.NET Sending email through Hotmail

ASP .NET How to Send Email asynchronously | ASP .NET sends email using account

I was browsing the ASP.NET forum the other day and saw a topic "Problem sending email" using hotmail account Asynchronously. Personally, I haven't tried that before and I did get it to work. Below is the code in and CSharp version. Make sure to set Async="true" in the page directive because we are sending the email Asynchronously.

Here is the entry in Web.config

      <smtp from=""> 
        <network host="" defaultCredentials="false"  port="25" userName ="" password="myHotmailPassword" />

This is the ASPX page

<%@ Page Language="VB" Async="true" AutoEventWireup="false" CodeFile="TestHotmail.aspx.vb" Inherits="TestHotmail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>


Imports System.Net.Mail
Partial Class TestHotmail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Button1.Click, AddressOf Button1_Click
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mailMessage As New System.Net.Mail.MailMessage

        mailMessage.To.Add(New MailAddress(""))
        mailMessage.From = New MailAddress("")

        mailMessage.Subject = "my test subject"
        mailMessage.Body = "my test body"
        mailMessage.IsBodyHtml = True

        Dim smtpClient As New SmtpClient()
        smtpClient.EnableSsl = True

        Dim userState As Object = mailMessage

        AddHandler smtpClient.SendCompleted, AddressOf smtpClient_OnCompleted
        smtpClient.SendAsync(mailMessage, userState)

    End Sub

    Public Sub smtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        Dim mailMessage As MailMessage

        mailMessage = CType(e.UserState, MailMessage)

        If (e.Cancelled) Then
            lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To(0).Address
        End If
        If Not (e.Error Is Nothing) Then
            lblMessage.Text = "Error occured, info :" + e.Error.Message
            lblMessage.Text = "Mail sent successfully"
        End If
    End Sub

End Class


If you are using CSharp, this is the code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class TestHotmailCsharp : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        Button1.Click +=new EventHandler(Button1_Click);

    protected void Button1_Click(object sender, EventArgs e)
        System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

        mailMessage.To.Add(new MailAddress(""));
        mailMessage.From = new MailAddress("");

        mailMessage.Subject = "my test subject";
        mailMessage.Body = "my test body";
        mailMessage.IsBodyHtml = true;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.EnableSsl = true;

        object userState = mailMessage;

        smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
        smtpClient.SendAsync(mailMessage, userState);

    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        MailMessage mailMessage = default(MailMessage);

        mailMessage = (MailMessage)e.UserState;

        if ((e.Cancelled))
            lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To[0].Address;
        if ((e.Error != null))
            lblMessage.Text = "Error occured, info :" + e.Error.Message;
            lblMessage.Text = "Mail sent successfully";


Send Email Asynchronously with ASP.NET
Incoming and Outgoing Mail Server Settings for Hotmail, Yahoo! Mail, GMail, MSN, AOL and more

Comments (16) -

  • miracle cabbage soup diet

    11/3/2009 2:18:21 AM |

    Thanks for such a nice post.


  • Niche Blueprint 2.0 Review

    11/16/2009 6:59:07 PM |

    I love reading your posts. I wish you a Happy New Year!

  • lifestyle on the net

    11/19/2009 5:50:05 AM |

    WOW ... I learn lots of new stuff here ! regard !!

  • Best Car Rental

    12/11/2009 7:14:29 PM |

    Thanks for such a nice post. I learnt a lot !

  • Top Destinations

    12/12/2009 11:50:48 AM |

    still works great

  • Lisa Lunndra

    12/15/2009 12:30:05 AM |

    I am surprised at the things I overlooked before I read this post. Thanks for the great information.

  • chicago furniture stores

    12/21/2009 12:26:39 AM |

    it is so happy can visit the blog Smile

  • bambang tri

    12/22/2009 9:59:59 AM |

    Aaaargh, I wish I read this article sooner.
    Nice share, thanks.

  • business opportunities from home

    12/28/2009 1:24:02 PM |

    Insightful piece, thanks a lot!

  • diets that work

    12/30/2009 4:05:36 PM |

    Thanks for putting this together - this is a great article for those of us with our heads buried in the keyboard all day.

  • jillian

    12/31/2009 4:20:50 PM |

    Thanks very much for the information. I have been searching for this for awhile with Google and it has been a real chore.

  • 35mm slide scanners

    1/6/2010 10:18:39 PM |


    I always wonderd how i could send mails trough hotmail.



  • ripped abs

    1/19/2010 12:50:09 PM |

    This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article!

  • Tech Blog

    1/20/2010 2:08:21 AM |

    Thanks. This makes my life a lot easier.

  • Watch Movies Online

    1/20/2010 3:53:24 AM |

    Such a Wonderful Job... Solved my Problem.. i just needed that.

  • cordless irons

    12/28/2010 9:33:31 AM |

    Thanks you for this helpful information.

Pingbacks and trackbacks (1)+

Add comment